@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @param LoggerInterface $logger |
32 | 32 | */ |
33 | - public function __construct( LoggerInterface $logger = null ) { |
|
33 | + public function __construct(LoggerInterface $logger = null) { |
|
34 | 34 | $this->logger = $logger; |
35 | 35 | } |
36 | 36 | |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | * @param bool $setSourceInTarget |
46 | 46 | * @param XliffReplacerCallbackInterface|null $callback |
47 | 47 | */ |
48 | - public function replaceTranslation( $originalXliffPath, &$data, &$transUnits, $targetLang, $outputFile, $setSourceInTarget = false, XliffReplacerCallbackInterface $callback = null ) { |
|
48 | + public function replaceTranslation($originalXliffPath, &$data, &$transUnits, $targetLang, $outputFile, $setSourceInTarget = false, XliffReplacerCallbackInterface $callback = null) { |
|
49 | 49 | try { |
50 | - $parser = XliffReplacerFactory::getInstance( $originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback ); |
|
50 | + $parser = XliffReplacerFactory::getInstance($originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback); |
|
51 | 51 | $parser->replaceTranslation(); |
52 | - } catch ( Exception $exception ) { |
|
52 | + } catch (Exception $exception) { |
|
53 | 53 | // do nothing |
54 | 54 | } |
55 | 55 | } |
@@ -67,26 +67,26 @@ discard block |
||
67 | 67 | * @throws InvalidXmlException |
68 | 68 | * @throws XmlParsingException |
69 | 69 | */ |
70 | - public function xliffToArray( $xliffContent, $collapseEmptyTags = false ) { |
|
70 | + public function xliffToArray($xliffContent, $collapseEmptyTags = false) { |
|
71 | 71 | $xliff = []; |
72 | - $xliffContent = self::forceUft8Encoding( $xliffContent, $xliff ); |
|
73 | - $xliffVersion = XliffVersionDetector::detect( $xliffContent ); |
|
74 | - $info = XliffProprietaryDetect::getInfoFromXliffContent( $xliffContent ); |
|
72 | + $xliffContent = self::forceUft8Encoding($xliffContent, $xliff); |
|
73 | + $xliffVersion = XliffVersionDetector::detect($xliffContent); |
|
74 | + $info = XliffProprietaryDetect::getInfoFromXliffContent($xliffContent); |
|
75 | 75 | |
76 | - if ( $xliffVersion === 1 ) { |
|
77 | - $xliffContent = self::removeInternalFileTagFromContent( $xliffContent, $xliff ); |
|
76 | + if ($xliffVersion === 1) { |
|
77 | + $xliffContent = self::removeInternalFileTagFromContent($xliffContent, $xliff); |
|
78 | 78 | } |
79 | 79 | |
80 | - if ( $xliffVersion === 2 ) { |
|
81 | - $xliffContent = self::escapeDataInOriginalMap( $xliffContent ); |
|
80 | + if ($xliffVersion === 2) { |
|
81 | + $xliffContent = self::escapeDataInOriginalMap($xliffContent); |
|
82 | 82 | } |
83 | 83 | |
84 | - if ( $collapseEmptyTags === false ) { |
|
85 | - $xliffContent = self::insertPlaceholderInEmptyTags( $xliffContent ); |
|
84 | + if ($collapseEmptyTags === false) { |
|
85 | + $xliffContent = self::insertPlaceholderInEmptyTags($xliffContent); |
|
86 | 86 | } |
87 | 87 | |
88 | - $xliffProprietary = isset( $info[ 'proprietary_short_name' ] ) ? $info[ 'proprietary_short_name' ] : null; |
|
89 | - $parser = XliffParserFactory::getInstance( $xliffVersion, $xliffProprietary, $this->logger ); |
|
88 | + $xliffProprietary = isset($info['proprietary_short_name']) ? $info['proprietary_short_name'] : null; |
|
89 | + $parser = XliffParserFactory::getInstance($xliffVersion, $xliffProprietary, $this->logger); |
|
90 | 90 | |
91 | 91 | $dom = XmlDomLoader::load( |
92 | 92 | $xliffContent, |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | ) |
98 | 98 | ); |
99 | 99 | |
100 | - return $parser->parse( $dom, $xliff ); |
|
100 | + return $parser->parse($dom, $xliff); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -109,12 +109,12 @@ discard block |
||
109 | 109 | * |
110 | 110 | * @return string |
111 | 111 | */ |
112 | - private static function forceUft8Encoding( $xliffContent, &$xliff ) { |
|
113 | - $enc = mb_detect_encoding( $xliffContent ); |
|
112 | + private static function forceUft8Encoding($xliffContent, &$xliff) { |
|
113 | + $enc = mb_detect_encoding($xliffContent); |
|
114 | 114 | |
115 | - if ( $enc !== 'UTF-8' ) { |
|
116 | - $xliff[ 'parser-warnings' ][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
117 | - $s = iconv( $enc, 'UTF-8', $xliffContent ); |
|
115 | + if ($enc !== 'UTF-8') { |
|
116 | + $xliff['parser-warnings'][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
117 | + $s = iconv($enc, 'UTF-8', $xliffContent); |
|
118 | 118 | $xliffContent = $s !== false ? $s : ""; |
119 | 119 | } |
120 | 120 | |
@@ -131,24 +131,24 @@ discard block |
||
131 | 131 | * |
132 | 132 | * @return mixed|string |
133 | 133 | */ |
134 | - private static function removeInternalFileTagFromContent( $xliffContent, &$xliff ) { |
|
134 | + private static function removeInternalFileTagFromContent($xliffContent, &$xliff) { |
|
135 | 135 | $index = 1; |
136 | - $a = Strings::preg_split( '|<internal-file[\s>]|i', $xliffContent ); |
|
136 | + $a = Strings::preg_split('|<internal-file[\s>]|i', $xliffContent); |
|
137 | 137 | |
138 | 138 | // no match, return original string |
139 | - if ( count( $a ) === 1 ) { |
|
140 | - return $a[ 0 ]; |
|
139 | + if (count($a) === 1) { |
|
140 | + return $a[0]; |
|
141 | 141 | } |
142 | 142 | |
143 | - $b = Strings::preg_split( '|</internal-file>|i', $a[ 1 ] ); |
|
144 | - $strippedContent = $a[ 0 ] . $b[ 1 ]; |
|
145 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $b[ 0 ] ); |
|
143 | + $b = Strings::preg_split('|</internal-file>|i', $a[1]); |
|
144 | + $strippedContent = $a[0] . $b[1]; |
|
145 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
146 | 146 | $index++; |
147 | 147 | |
148 | - if ( isset( $a[ 2 ] ) ) { |
|
149 | - $c = Strings::preg_split( '|</internal-file[\s>]|i', $a[ 2 ] ); |
|
150 | - $strippedContent .= $c[ 1 ]; |
|
151 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $c[ 0 ] ); |
|
148 | + if (isset($a[2])) { |
|
149 | + $c = Strings::preg_split('|</internal-file[\s>]|i', $a[2]); |
|
150 | + $strippedContent .= $c[1]; |
|
151 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | return $strippedContent; |
@@ -159,10 +159,10 @@ discard block |
||
159 | 159 | * |
160 | 160 | * @return array |
161 | 161 | */ |
162 | - private static function extractBase64( $base64 ) { |
|
162 | + private static function extractBase64($base64) { |
|
163 | 163 | return [ |
164 | 164 | 'form-type' => 'base64', |
165 | - 'base64' => trim( str_replace( 'form="base64">', '', $base64 ) ), |
|
165 | + 'base64' => trim(str_replace('form="base64">', '', $base64)), |
|
166 | 166 | ]; |
167 | 167 | } |
168 | 168 | |
@@ -182,9 +182,9 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return string |
184 | 184 | */ |
185 | - private static function escapeDataInOriginalMap( $xliffContent ) { |
|
186 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceSpace' ], $xliffContent ); |
|
187 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceXliffTags' ], $xliffContent ); |
|
185 | + private static function escapeDataInOriginalMap($xliffContent) { |
|
186 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
187 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
188 | 188 | |
189 | 189 | return $xliffContent; |
190 | 190 | } |
@@ -203,15 +203,15 @@ discard block |
||
203 | 203 | * |
204 | 204 | * @return string |
205 | 205 | */ |
206 | - private static function insertPlaceholderInEmptyTags( $xliffContent ) { |
|
207 | - preg_match_all( '|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches ); |
|
206 | + private static function insertPlaceholderInEmptyTags($xliffContent) { |
|
207 | + preg_match_all('|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches); |
|
208 | 208 | |
209 | - if ( !empty( $emptyTagMatches[ 0 ] ) ) { |
|
210 | - foreach ( $emptyTagMatches[ 0 ] as $index => $emptyTagMatch ) { |
|
211 | - $matchedTag = $emptyTagMatches[ 1 ][ $index ]; |
|
209 | + if (!empty($emptyTagMatches[0])) { |
|
210 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
211 | + $matchedTag = $emptyTagMatches[1][$index]; |
|
212 | 212 | $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
213 | - $replacedTag = str_replace( '</' . $matchedTag . '>', $subst, $emptyTagMatch ); |
|
214 | - $xliffContent = str_replace( $emptyTagMatch, $replacedTag, $xliffContent ); |
|
213 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
214 | + $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
@@ -225,12 +225,12 @@ discard block |
||
225 | 225 | * |
226 | 226 | * @return string |
227 | 227 | */ |
228 | - private static function replaceSpace( $matches ) { |
|
229 | - $content = str_replace( ' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[ 2 ] ); |
|
230 | - $content = str_replace( '\n', Placeholder::NEW_LINE_PLACEHOLDER, $content ); |
|
231 | - $content = str_replace( '\t', Placeholder::TAB_PLACEHOLDER, $content ); |
|
228 | + private static function replaceSpace($matches) { |
|
229 | + $content = str_replace(' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[2]); |
|
230 | + $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
|
231 | + $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
|
232 | 232 | |
233 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
233 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -238,15 +238,15 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @return string |
240 | 240 | */ |
241 | - private static function replaceXliffTags( $matches ) { |
|
241 | + private static function replaceXliffTags($matches) { |
|
242 | 242 | $xliffTags = XliffTags::$tags; |
243 | - $content = $matches[ 2 ]; |
|
243 | + $content = $matches[2]; |
|
244 | 244 | |
245 | - foreach ( $xliffTags as $xliffTag ) { |
|
246 | - $content = preg_replace( '|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
247 | - $content = preg_replace( '|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
245 | + foreach ($xliffTags as $xliffTag) { |
|
246 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
247 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
248 | 248 | } |
249 | 249 | |
250 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
250 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
251 | 251 | } |
252 | 252 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * @param string|null $xliffProprietary |
37 | 37 | * @param LoggerInterface|null $logger |
38 | 38 | */ |
39 | - public function __construct( $xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null ) { |
|
39 | + public function __construct($xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null) { |
|
40 | 40 | $this->xliffVersion = $xliffVersion; |
41 | 41 | $this->logger = $logger; |
42 | 42 | $this->xliffProprietary = $xliffProprietary; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @return string |
47 | 47 | */ |
48 | 48 | protected function getTuTagName() { |
49 | - return ( $this->xliffVersion === 1 ) ? 'trans-unit' : 'unit'; |
|
49 | + return ($this->xliffVersion === 1) ? 'trans-unit' : 'unit'; |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | * |
55 | 55 | * @return array |
56 | 56 | */ |
57 | - abstract public function parse( DOMDocument $dom, $output = [] ); |
|
57 | + abstract public function parse(DOMDocument $dom, $output = []); |
|
58 | 58 | |
59 | 59 | /** |
60 | 60 | * Extract trans-unit content from the current node |
@@ -68,17 +68,17 @@ discard block |
||
68 | 68 | * @param array $contextGroups |
69 | 69 | * @param int $recursionLevel |
70 | 70 | */ |
71 | - protected function extractTuFromNode( DOMNode $childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j, $contextGroups = [], $recursionLevel = 0 ) { |
|
71 | + protected function extractTuFromNode(DOMNode $childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j, $contextGroups = [], $recursionLevel = 0) { |
|
72 | 72 | |
73 | - if ( $childNode->nodeType != XML_ELEMENT_NODE ) { |
|
73 | + if ($childNode->nodeType != XML_ELEMENT_NODE) { |
|
74 | 74 | return; |
75 | 75 | } |
76 | 76 | |
77 | - if ( $childNode->nodeName === 'group' ) { |
|
77 | + if ($childNode->nodeName === 'group') { |
|
78 | 78 | |
79 | 79 | // add nested context-groups |
80 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
81 | - if ( $nestedChildNode->nodeName === 'context-group' ) { |
|
80 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
81 | + if ($nestedChildNode->nodeName === 'context-group') { |
|
82 | 82 | $contextGroups[] = $nestedChildNode; |
83 | 83 | } |
84 | 84 | } |
@@ -86,21 +86,21 @@ discard block |
||
86 | 86 | // avoid infinite recursion |
87 | 87 | $recursionLevel++; |
88 | 88 | |
89 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
89 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
90 | 90 | |
91 | 91 | // nested groups |
92 | - if ( $nestedChildNode->nodeName === 'group' ) { |
|
92 | + if ($nestedChildNode->nodeName === 'group') { |
|
93 | 93 | |
94 | - if ( $recursionLevel < self::MAX_GROUP_RECURSION_LEVEL ) { |
|
95 | - $this->extractTuFromNode( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel ); |
|
94 | + if ($recursionLevel < self::MAX_GROUP_RECURSION_LEVEL) { |
|
95 | + $this->extractTuFromNode($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel); |
|
96 | 96 | } |
97 | 97 | |
98 | - } elseif ( $nestedChildNode->nodeName === $this->getTuTagName() ) { |
|
99 | - $this->extractTransUnit( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
98 | + } elseif ($nestedChildNode->nodeName === $this->getTuTagName()) { |
|
99 | + $this->extractTransUnit($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
100 | 100 | } |
101 | 101 | } |
102 | - } elseif ( $childNode->nodeName === $this->getTuTagName() ) { |
|
103 | - $this->extractTransUnit( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
102 | + } elseif ($childNode->nodeName === $this->getTuTagName()) { |
|
103 | + $this->extractTransUnit($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 | |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return mixed |
119 | 119 | */ |
120 | - abstract protected function extractTransUnit( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = [] ); |
|
120 | + abstract protected function extractTransUnit(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = []); |
|
121 | 121 | |
122 | 122 | /** |
123 | 123 | * @param DOMDocument $dom |
@@ -125,10 +125,10 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return array |
127 | 127 | */ |
128 | - protected function extractContent( DOMDocument $dom, DOMNode $node ) { |
|
128 | + protected function extractContent(DOMDocument $dom, DOMNode $node) { |
|
129 | 129 | return [ |
130 | - 'raw-content' => $this->extractTagContent( $dom, $node ), |
|
131 | - 'attr' => $this->extractTagAttributes( $node ) |
|
130 | + 'raw-content' => $this->extractTagContent($dom, $node), |
|
131 | + 'attr' => $this->extractTagAttributes($node) |
|
132 | 132 | ]; |
133 | 133 | } |
134 | 134 | |
@@ -145,12 +145,12 @@ discard block |
||
145 | 145 | * |
146 | 146 | * @return array |
147 | 147 | */ |
148 | - protected function extractTagAttributes( DOMNode $element ) { |
|
148 | + protected function extractTagAttributes(DOMNode $element) { |
|
149 | 149 | $tagAttributes = []; |
150 | 150 | |
151 | - if ( $element->hasAttributes() ) { |
|
152 | - foreach ( $element->attributes as $attr ) { |
|
153 | - $tagAttributes[ $attr->nodeName ] = $attr->nodeValue; |
|
151 | + if ($element->hasAttributes()) { |
|
152 | + foreach ($element->attributes as $attr) { |
|
153 | + $tagAttributes[$attr->nodeName] = $attr->nodeValue; |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
@@ -165,17 +165,17 @@ discard block |
||
165 | 165 | * |
166 | 166 | * @return string |
167 | 167 | */ |
168 | - protected function extractTagContent( DOMDocument $dom, DOMNode $element ) { |
|
168 | + protected function extractTagContent(DOMDocument $dom, DOMNode $element) { |
|
169 | 169 | $childNodes = $element->hasChildNodes(); |
170 | 170 | $extractedContent = ''; |
171 | 171 | |
172 | - if ( !empty( $childNodes ) ) { |
|
173 | - foreach ( $element->childNodes as $node ) { |
|
174 | - $extractedContent .= Emoji::toEntity( Strings::fixNonWellFormedXml( $dom->saveXML( $node ) ) ); |
|
172 | + if (!empty($childNodes)) { |
|
173 | + foreach ($element->childNodes as $node) { |
|
174 | + $extractedContent .= Emoji::toEntity(Strings::fixNonWellFormedXml($dom->saveXML($node))); |
|
175 | 175 | } |
176 | 176 | } |
177 | 177 | |
178 | - return str_replace( Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent ); |
|
178 | + return str_replace(Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
@@ -186,45 +186,45 @@ discard block |
||
186 | 186 | * |
187 | 187 | * @return array |
188 | 188 | */ |
189 | - protected function extractContentWithMarksAndExtTags( DOMDocument $dom, DOMElement $childNode ) { |
|
189 | + protected function extractContentWithMarksAndExtTags(DOMDocument $dom, DOMElement $childNode) { |
|
190 | 190 | $source = []; |
191 | 191 | |
192 | 192 | // example: |
193 | 193 | // <g id="1"><mrk mid="0" mtype="seg">An English string with g tags</mrk></g> |
194 | - $raw = $this->extractTagContent( $dom, $childNode ); |
|
194 | + $raw = $this->extractTagContent($dom, $childNode); |
|
195 | 195 | |
196 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
196 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
197 | 197 | |
198 | 198 | $mi = 0; |
199 | - while ( isset( $markers[ $mi + 1 ] ) ) { |
|
200 | - unset( $mid ); |
|
199 | + while (isset($markers[$mi + 1])) { |
|
200 | + unset($mid); |
|
201 | 201 | |
202 | - preg_match( '|mid\s?=\s?["\'](.*?)["\']|si', $markers[ $mi + 1 ], $mid ); |
|
202 | + preg_match('|mid\s?=\s?["\'](.*?)["\']|si', $markers[$mi + 1], $mid); |
|
203 | 203 | |
204 | 204 | // if it's a Trados file the trailing spaces after </mrk> are meaningful |
205 | 205 | // so we add them to |
206 | 206 | $trailingSpaces = ''; |
207 | - if ( $this->xliffProprietary === 'trados' ) { |
|
208 | - preg_match_all( '/<\/mrk>[\s]+/iu', $markers[ $mi + 1 ], $trailingSpacesMatches ); |
|
207 | + if ($this->xliffProprietary === 'trados') { |
|
208 | + preg_match_all('/<\/mrk>[\s]+/iu', $markers[$mi + 1], $trailingSpacesMatches); |
|
209 | 209 | |
210 | - if ( isset( $trailingSpacesMatches[ 0 ] ) && count( $trailingSpacesMatches[ 0 ] ) > 0 ) { |
|
211 | - foreach ( $trailingSpacesMatches[ 0 ] as $match ) { |
|
212 | - $trailingSpaces = str_replace( '</mrk>', '', $match ); |
|
210 | + if (isset($trailingSpacesMatches[0]) && count($trailingSpacesMatches[0]) > 0) { |
|
211 | + foreach ($trailingSpacesMatches[0] as $match) { |
|
212 | + $trailingSpaces = str_replace('</mrk>', '', $match); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | 217 | //re-build the mrk tag after the split |
218 | - $originalMark = trim( '<mrk ' . $markers[ $mi + 1 ] ); |
|
218 | + $originalMark = trim('<mrk ' . $markers[$mi + 1]); |
|
219 | 219 | |
220 | - $mark_string = preg_replace( '#^<mrk\s[^>]+>(.*)#', '$1', $originalMark ); // at this point we have: ---> 'Test </mrk> </g>>' |
|
221 | - $mark_content = preg_split( '#</mrk>#si', $mark_string ); |
|
220 | + $mark_string = preg_replace('#^<mrk\s[^>]+>(.*)#', '$1', $originalMark); // at this point we have: ---> 'Test </mrk> </g>>' |
|
221 | + $mark_content = preg_split('#</mrk>#si', $mark_string); |
|
222 | 222 | |
223 | 223 | $sourceArray = [ |
224 | - 'mid' => ( isset( $mid[ 1 ] ) ) ? $mid[ 1 ] : $mi, |
|
225 | - 'ext-prec-tags' => ( $mi == 0 ? $markers[ 0 ] : "" ), |
|
226 | - 'raw-content' => ( isset( $mark_content[ 0 ] ) ) ? $mark_content[ 0 ] . $trailingSpaces : '', |
|
227 | - 'ext-succ-tags' => ( isset( $mark_content[ 1 ] ) ) ? $mark_content[ 1 ] : '', |
|
224 | + 'mid' => (isset($mid[1])) ? $mid[1] : $mi, |
|
225 | + 'ext-prec-tags' => ($mi == 0 ? $markers[0] : ""), |
|
226 | + 'raw-content' => (isset($mark_content[0])) ? $mark_content[0] . $trailingSpaces : '', |
|
227 | + 'ext-succ-tags' => (isset($mark_content[1])) ? $mark_content[1] : '', |
|
228 | 228 | ]; |
229 | 229 | |
230 | 230 | $source[] = $sourceArray; |
@@ -240,12 +240,12 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @return array |
242 | 242 | */ |
243 | - protected function getDataRefMap( $originalData ) { |
|
243 | + protected function getDataRefMap($originalData) { |
|
244 | 244 | // dataRef map |
245 | 245 | $dataRefMap = []; |
246 | - foreach ( $originalData as $datum ) { |
|
247 | - if ( isset( $datum[ 'attr' ][ 'id' ] ) ) { |
|
248 | - $dataRefMap[ $datum[ 'attr' ][ 'id' ] ] = $datum[ 'raw-content' ]; |
|
246 | + foreach ($originalData as $datum) { |
|
247 | + if (isset($datum['attr']['id'])) { |
|
248 | + $dataRefMap[$datum['attr']['id']] = $datum['raw-content']; |
|
249 | 249 | } |
250 | 250 | } |
251 | 251 | |
@@ -257,10 +257,10 @@ discard block |
||
257 | 257 | * |
258 | 258 | * @return bool |
259 | 259 | */ |
260 | - protected function stringContainsMarks( $raw ) { |
|
261 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
260 | + protected function stringContainsMarks($raw) { |
|
261 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
262 | 262 | |
263 | - return isset( $markers[ 1 ] ); |
|
263 | + return isset($markers[1]); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | * @return array |
271 | 271 | * @throws \Exception |
272 | 272 | */ |
273 | - protected function JSONOrRawContentArray( $noteValue, $escapeStrings = true ) { |
|
273 | + protected function JSONOrRawContentArray($noteValue, $escapeStrings = true) { |
|
274 | 274 | // |
275 | 275 | // convert double escaped entites |
276 | 276 | // |
@@ -280,17 +280,17 @@ discard block |
||
280 | 280 | // &amp; ---> & |
281 | 281 | // &apos ---> ' |
282 | 282 | // |
283 | - if ( Strings::isADoubleEscapedEntity( $noteValue ) ) { |
|
284 | - $noteValue = Strings::htmlspecialchars_decode( $noteValue, true ); |
|
283 | + if (Strings::isADoubleEscapedEntity($noteValue)) { |
|
284 | + $noteValue = Strings::htmlspecialchars_decode($noteValue, true); |
|
285 | 285 | } else { |
286 | 286 | // for non escaped entities $escapeStrings is always true for security reasons |
287 | 287 | $escapeStrings = true; |
288 | 288 | } |
289 | 289 | |
290 | - if ( Strings::isJSON( $noteValue ) ) { |
|
291 | - return [ 'json' => Strings::cleanCDATA( $noteValue ) ]; |
|
290 | + if (Strings::isJSON($noteValue)) { |
|
291 | + return ['json' => Strings::cleanCDATA($noteValue)]; |
|
292 | 292 | } |
293 | 293 | |
294 | - return [ 'raw-content' => Strings::fixNonWellFormedXml( $noteValue, $escapeStrings ) ]; |
|
294 | + return ['raw-content' => Strings::fixNonWellFormedXml($noteValue, $escapeStrings)]; |
|
295 | 295 | } |
296 | 296 | } |
@@ -16,33 +16,33 @@ discard block |
||
16 | 16 | * @inheritDoc |
17 | 17 | * @throws Exception |
18 | 18 | */ |
19 | - public function parse( DOMDocument $dom, $output = [] ) { |
|
19 | + public function parse(DOMDocument $dom, $output = []) { |
|
20 | 20 | $i = 1; |
21 | 21 | /** @var DOMElement $file */ |
22 | - foreach ( $dom->getElementsByTagName( 'file' ) as $file ) { |
|
22 | + foreach ($dom->getElementsByTagName('file') as $file) { |
|
23 | 23 | |
24 | 24 | // metadata |
25 | - $output[ 'files' ][ $i ][ 'attr' ] = $this->extractMetadata( $file ); |
|
25 | + $output['files'][$i]['attr'] = $this->extractMetadata($file); |
|
26 | 26 | |
27 | 27 | // reference |
28 | - if ( !empty( $this->extractReference( $file ) ) ) { |
|
29 | - $output[ 'files' ][ $i ][ 'reference' ] = $this->extractReference( $file ); |
|
28 | + if (!empty($this->extractReference($file))) { |
|
29 | + $output['files'][$i]['reference'] = $this->extractReference($file); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | // trans-units |
33 | 33 | $transUnitIdArrayForUniquenessCheck = []; |
34 | 34 | $j = 1; |
35 | - foreach ( $file->childNodes as $body ) { |
|
36 | - if ( $body->nodeName === 'body' ) { |
|
37 | - foreach ( $body->childNodes as $childNode ) { |
|
38 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
35 | + foreach ($file->childNodes as $body) { |
|
36 | + if ($body->nodeName === 'body') { |
|
37 | + foreach ($body->childNodes as $childNode) { |
|
38 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | // trans-unit re-count check |
42 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
43 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
44 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
45 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
42 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
43 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
44 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
45 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | $i++; |
@@ -58,50 +58,50 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return array |
60 | 60 | */ |
61 | - private function extractMetadata( DOMElement $file ) { |
|
61 | + private function extractMetadata(DOMElement $file) { |
|
62 | 62 | $metadata = []; |
63 | 63 | $customAttr = []; |
64 | 64 | |
65 | 65 | /** @var DOMAttr $attribute */ |
66 | - foreach ( $file->attributes as $attribute ) { |
|
67 | - switch ( $attribute->localName ) { |
|
66 | + foreach ($file->attributes as $attribute) { |
|
67 | + switch ($attribute->localName) { |
|
68 | 68 | // original |
69 | 69 | case 'original': |
70 | - $metadata[ 'original' ] = $attribute->value; |
|
70 | + $metadata['original'] = $attribute->value; |
|
71 | 71 | break; |
72 | 72 | |
73 | 73 | // source-language |
74 | 74 | case 'source-language': |
75 | - $metadata[ 'source-language' ] = $attribute->value; |
|
75 | + $metadata['source-language'] = $attribute->value; |
|
76 | 76 | break; |
77 | 77 | |
78 | 78 | // data-type |
79 | 79 | case 'datatype': |
80 | - $metadata[ 'data-type' ] = $attribute->value; |
|
80 | + $metadata['data-type'] = $attribute->value; |
|
81 | 81 | break; |
82 | 82 | |
83 | 83 | // target-language |
84 | 84 | case 'target-language': |
85 | - $metadata[ 'target-language' ] = $attribute->value; |
|
85 | + $metadata['target-language'] = $attribute->value; |
|
86 | 86 | break; |
87 | 87 | } |
88 | 88 | |
89 | 89 | // Custom MateCat x-Attribute |
90 | - preg_match( '|x-(.*?)|si', $attribute->localName, $temp ); |
|
91 | - if ( isset( $temp[ 1 ] ) ) { |
|
92 | - $customAttr[ $attribute->localName ] = $attribute->value; |
|
90 | + preg_match('|x-(.*?)|si', $attribute->localName, $temp); |
|
91 | + if (isset($temp[1])) { |
|
92 | + $customAttr[$attribute->localName] = $attribute->value; |
|
93 | 93 | } |
94 | - unset( $temp ); |
|
94 | + unset($temp); |
|
95 | 95 | |
96 | 96 | // Custom MateCat namespace Attribute mtc: |
97 | - preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp ); |
|
98 | - if ( isset( $temp[ 1 ] ) ) { |
|
99 | - $customAttr[ $attribute->nodeName ] = $attribute->value; |
|
97 | + preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp); |
|
98 | + if (isset($temp[1])) { |
|
99 | + $customAttr[$attribute->nodeName] = $attribute->value; |
|
100 | 100 | } |
101 | - unset( $temp ); |
|
101 | + unset($temp); |
|
102 | 102 | |
103 | - if ( !empty( $customAttr ) ) { |
|
104 | - $metadata[ 'custom' ] = $customAttr; |
|
103 | + if (!empty($customAttr)) { |
|
104 | + $metadata['custom'] = $customAttr; |
|
105 | 105 | } |
106 | 106 | } |
107 | 107 | |
@@ -113,16 +113,16 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @return array |
115 | 115 | */ |
116 | - private function extractReference( DOMElement $file ) { |
|
116 | + private function extractReference(DOMElement $file) { |
|
117 | 117 | $reference = []; |
118 | 118 | |
119 | 119 | $order = 0; |
120 | - foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) { |
|
120 | + foreach ($file->getElementsByTagName('reference') as $ref) { |
|
121 | 121 | /** @var DOMNode $childNode */ |
122 | - foreach ( $ref->childNodes as $childNode ) { |
|
123 | - if ( $childNode->nodeName === 'internal-file' ) { |
|
124 | - $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue; |
|
125 | - $reference[ $order ][ 'base64' ] = trim( $childNode->nodeValue ); |
|
122 | + foreach ($ref->childNodes as $childNode) { |
|
123 | + if ($childNode->nodeName === 'internal-file') { |
|
124 | + $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue; |
|
125 | + $reference[$order]['base64'] = trim($childNode->nodeValue); |
|
126 | 126 | $order++; |
127 | 127 | } |
128 | 128 | } |
@@ -144,60 +144,60 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @throws Exception |
146 | 146 | */ |
147 | - protected function extractTransUnit( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = [] ) { |
|
147 | + protected function extractTransUnit(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = []) { |
|
148 | 148 | // metadata |
149 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
149 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
150 | 150 | |
151 | 151 | // notes |
152 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit ); |
|
152 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit); |
|
153 | 153 | |
154 | 154 | // content |
155 | 155 | /** @var DOMElement $childNode */ |
156 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
156 | + foreach ($transUnit->childNodes as $childNode) { |
|
157 | 157 | // source |
158 | - if ( $childNode->nodeName === 'source' ) { |
|
159 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode ); |
|
158 | + if ($childNode->nodeName === 'source') { |
|
159 | + $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | // seg-source |
163 | - if ( $childNode->nodeName === 'seg-source' ) { |
|
164 | - $rawSegment = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ]; |
|
165 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment ); |
|
163 | + if ($childNode->nodeName === 'seg-source') { |
|
164 | + $rawSegment = $output['files'][$i]['trans-units'][$j]['source']['raw-content']; |
|
165 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | // target |
169 | - if ( $childNode->nodeName === 'target' ) { |
|
170 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode ); |
|
169 | + if ($childNode->nodeName === 'target') { |
|
170 | + $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode); |
|
171 | 171 | |
172 | 172 | // seg-target |
173 | - $targetRawContent = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ]; |
|
174 | - $segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ]; |
|
175 | - if ( isset( $targetRawContent ) && !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) { |
|
176 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
177 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ]['attr'] = $this->extractTagAttributes($childNode); |
|
173 | + $targetRawContent = @$output['files'][$i]['trans-units'][$j]['target']['raw-content']; |
|
174 | + $segSource = @$output['files'][$i]['trans-units'][$j]['seg-source']; |
|
175 | + if (isset($targetRawContent) && !empty($targetRawContent) && isset($segSource) && count($segSource) > 0) { |
|
176 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
177 | + $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode); |
|
178 | 178 | } |
179 | 179 | } |
180 | 180 | |
181 | 181 | // locked |
182 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
183 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
182 | + if ($childNode->nodeName === 'sdl:seg') { |
|
183 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
184 | 184 | } |
185 | 185 | } |
186 | 186 | |
187 | 187 | // context-group |
188 | - if(!empty($contextGroups)){ |
|
189 | - foreach ($contextGroups as $contextGroup){ |
|
190 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
188 | + if (!empty($contextGroups)) { |
|
189 | + foreach ($contextGroups as $contextGroup) { |
|
190 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
195 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
194 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
195 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | // alt-trans |
199 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
200 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
199 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
200 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | $j++; |
@@ -210,41 +210,41 @@ discard block |
||
210 | 210 | * @return array |
211 | 211 | * @throws Exception |
212 | 212 | */ |
213 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ) { |
|
213 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck) { |
|
214 | 214 | $metadata = []; |
215 | 215 | |
216 | 216 | // id MUST NOT be null |
217 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
218 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
217 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
218 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
222 | 222 | * @var DOMAttr $element |
223 | 223 | */ |
224 | - foreach ( $transUnit->attributes as $element ) { |
|
224 | + foreach ($transUnit->attributes as $element) { |
|
225 | 225 | |
226 | - if ( $element->nodeName === "id" ) { |
|
226 | + if ($element->nodeName === "id") { |
|
227 | 227 | |
228 | 228 | $id = $element->nodeValue; |
229 | 229 | |
230 | - if ( strlen( $id ) > 100 ) { |
|
231 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
230 | + if (strlen($id) > 100) { |
|
231 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | $transUnitIdArrayForUniquenessCheck[] = $id; |
235 | - $metadata[ 'id' ] = $id; |
|
235 | + $metadata['id'] = $id; |
|
236 | 236 | |
237 | - } elseif ( $element->nodeName === "approved" ) { |
|
237 | + } elseif ($element->nodeName === "approved") { |
|
238 | 238 | // approved as BOOLEAN |
239 | 239 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
240 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
241 | - } elseif ( $element->nodeName === "maxwidth" ) { |
|
240 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
241 | + } elseif ($element->nodeName === "maxwidth") { |
|
242 | 242 | // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character |
243 | 243 | // we duplicate the info to allow Xliff V1 and V2 to work the same |
244 | - $metadata[ 'sizeRestriction' ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
245 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
244 | + $metadata['sizeRestriction'] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
245 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
246 | 246 | } else { |
247 | - $metadata[ $element->nodeName ] = $element->nodeValue; |
|
247 | + $metadata[$element->nodeName] = $element->nodeValue; |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | } |
@@ -258,19 +258,19 @@ discard block |
||
258 | 258 | * @return array |
259 | 259 | * @throws Exception |
260 | 260 | */ |
261 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ) { |
|
261 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit) { |
|
262 | 262 | $notes = []; |
263 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
263 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
264 | 264 | |
265 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
265 | + $noteValue = $this->extractTagContent($dom, $note); |
|
266 | 266 | |
267 | - if ( '' !== $noteValue ) { |
|
267 | + if ('' !== $noteValue) { |
|
268 | 268 | |
269 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
269 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
270 | 270 | |
271 | 271 | // extract all the attributes |
272 | - foreach ( $note->attributes as $attribute ) { |
|
273 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
272 | + foreach ($note->attributes as $attribute) { |
|
273 | + $extractedNote[$attribute->name] = $attribute->value; |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | $notes[] = $extractedNote; |
@@ -285,14 +285,14 @@ discard block |
||
285 | 285 | * |
286 | 286 | * @return array |
287 | 287 | */ |
288 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ) { |
|
288 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup) { |
|
289 | 289 | $cg = []; |
290 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
290 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
291 | 291 | |
292 | 292 | /** @var DOMNode $context */ |
293 | - foreach ( $contextGroup->childNodes as $context ) { |
|
294 | - if ( $context->nodeName === 'context' ) { |
|
295 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
293 | + foreach ($contextGroup->childNodes as $context) { |
|
294 | + if ($context->nodeName === 'context') { |
|
295 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
296 | 296 | } |
297 | 297 | } |
298 | 298 | |
@@ -304,16 +304,16 @@ discard block |
||
304 | 304 | * |
305 | 305 | * @return array |
306 | 306 | */ |
307 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
307 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
308 | 308 | $at = []; |
309 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
309 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
310 | 310 | |
311 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
312 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
311 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
312 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
313 | 313 | } |
314 | 314 | |
315 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
316 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
315 | + if ($altTrans->getElementsByTagName('target')) { |
|
316 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
317 | 317 | } |
318 | 318 | |
319 | 319 | return $at; |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | * |
325 | 325 | * @return bool |
326 | 326 | */ |
327 | - private function extractLocked( DOMElement $locked ) { |
|
328 | - return null !== $locked->getAttribute( 'locked' ); |
|
327 | + private function extractLocked(DOMElement $locked) { |
|
328 | + return null !== $locked->getAttribute('locked'); |
|
329 | 329 | } |
330 | 330 | } |
@@ -15,30 +15,30 @@ discard block |
||
15 | 15 | * @inheritDoc |
16 | 16 | * @throws \Exception |
17 | 17 | */ |
18 | - public function parse( DOMDocument $dom, $output = [] ) { |
|
18 | + public function parse(DOMDocument $dom, $output = []) { |
|
19 | 19 | $i = 1; |
20 | 20 | /** @var DOMElement $file */ |
21 | - foreach ( $dom->getElementsByTagName( 'file' ) as $file ) { |
|
21 | + foreach ($dom->getElementsByTagName('file') as $file) { |
|
22 | 22 | |
23 | 23 | // metadata |
24 | - $output[ 'files' ][ $i ][ 'attr' ] = $this->extractMetadata( $dom ); |
|
24 | + $output['files'][$i]['attr'] = $this->extractMetadata($dom); |
|
25 | 25 | |
26 | 26 | // notes |
27 | - $output[ 'files' ][ $i ][ 'notes' ] = $this->extractNotes( $file ); |
|
27 | + $output['files'][$i]['notes'] = $this->extractNotes($file); |
|
28 | 28 | |
29 | 29 | // trans-units |
30 | 30 | $transUnitIdArrayForUniquenessCheck = []; |
31 | 31 | $j = 1; |
32 | 32 | /** @var DOMElement $transUnit */ |
33 | - foreach ( $file->childNodes as $childNode ) { |
|
34 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
33 | + foreach ($file->childNodes as $childNode) { |
|
34 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | // trans-unit re-count check |
38 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
39 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
40 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
41 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
38 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
39 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
40 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
41 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | $i++; |
@@ -52,23 +52,23 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return array |
54 | 54 | */ |
55 | - private function extractMetadata( DOMDocument $dom ) { |
|
55 | + private function extractMetadata(DOMDocument $dom) { |
|
56 | 56 | $metadata = []; |
57 | 57 | |
58 | - $xliffNode = $dom->getElementsByTagName( 'xliff' )->item( 0 ); |
|
59 | - $fileNode = $dom->getElementsByTagName( 'file' )->item( 0 ); |
|
58 | + $xliffNode = $dom->getElementsByTagName('xliff')->item(0); |
|
59 | + $fileNode = $dom->getElementsByTagName('file')->item(0); |
|
60 | 60 | |
61 | 61 | // original |
62 | - $metadata[ 'original' ] = ( null !== $fileNode->attributes->getNamedItem( 'original' ) ) ? $fileNode->attributes->getNamedItem( 'original' )->nodeValue : 'no-name'; |
|
62 | + $metadata['original'] = (null !== $fileNode->attributes->getNamedItem('original')) ? $fileNode->attributes->getNamedItem('original')->nodeValue : 'no-name'; |
|
63 | 63 | |
64 | 64 | // source-language |
65 | - $metadata[ 'source-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'srcLang' ) ) ? $xliffNode->attributes->getNamedItem( 'srcLang' )->nodeValue : 'en-US'; |
|
65 | + $metadata['source-language'] = (null !== $xliffNode->attributes->getNamedItem('srcLang')) ? $xliffNode->attributes->getNamedItem('srcLang')->nodeValue : 'en-US'; |
|
66 | 66 | |
67 | 67 | // datatype |
68 | 68 | // @TODO to be implemented |
69 | 69 | |
70 | 70 | // target-language |
71 | - $metadata[ 'target-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'trgLang' ) ) ? $xliffNode->attributes->getNamedItem( 'trgLang' )->nodeValue : 'en-US'; |
|
71 | + $metadata['target-language'] = (null !== $xliffNode->attributes->getNamedItem('trgLang')) ? $xliffNode->attributes->getNamedItem('trgLang')->nodeValue : 'en-US'; |
|
72 | 72 | |
73 | 73 | // custom MateCat x-attribute |
74 | 74 | // @TODO to be implemented |
@@ -82,16 +82,16 @@ discard block |
||
82 | 82 | * @return array |
83 | 83 | * @throws \Exception |
84 | 84 | */ |
85 | - private function extractNotes( DOMElement $file ) { |
|
85 | + private function extractNotes(DOMElement $file) { |
|
86 | 86 | $notes = []; |
87 | 87 | |
88 | 88 | // loop <notes> to get nested <note> tag |
89 | - foreach ( $file->childNodes as $childNode ) { |
|
90 | - if ( $childNode->nodeName === 'notes' ) { |
|
91 | - foreach ( $childNode->childNodes as $note ) { |
|
92 | - $noteValue = trim( $note->nodeValue ); |
|
93 | - if ( '' !== $noteValue ) { |
|
94 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
89 | + foreach ($file->childNodes as $childNode) { |
|
90 | + if ($childNode->nodeName === 'notes') { |
|
91 | + foreach ($childNode->childNodes as $note) { |
|
92 | + $noteValue = trim($note->nodeValue); |
|
93 | + if ('' !== $noteValue) { |
|
94 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 | } |
@@ -113,32 +113,32 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @throws \Exception |
115 | 115 | */ |
116 | - protected function extractTransUnit( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = [] ) { |
|
116 | + protected function extractTransUnit(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j, $contextGroups = []) { |
|
117 | 117 | // metadata |
118 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
118 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
119 | 119 | |
120 | 120 | // notes |
121 | 121 | // merge <notes> with key and key-note contained in metadata <mda:metaGroup> |
122 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $transUnit ); |
|
122 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($transUnit); |
|
123 | 123 | |
124 | 124 | // uuid |
125 | - foreach ( $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] as $note ) { |
|
126 | - if ( isset( $note[ 'raw-content' ] ) && Strings::isAValidUuid( $note[ 'raw-content' ] ) ) { |
|
127 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ][ 'uuid' ] = $note[ 'raw-content' ]; |
|
125 | + foreach ($output['files'][$i]['trans-units'][$j]['notes'] as $note) { |
|
126 | + if (isset($note['raw-content']) && Strings::isAValidUuid($note['raw-content'])) { |
|
127 | + $output['files'][$i]['trans-units'][$j]['attr']['uuid'] = $note['raw-content']; |
|
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | 131 | // original-data (exclusive for V2) |
132 | 132 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html#originaldata |
133 | - $originalData = $this->extractTransUnitOriginalData( $transUnit ); |
|
134 | - if ( !empty( $originalData ) ) { |
|
135 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'original-data' ] = $originalData; |
|
133 | + $originalData = $this->extractTransUnitOriginalData($transUnit); |
|
134 | + if (!empty($originalData)) { |
|
135 | + $output['files'][$i]['trans-units'][$j]['original-data'] = $originalData; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | // additionalTagData (exclusive for V2) |
139 | - $additionalTagData = $this->extractTransUnitAdditionalTagData( $transUnit ); |
|
140 | - if ( !empty( $additionalTagData ) ) { |
|
141 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'additional-tag-data' ] = $additionalTagData; |
|
139 | + $additionalTagData = $this->extractTransUnitAdditionalTagData($transUnit); |
|
140 | + if (!empty($additionalTagData)) { |
|
141 | + $output['files'][$i]['trans-units'][$j]['additional-tag-data'] = $additionalTagData; |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | // content |
@@ -158,54 +158,54 @@ discard block |
||
158 | 158 | |
159 | 159 | /** @var DOMElement $segment */ |
160 | 160 | $c = 0; |
161 | - foreach ( $transUnit->childNodes as $segment ) { |
|
162 | - if ( $segment->nodeName === 'segment' ) { |
|
161 | + foreach ($transUnit->childNodes as $segment) { |
|
162 | + if ($segment->nodeName === 'segment') { |
|
163 | 163 | |
164 | 164 | // check segment id consistency |
165 | - $attr = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ]; |
|
166 | - $this->checkSegmentIdConsistency( $segment, $attr ); |
|
165 | + $attr = $output['files'][$i]['trans-units'][$j]['attr']; |
|
166 | + $this->checkSegmentIdConsistency($segment, $attr); |
|
167 | 167 | |
168 | 168 | // loop <segment> to get nested <source> and <target> tag |
169 | - foreach ( $segment->childNodes as $childNode ) { |
|
170 | - if ( $childNode->nodeName === 'source' ) { |
|
171 | - $extractedSource = $this->extractContent( $dom, $childNode ); |
|
172 | - $source[ 'raw-content' ][ $c ] = $extractedSource[ 'raw-content' ]; |
|
169 | + foreach ($segment->childNodes as $childNode) { |
|
170 | + if ($childNode->nodeName === 'source') { |
|
171 | + $extractedSource = $this->extractContent($dom, $childNode); |
|
172 | + $source['raw-content'][$c] = $extractedSource['raw-content']; |
|
173 | 173 | |
174 | - if ( !empty( $extractedSource[ 'attr' ] ) ) { |
|
175 | - $source[ 'attr' ][ $c ] = $extractedSource[ 'attr' ]; |
|
174 | + if (!empty($extractedSource['attr'])) { |
|
175 | + $source['attr'][$c] = $extractedSource['attr']; |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | // append value to 'seg-source' |
179 | - if ( $this->stringContainsMarks( $extractedSource[ 'raw-content' ] ) ) { |
|
180 | - $segSource = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
179 | + if ($this->stringContainsMarks($extractedSource['raw-content'])) { |
|
180 | + $segSource = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
181 | 181 | } else { |
182 | 182 | $segSource[] = [ |
183 | - 'attr' => $this->extractTagAttributes( $segment), |
|
184 | - 'mid' => count( $segSource ) > 0 ? count( $segSource ) : 0, |
|
183 | + 'attr' => $this->extractTagAttributes($segment), |
|
184 | + 'mid' => count($segSource) > 0 ? count($segSource) : 0, |
|
185 | 185 | 'ext-prec-tags' => '', |
186 | - 'raw-content' => $extractedSource[ 'raw-content' ], |
|
186 | + 'raw-content' => $extractedSource['raw-content'], |
|
187 | 187 | 'ext-succ-tags' => '', |
188 | 188 | ]; |
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | - if ( $childNode->nodeName === 'target' ) { |
|
193 | - $extractedTarget = $this->extractContent( $dom, $childNode ); |
|
194 | - $target[ 'raw-content' ][ $c ] = $extractedTarget[ 'raw-content' ]; |
|
192 | + if ($childNode->nodeName === 'target') { |
|
193 | + $extractedTarget = $this->extractContent($dom, $childNode); |
|
194 | + $target['raw-content'][$c] = $extractedTarget['raw-content']; |
|
195 | 195 | |
196 | - if ( !empty( $extractedTarget[ 'attr' ] ) ) { |
|
197 | - $target[ 'attr' ][ $c ] = $extractedTarget[ 'attr' ]; |
|
196 | + if (!empty($extractedTarget['attr'])) { |
|
197 | + $target['attr'][$c] = $extractedTarget['attr']; |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | // append value to 'seg-target' |
201 | - if ( $this->stringContainsMarks( $extractedTarget[ 'raw-content' ] ) ) { |
|
202 | - $segTarget = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
201 | + if ($this->stringContainsMarks($extractedTarget['raw-content'])) { |
|
202 | + $segTarget = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
203 | 203 | } else { |
204 | 204 | $segTarget[] = [ |
205 | - 'attr' => $this->extractTagAttributes( $segment), |
|
206 | - 'mid' => count( $segTarget ) > 0 ? count( $segTarget ) : 0, |
|
205 | + 'attr' => $this->extractTagAttributes($segment), |
|
206 | + 'mid' => count($segTarget) > 0 ? count($segTarget) : 0, |
|
207 | 207 | 'ext-prec-tags' => '', |
208 | - 'raw-content' => $extractedTarget[ 'raw-content' ], |
|
208 | + 'raw-content' => $extractedTarget['raw-content'], |
|
209 | 209 | 'ext-succ-tags' => '', |
210 | 210 | ]; |
211 | 211 | } |
@@ -216,10 +216,10 @@ discard block |
||
216 | 216 | } |
217 | 217 | } |
218 | 218 | |
219 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $source; |
|
220 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $target; |
|
221 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $segSource; |
|
222 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $segTarget; |
|
219 | + $output['files'][$i]['trans-units'][$j]['source'] = $source; |
|
220 | + $output['files'][$i]['trans-units'][$j]['target'] = $target; |
|
221 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $segSource; |
|
222 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $segTarget; |
|
223 | 223 | |
224 | 224 | $j++; |
225 | 225 | } |
@@ -230,41 +230,41 @@ discard block |
||
230 | 230 | * |
231 | 231 | * @return array |
232 | 232 | */ |
233 | - private function extractTransUnitMetadata( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck ) { |
|
233 | + private function extractTransUnitMetadata(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck) { |
|
234 | 234 | $metadata = []; |
235 | 235 | |
236 | 236 | // id |
237 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
238 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
237 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
238 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
239 | 239 | } |
240 | 240 | |
241 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
241 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
242 | 242 | |
243 | - if ( strlen( $id ) > 100 ) { |
|
244 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
243 | + if (strlen($id) > 100) { |
|
244 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | $transUnitIdArrayForUniquenessCheck[] = $id; |
248 | - $metadata[ 'id' ] = $id; |
|
248 | + $metadata['id'] = $id; |
|
249 | 249 | |
250 | 250 | // translate |
251 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
252 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
251 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
252 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | // tGroupBegin |
256 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupBegin' ) ) { |
|
257 | - $metadata[ 'tGroupBegin' ] = $transUnit->attributes->getNamedItem( 'tGroupBegin' )->nodeValue; |
|
256 | + if (null !== $transUnit->attributes->getNamedItem('tGroupBegin')) { |
|
257 | + $metadata['tGroupBegin'] = $transUnit->attributes->getNamedItem('tGroupBegin')->nodeValue; |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | // tGroupEnd |
261 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupEnd' ) ) { |
|
262 | - $metadata[ 'tGroupEnd' ] = $transUnit->attributes->getNamedItem( 'tGroupEnd' )->nodeValue; |
|
261 | + if (null !== $transUnit->attributes->getNamedItem('tGroupEnd')) { |
|
262 | + $metadata['tGroupEnd'] = $transUnit->attributes->getNamedItem('tGroupEnd')->nodeValue; |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | // sizeRestriction |
266 | - if ( null !== $transUnit->attributes->getNamedItem( 'sizeRestriction' ) && '' !== $transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue ) { |
|
267 | - $metadata[ 'sizeRestriction' ] = (int)$transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue; |
|
266 | + if (null !== $transUnit->attributes->getNamedItem('sizeRestriction') && '' !== $transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue) { |
|
267 | + $metadata['sizeRestriction'] = (int)$transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | return $metadata; |
@@ -276,31 +276,31 @@ discard block |
||
276 | 276 | * @return array |
277 | 277 | * @throws \Exception |
278 | 278 | */ |
279 | - private function extractTransUnitOriginalData( DOMElement $transUnit ) { |
|
279 | + private function extractTransUnitOriginalData(DOMElement $transUnit) { |
|
280 | 280 | $originalData = []; |
281 | 281 | |
282 | 282 | // loop <originalData> to get nested content |
283 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
284 | - if ( $childNode->nodeName === 'originalData' ) { |
|
285 | - foreach ( $childNode->childNodes as $data ) { |
|
286 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
287 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
283 | + foreach ($transUnit->childNodes as $childNode) { |
|
284 | + if ($childNode->nodeName === 'originalData') { |
|
285 | + foreach ($childNode->childNodes as $data) { |
|
286 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
287 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
288 | 288 | |
289 | - $dataValue = str_replace( Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue ); |
|
290 | - $dataValue = str_replace( Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue ); |
|
291 | - $dataValue = str_replace( Placeholder::TAB_PLACEHOLDER, '\t', $dataValue ); |
|
289 | + $dataValue = str_replace(Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue); |
|
290 | + $dataValue = str_replace(Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue); |
|
291 | + $dataValue = str_replace(Placeholder::TAB_PLACEHOLDER, '\t', $dataValue); |
|
292 | 292 | |
293 | - if ( '' !== $dataValue ) { |
|
293 | + if ('' !== $dataValue) { |
|
294 | 294 | |
295 | - $jsonOrRawContentArray = $this->JSONOrRawContentArray( $dataValue, false ); |
|
295 | + $jsonOrRawContentArray = $this->JSONOrRawContentArray($dataValue, false); |
|
296 | 296 | |
297 | 297 | // restore xliff tags |
298 | - if ( isset( $jsonOrRawContentArray[ 'json' ] ) ) { |
|
299 | - $jsonOrRawContentArray[ 'json' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'json' ] ); |
|
298 | + if (isset($jsonOrRawContentArray['json'])) { |
|
299 | + $jsonOrRawContentArray['json'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['json']); |
|
300 | 300 | } |
301 | 301 | |
302 | - if ( isset( $jsonOrRawContentArray[ 'raw-content' ] ) ) { |
|
303 | - $jsonOrRawContentArray[ 'raw-content' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'raw-content' ] ); |
|
302 | + if (isset($jsonOrRawContentArray['raw-content'])) { |
|
303 | + $jsonOrRawContentArray['raw-content'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['raw-content']); |
|
304 | 304 | } |
305 | 305 | |
306 | 306 | $originalData[] = array_merge( |
@@ -325,41 +325,41 @@ discard block |
||
325 | 325 | * |
326 | 326 | * @return array |
327 | 327 | */ |
328 | - private function extractTransUnitAdditionalTagData( DOMElement $transUnit ) { |
|
328 | + private function extractTransUnitAdditionalTagData(DOMElement $transUnit) { |
|
329 | 329 | $additionalTagData = []; |
330 | 330 | |
331 | 331 | // loop <originalData> to get nested content |
332 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
333 | - if ( $childNode->nodeName === 'memsource:additionalTagData' ) { |
|
334 | - foreach ( $childNode->childNodes as $data ) { |
|
332 | + foreach ($transUnit->childNodes as $childNode) { |
|
333 | + if ($childNode->nodeName === 'memsource:additionalTagData') { |
|
334 | + foreach ($childNode->childNodes as $data) { |
|
335 | 335 | $dataArray = []; |
336 | 336 | |
337 | 337 | // id |
338 | - if ( $data->nodeName === 'memsource:tag' ) { |
|
339 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
340 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
341 | - $dataArray[ 'attr' ][ 'id' ] = $dataId; |
|
338 | + if ($data->nodeName === 'memsource:tag') { |
|
339 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
340 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
341 | + $dataArray['attr']['id'] = $dataId; |
|
342 | 342 | } |
343 | 343 | } |
344 | 344 | |
345 | 345 | // in PHP 7.4 $data->childNodes is an empty DomNodeList, it is iterable with size 0 |
346 | 346 | // PHP 5.6 check: in php 5.6 $data->childNodes can be null |
347 | - if ( $data->childNodes != null ) { |
|
347 | + if ($data->childNodes != null) { |
|
348 | 348 | |
349 | 349 | // content |
350 | - foreach ( $data->childNodes as $datum ) { |
|
351 | - if ( $datum->nodeName === 'memsource:tagId' ) { |
|
352 | - $dataArray[ 'raw-content' ][ 'tagId' ] = $datum->nodeValue; |
|
350 | + foreach ($data->childNodes as $datum) { |
|
351 | + if ($datum->nodeName === 'memsource:tagId') { |
|
352 | + $dataArray['raw-content']['tagId'] = $datum->nodeValue; |
|
353 | 353 | } |
354 | 354 | |
355 | - if ( $datum->nodeName === 'memsource:type' ) { |
|
356 | - $dataArray[ 'raw-content' ][ 'type' ] = $datum->nodeValue; |
|
355 | + if ($datum->nodeName === 'memsource:type') { |
|
356 | + $dataArray['raw-content']['type'] = $datum->nodeValue; |
|
357 | 357 | } |
358 | 358 | } |
359 | 359 | |
360 | 360 | } |
361 | 361 | |
362 | - if ( !empty( $dataArray ) ) { |
|
362 | + if (!empty($dataArray)) { |
|
363 | 363 | $additionalTagData[] = $dataArray; |
364 | 364 | } |
365 | 365 | } |
@@ -375,15 +375,15 @@ discard block |
||
375 | 375 | * @param DOMElement $segment |
376 | 376 | * @param array $attr |
377 | 377 | */ |
378 | - private function checkSegmentIdConsistency( DOMElement $segment, array $attr ) { |
|
379 | - if ( isset( $attr[ 'tGroupBegin' ] ) && isset( $attr[ 'tGroupEnd' ] ) && $segment->attributes->getNamedItem( 'id' ) ) { |
|
380 | - $id = $segment->attributes->getNamedItem( 'id' )->nodeValue; |
|
381 | - $min = (int)$attr[ 'tGroupBegin' ]; |
|
382 | - $max = (int)$attr[ 'tGroupEnd' ]; |
|
383 | - |
|
384 | - if ( false === ( ( $min <= $id ) && ( $id <= $max ) ) ) { |
|
385 | - if ( $this->logger ) { |
|
386 | - $this->logger->warning( 'Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd' ); |
|
378 | + private function checkSegmentIdConsistency(DOMElement $segment, array $attr) { |
|
379 | + if (isset($attr['tGroupBegin']) && isset($attr['tGroupEnd']) && $segment->attributes->getNamedItem('id')) { |
|
380 | + $id = $segment->attributes->getNamedItem('id')->nodeValue; |
|
381 | + $min = (int)$attr['tGroupBegin']; |
|
382 | + $max = (int)$attr['tGroupEnd']; |
|
383 | + |
|
384 | + if (false === (($min <= $id) && ($id <= $max))) { |
|
385 | + if ($this->logger) { |
|
386 | + $this->logger->warning('Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd'); |
|
387 | 387 | } |
388 | 388 | } |
389 | 389 | } |
@@ -395,31 +395,31 @@ discard block |
||
395 | 395 | * @return array |
396 | 396 | * @throws \Exception |
397 | 397 | */ |
398 | - private function extractTransUnitNotes( DOMElement $transUnit ) { |
|
398 | + private function extractTransUnitNotes(DOMElement $transUnit) { |
|
399 | 399 | $notes = []; |
400 | 400 | |
401 | 401 | // loop <notes> to get nested <note> tag |
402 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
403 | - if ( $childNode->nodeName == 'notes' ) { |
|
404 | - foreach ( $childNode->childNodes as $note ) { |
|
405 | - $noteValue = trim( $note->nodeValue ); |
|
406 | - if ( '' !== $noteValue ) { |
|
407 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
402 | + foreach ($transUnit->childNodes as $childNode) { |
|
403 | + if ($childNode->nodeName == 'notes') { |
|
404 | + foreach ($childNode->childNodes as $note) { |
|
405 | + $noteValue = trim($note->nodeValue); |
|
406 | + if ('' !== $noteValue) { |
|
407 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
408 | 408 | } |
409 | 409 | } |
410 | 410 | } |
411 | 411 | |
412 | - if ( $childNode->nodeName === 'mda:metadata' ) { |
|
413 | - foreach ( $childNode->childNodes as $metadata ) { |
|
414 | - if ( $metadata->nodeName === 'mda:metaGroup' ) { |
|
415 | - foreach ( $metadata->childNodes as $meta ) { |
|
416 | - if ( null !== $meta->attributes && null !== $meta->attributes->getNamedItem( 'type' ) ) { |
|
417 | - $type = $meta->attributes->getNamedItem( 'type' )->nodeValue; |
|
418 | - $metaValue = trim( $meta->nodeValue ); |
|
412 | + if ($childNode->nodeName === 'mda:metadata') { |
|
413 | + foreach ($childNode->childNodes as $metadata) { |
|
414 | + if ($metadata->nodeName === 'mda:metaGroup') { |
|
415 | + foreach ($metadata->childNodes as $meta) { |
|
416 | + if (null !== $meta->attributes && null !== $meta->attributes->getNamedItem('type')) { |
|
417 | + $type = $meta->attributes->getNamedItem('type')->nodeValue; |
|
418 | + $metaValue = trim($meta->nodeValue); |
|
419 | 419 | |
420 | - if ( '' !== $metaValue ) { |
|
420 | + if ('' !== $metaValue) { |
|
421 | 421 | $notes[] = array_merge( |
422 | - $this->JSONOrRawContentArray( $metaValue ), |
|
422 | + $this->JSONOrRawContentArray($metaValue), |
|
423 | 423 | [ |
424 | 424 | 'attr' => [ |
425 | 425 | 'type' => $type |