src/Viserio/Component/Parser/Parser/XliffParser.php
private function extractXliffVersion2(DOMDocument $dom): array
    {
        $xml = XmlUtils::importDom($dom);

        $encoding = \mb_strtoupper($dom->encoding);
        $datas    = [
            'version' => '2.0',
            'srcLang' => '',
            'trgLang' => '',
        ];

        foreach ($xml->attributes() as $key => $value) {
            if ($key === 'srcLang' || $key === 'trgLang') {
                $datas[$key] = (string) $value;
            }
        }

        $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');

        foreach ((array) $xml->xpath('//xliff:unit') as $unit) {
            $unitAttr = (array) $unit->attributes();
            $unitAttr = \reset($unitAttr);
            $source   = (string) $unit->segment->source;
            $target   = null;
            $id       = $unitAttr['id'];

            if (isset($unit->segment->target)) {
                $target = self::utf8ToCharset((string) $unit->segment->target, $encoding);
            }

            $datas[$id] = [
                'source' => $source,
                // If the xlf file has another encoding specified, try to convert it because
                // simple_xml will always return utf-8 encoded values
                'target' => $target,
            ];

            if ($target !== null && $unit->segment->target->attributes()) {
                $datas[$id]['target-attributes'] = [];

                foreach ($unit->segment->target->attributes() as $key => $value) {
                    $datas[$id]['target-attributes'][$key] = (string) $value;
                }
            }

            if (isset($unit->notes)) {
                $metadata['notes'] = [];

                foreach ($unit->notes->note as $noteNode) {
                    $note = [];

                    foreach ($noteNode->attributes() as $key => $value) {
                        $note[$key] = (string) $value;
                    }

                    $note['content']       = (string) $noteNode;
                    $datas[$id]['notes'][] = $note;
                }
            }
        }

        return $datas;
    }
private function extractXliffVersion2(DOMDocument $dom): array
    {
        $xml = XmlUtils::importDom($dom);

        $encoding = \mb_strtoupper($dom->encoding);
        $datas    = [
            'version' => '2.0',
            'srcLang' => '',
            'trgLang' => '',
        ];

        foreach ($xml->attributes() as $key => $value) {
            if ($key === 'srcLang' || $key === 'trgLang') {
                $datas[$key] = (string) $value;
            }
        }

        $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');

        foreach ((array) $xml->xpath('//xliff:unit') as $unit) {
            $unitAttr = (array) $unit->attributes();
            $unitAttr = \reset($unitAttr);
            $source   = (string) $unit->segment->source;
            $target   = null;
            $id       = $unitAttr['id'];

            if (isset($unit->segment->target)) {
                $target = self::utf8ToCharset((string) $unit->segment->target, $encoding);
            }

            $datas[$id] = [
                'source' => $source,
                // If the xlf file has another encoding specified, try to convert it because
                // simple_xml will always return utf-8 encoded values
                'target' => $target,
            ];

            if ($target !== null && $unit->segment->target->attributes()) {
                $datas[$id]['target-attributes'] = [];

                foreach ($unit->segment->target->attributes() as $key => $value) {
                    $datas[$id]['target-attributes'][$key] = (string) $value;
                }
            }

            if (isset($unit->notes)) {
                $metadata['notes'] = [];

                foreach ($unit->notes->note as $noteNode) {
                    $note = [];

                    foreach ($noteNode->attributes() as $key => $value) {
                        $note[$key] = (string) $value;
                    }

                    $note['content']       = (string) $noteNode;
                    $datas[$id]['notes'][] = $note;
                }
            }
        }

        return $datas;
    }
Extract messages and metadata from DOMDocument into a MessageCatalogue.

@param \DOMDocument $dom

@throws \Viserio\Component\Contract\Parser\Exception\ParseException

@return array