Neos\Flow\I18n\Xliff\XliffParser::doParsingFromRoot PHP Method

doParsingFromRoot() protected method

Returns array representation of XLIFF data, starting from a root node.
protected doParsingFromRoot ( SimpleXMLElement $root ) : array
$root SimpleXMLElement A root node
return array An array representing parsed XLIFF
    protected function doParsingFromRoot(\SimpleXMLElement $root)
    {
        $parsedData = ['sourceLocale' => new Locale((string) $root->file['source-language'])];
        foreach ($root->file->body->children() as $translationElement) {
            switch ($translationElement->getName()) {
                case 'trans-unit':
                    // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
                    if (!isset($translationElement['restype'])) {
                        if (!isset($translationElement['id'])) {
                            throw new Exception\InvalidXliffDataException('A trans-unit tag without id attribute was found, validate your XLIFF files.', 1329399257);
                        }
                        $parsedData['translationUnits'][(string) $translationElement['id']][0] = ['source' => (string) $translationElement->source, 'target' => (string) $translationElement->target];
                    }
                    break;
                case 'group':
                    if (isset($translationElement['restype']) && (string) $translationElement['restype'] === 'x-gettext-plurals') {
                        $parsedTranslationElement = [];
                        foreach ($translationElement->children() as $translationPluralForm) {
                            if ($translationPluralForm->getName() === 'trans-unit') {
                                // When using plural forms, ID looks like this: 1[0], 1[1] etc
                                $formIndex = substr((string) $translationPluralForm['id'], strpos((string) $translationPluralForm['id'], '[') + 1, -1);
                                $parsedTranslationElement[(int) $formIndex] = ['source' => (string) $translationPluralForm->source, 'target' => (string) $translationPluralForm->target];
                            }
                        }
                        if (!empty($parsedTranslationElement)) {
                            if (isset($translationElement->{'trans-unit'}[0]['id'])) {
                                $id = (string) $translationElement->{'trans-unit'}[0]['id'];
                                $id = substr($id, 0, strpos($id, '['));
                            } else {
                                throw new Exception\InvalidXliffDataException('A trans-unit tag without id attribute was found, validate your XLIFF files.', 1329399258);
                            }
                            $parsedData['translationUnits'][$id] = $parsedTranslationElement;
                        }
                    }
                    break;
            }
        }
        return $parsedData;
    }