loadXML($bytes, LIBXML_NONET) || $document->documentElement?->localName !== 'Task' || $document->documentElement->namespaceURI !== 'http://schemas.microsoft.com/windows/2004/02/mit/task') { throw new LogicException('WINDOWS_TASK_XML_INVALID'); } return $document; } finally { libxml_clear_errors(); libxml_use_internal_errors($previous); } } public static function encode(string $xml): string { $document = self::parse($xml); $document->encoding = 'UTF-16'; $bytes = $document->saveXML(); if (! is_string($bytes) || self::parse($bytes)->documentElement->C14N() !== $document->documentElement->C14N()) { throw new LogicException('WINDOWS_TASK_XML_ENCODING_CHANGED_CONTENT'); } return $bytes; } }