@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @param array $map |
18 | 18 | */ |
19 | - public function __construct( array $map ) { |
|
19 | + public function __construct(array $map) { |
|
20 | 20 | $this->map = $map; |
21 | 21 | } |
22 | 22 | |
@@ -31,38 +31,38 @@ discard block |
||
31 | 31 | * |
32 | 32 | * @return string |
33 | 33 | */ |
34 | - public function replace( $string ) { |
|
34 | + public function replace($string) { |
|
35 | 35 | // if map is empty |
36 | 36 | // or the string has not a dataRef attribute |
37 | 37 | // return string as is |
38 | - if ( empty( $this->map ) || !$this->hasAnyDataRefAttribute( $string ) ) { |
|
38 | + if (empty($this->map) || !$this->hasAnyDataRefAttribute($string)) { |
|
39 | 39 | return $string; |
40 | 40 | } |
41 | 41 | |
42 | 42 | // (recursively) clean string from equiv-text eventually present |
43 | - $string = $this->cleanFromEquivText( $string ); |
|
43 | + $string = $this->cleanFromEquivText($string); |
|
44 | 44 | |
45 | - $html = HtmlParser::parse( $string ); |
|
45 | + $html = HtmlParser::parse($string); |
|
46 | 46 | |
47 | 47 | // 1. Replace <ph>|<sc>|<ec> tags |
48 | - foreach ( $html as $node ) { |
|
49 | - $string = $this->recursiveAddEquivTextToPhTag( $node, $string ); |
|
48 | + foreach ($html as $node) { |
|
49 | + $string = $this->recursiveAddEquivTextToPhTag($node, $string); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | // 2. Replace <pc> tags |
53 | - $toBeEscaped = Strings::isAnEscapedHTML( $string ); |
|
53 | + $toBeEscaped = Strings::isAnEscapedHTML($string); |
|
54 | 54 | |
55 | - if ( $this->stringContainsPcTags( $string, $toBeEscaped ) ) { |
|
55 | + if ($this->stringContainsPcTags($string, $toBeEscaped)) { |
|
56 | 56 | |
57 | 57 | // replace self-closed <pc /> |
58 | - $string = $this->replaceSelfClosedPcTags( $string, $toBeEscaped ); |
|
58 | + $string = $this->replaceSelfClosedPcTags($string, $toBeEscaped); |
|
59 | 59 | |
60 | 60 | // create a dataRefEnd map |
61 | 61 | // (needed for correct handling of </pc> closing tags) |
62 | - $dataRefEndMap = $this->buildDataRefEndMap( $html ); |
|
63 | - $string = $this->replaceOpeningPcTags( $string, $toBeEscaped ); |
|
64 | - $string = $this->replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap ); |
|
65 | - $string = ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $string ) : $string; |
|
62 | + $dataRefEndMap = $this->buildDataRefEndMap($html); |
|
63 | + $string = $this->replaceOpeningPcTags($string, $toBeEscaped); |
|
64 | + $string = $this->replaceClosingPcTags($string, $toBeEscaped, $dataRefEndMap); |
|
65 | + $string = ($toBeEscaped) ? Strings::escapeOnlyHTMLTags($string) : $string; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | return $string; |
@@ -73,17 +73,17 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @return bool |
75 | 75 | */ |
76 | - private function hasAnyDataRefAttribute( $string ) { |
|
76 | + private function hasAnyDataRefAttribute($string) { |
|
77 | 77 | $dataRefTags = [ |
78 | 78 | 'dataRef', |
79 | 79 | 'dataRefStart', |
80 | 80 | 'dataRefEnd', |
81 | 81 | ]; |
82 | 82 | |
83 | - foreach ( $dataRefTags as $tag ) { |
|
84 | - preg_match( '/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches ); |
|
83 | + foreach ($dataRefTags as $tag) { |
|
84 | + preg_match('/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches); |
|
85 | 85 | |
86 | - if ( count( $matches ) > 0 ) { |
|
86 | + if (count($matches) > 0) { |
|
87 | 87 | return true; |
88 | 88 | } |
89 | 89 | } |
@@ -94,11 +94,11 @@ discard block |
||
94 | 94 | * |
95 | 95 | * @return string |
96 | 96 | */ |
97 | - private function cleanFromEquivText( $string ) { |
|
98 | - $html = HtmlParser::parse( $string ); |
|
97 | + private function cleanFromEquivText($string) { |
|
98 | + $html = HtmlParser::parse($string); |
|
99 | 99 | |
100 | - foreach ( $html as $node ) { |
|
101 | - $string = $this->recursiveCleanFromEquivText( $node, $string ); |
|
100 | + foreach ($html as $node) { |
|
101 | + $string = $this->recursiveCleanFromEquivText($node, $string); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | return $string; |
@@ -117,57 +117,57 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return string |
119 | 119 | */ |
120 | - private function recursiveAddEquivTextToPhTag( $node, $string ) { |
|
121 | - if ( $node->has_children ) { |
|
122 | - foreach ( $node->inner_html as $childNode ) { |
|
123 | - $string = $this->recursiveAddEquivTextToPhTag( $childNode, $string ); |
|
120 | + private function recursiveAddEquivTextToPhTag($node, $string) { |
|
121 | + if ($node->has_children) { |
|
122 | + foreach ($node->inner_html as $childNode) { |
|
123 | + $string = $this->recursiveAddEquivTextToPhTag($childNode, $string); |
|
124 | 124 | } |
125 | 125 | } else { |
126 | - if ( $node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec' ) { |
|
127 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
126 | + if ($node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec') { |
|
127 | + if (!isset($node->attributes['dataRef'])) { |
|
128 | 128 | return $string; |
129 | 129 | } |
130 | 130 | |
131 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
132 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
131 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
132 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
133 | 133 | |
134 | 134 | |
135 | 135 | // if isset a value in the map calculate base64 encoded value |
136 | 136 | // otherwise skip |
137 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
137 | + if (!in_array($b, array_keys($this->map))) { |
|
138 | 138 | return $string; |
139 | 139 | } |
140 | 140 | |
141 | 141 | // check if is null, in this case convert it to NULL string |
142 | - if ( is_null( $this->map[ $b ] ) ) { |
|
143 | - $this->map[ $b ] = 'NULL'; |
|
142 | + if (is_null($this->map[$b])) { |
|
143 | + $this->map[$b] = 'NULL'; |
|
144 | 144 | } |
145 | 145 | |
146 | - $value = $this->map[ $b ]; |
|
147 | - $base64EncodedValue = base64_encode( $value ); |
|
146 | + $value = $this->map[$b]; |
|
147 | + $base64EncodedValue = base64_encode($value); |
|
148 | 148 | |
149 | - if ( empty( $base64EncodedValue ) || $base64EncodedValue === '' ) { |
|
149 | + if (empty($base64EncodedValue) || $base64EncodedValue === '') { |
|
150 | 150 | return $string; |
151 | 151 | } |
152 | 152 | |
153 | 153 | // if there is no id copy it from dataRef |
154 | - $id = ( !isset( $node->attributes[ 'id' ] ) ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
154 | + $id = (!isset($node->attributes['id'])) ? ' id="' . $b . '" removeId="true"' : ''; |
|
155 | 155 | |
156 | 156 | // introduce dataType for <ec>/<sc> tag handling |
157 | - $dataType = ( $this->isAEcOrScTag( $node ) ) ? ' dataType="' . $node->tagname . '"' : ''; |
|
157 | + $dataType = ($this->isAEcOrScTag($node)) ? ' dataType="' . $node->tagname . '"' : ''; |
|
158 | 158 | |
159 | 159 | // replacement |
160 | - $d = str_replace( '/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a ); |
|
161 | - $a = str_replace( [ '<', '>', '>', '<' ], '', $a ); |
|
162 | - $d = str_replace( [ '<', '>', '>', '<' ], '', $d ); |
|
160 | + $d = str_replace('/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a); |
|
161 | + $a = str_replace(['<', '>', '>', '<'], '', $a); |
|
162 | + $d = str_replace(['<', '>', '>', '<'], '', $d); |
|
163 | 163 | |
164 | 164 | // convert <ec>/<sc> into <ph> |
165 | - if ( $this->isAEcOrScTag( $node ) ) { |
|
166 | - $d = 'ph' . substr( $d, 2 ); |
|
167 | - $d = trim( $d ); |
|
165 | + if ($this->isAEcOrScTag($node)) { |
|
166 | + $d = 'ph' . substr($d, 2); |
|
167 | + $d = trim($d); |
|
168 | 168 | } |
169 | 169 | |
170 | - return str_replace( $a, $d, $string ); |
|
170 | + return str_replace($a, $d, $string); |
|
171 | 171 | } |
172 | 172 | } |
173 | 173 | |
@@ -180,11 +180,11 @@ discard block |
||
180 | 180 | * |
181 | 181 | * @return bool |
182 | 182 | */ |
183 | - private function stringContainsPcTags( $string, $toBeEscaped ) { |
|
184 | - $regex = ( $toBeEscaped ) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
185 | - preg_match_all( $regex, $string, $openingPcMatches ); |
|
183 | + private function stringContainsPcTags($string, $toBeEscaped) { |
|
184 | + $regex = ($toBeEscaped) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
185 | + preg_match_all($regex, $string, $openingPcMatches); |
|
186 | 186 | |
187 | - return ( isset( $openingPcMatches[ 0 ] ) && count( $openingPcMatches[ 0 ] ) > 0 ); |
|
187 | + return (isset($openingPcMatches[0]) && count($openingPcMatches[0]) > 0); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
@@ -193,28 +193,28 @@ discard block |
||
193 | 193 | * |
194 | 194 | * @return mixed |
195 | 195 | */ |
196 | - private function replaceSelfClosedPcTags( $string, $toBeEscaped ) { |
|
197 | - if ( $toBeEscaped ) { |
|
198 | - $string = str_replace( [ '<', '>' ], [ '<', '>' ], $string ); |
|
196 | + private function replaceSelfClosedPcTags($string, $toBeEscaped) { |
|
197 | + if ($toBeEscaped) { |
|
198 | + $string = str_replace(['<', '>'], ['<', '>'], $string); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | $regex = '/<pc[^>]+?\/>/iu'; |
202 | - preg_match_all( $regex, $string, $selfClosedPcMatches ); |
|
202 | + preg_match_all($regex, $string, $selfClosedPcMatches); |
|
203 | 203 | |
204 | - foreach ( $selfClosedPcMatches[ 0 ] as $match ) { |
|
204 | + foreach ($selfClosedPcMatches[0] as $match) { |
|
205 | 205 | |
206 | - $html = HtmlParser::parse( $match ); |
|
207 | - $node = $html[ 0 ]; |
|
206 | + $html = HtmlParser::parse($match); |
|
207 | + $node = $html[0]; |
|
208 | 208 | $attributes = $node->attributes; |
209 | 209 | |
210 | - if ( isset( $attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
|
211 | - $replacement = '<ph id="' . $attributes[ 'id' ] . '" dataType="pcSelf" originalData="' . base64_encode( $match ) . '" dataRef="' . $attributes[ 'dataRefStart' ] . '" equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"/>'; |
|
212 | - $string = str_replace( $match, $replacement, $string ); |
|
210 | + if (isset($attributes['dataRefStart']) && array_key_exists($node->attributes['dataRefStart'], $this->map)) { |
|
211 | + $replacement = '<ph id="' . $attributes['id'] . '" dataType="pcSelf" originalData="' . base64_encode($match) . '" dataRef="' . $attributes['dataRefStart'] . '" equiv-text="base64:' . base64_encode($this->map[$node->attributes['dataRefStart']]) . '"/>'; |
|
212 | + $string = str_replace($match, $replacement, $string); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | - if ( $toBeEscaped ) { |
|
217 | - $string = str_replace( [ '<', '>' ], [ '<', '>' ], $string ); |
|
216 | + if ($toBeEscaped) { |
|
217 | + $string = str_replace(['<', '>'], ['<', '>'], $string); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | return $string; |
@@ -228,12 +228,12 @@ discard block |
||
228 | 228 | * |
229 | 229 | * @return array |
230 | 230 | */ |
231 | - private function buildDataRefEndMap( $html ) { |
|
231 | + private function buildDataRefEndMap($html) { |
|
232 | 232 | $dataRefEndMap = []; |
233 | 233 | |
234 | - foreach ( $html as $index => $node ) { |
|
235 | - if ( $node->tagname === 'pc' ) { |
|
236 | - $this->extractDataRefMapRecursively( $node, $dataRefEndMap ); |
|
234 | + foreach ($html as $index => $node) { |
|
235 | + if ($node->tagname === 'pc') { |
|
236 | + $this->extractDataRefMapRecursively($node, $dataRefEndMap); |
|
237 | 237 | } |
238 | 238 | } |
239 | 239 | |
@@ -246,25 +246,25 @@ discard block |
||
246 | 246 | * @param object $node |
247 | 247 | * @param $dataRefEndMap |
248 | 248 | */ |
249 | - private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
|
250 | - if ( $this->nodeContainsNestedPcTags( $node ) ) { |
|
251 | - foreach ( $node->inner_html as $nestedNode ) { |
|
252 | - $this->extractDataRefMapRecursively( $nestedNode, $dataRefEndMap ); |
|
249 | + private function extractDataRefMapRecursively($node, &$dataRefEndMap) { |
|
250 | + if ($this->nodeContainsNestedPcTags($node)) { |
|
251 | + foreach ($node->inner_html as $nestedNode) { |
|
252 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
253 | 253 | } |
254 | 254 | } |
255 | 255 | |
256 | 256 | // EXCLUDE self closed <pc/> |
257 | - if ( $node->tagname === 'pc' && $node->self_closed === false ) { |
|
258 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
259 | - $dataRefEnd = $node->attributes[ 'dataRefEnd' ]; |
|
260 | - } elseif ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
261 | - $dataRefEnd = $node->attributes[ 'dataRefStart' ]; |
|
257 | + if ($node->tagname === 'pc' && $node->self_closed === false) { |
|
258 | + if (isset($node->attributes['dataRefEnd'])) { |
|
259 | + $dataRefEnd = $node->attributes['dataRefEnd']; |
|
260 | + } elseif (isset($node->attributes['dataRefStart'])) { |
|
261 | + $dataRefEnd = $node->attributes['dataRefStart']; |
|
262 | 262 | } else { |
263 | 263 | $dataRefEnd = null; |
264 | 264 | } |
265 | 265 | |
266 | 266 | $dataRefEndMap[] = [ |
267 | - 'id' => isset( $node->attributes[ 'id' ] ) ? $node->attributes[ 'id' ] : null, |
|
267 | + 'id' => isset($node->attributes['id']) ? $node->attributes['id'] : null, |
|
268 | 268 | 'dataRefEnd' => $dataRefEnd, |
269 | 269 | ]; |
270 | 270 | } |
@@ -276,15 +276,15 @@ discard block |
||
276 | 276 | * |
277 | 277 | * @return string|string[] |
278 | 278 | */ |
279 | - private function recursiveCleanFromEquivText( $node, $string ) { |
|
280 | - if ( $node->has_children ) { |
|
281 | - foreach ( $node->inner_html as $childNode ) { |
|
282 | - $string = $this->recursiveCleanFromEquivText( $childNode, $string ); |
|
279 | + private function recursiveCleanFromEquivText($node, $string) { |
|
280 | + if ($node->has_children) { |
|
281 | + foreach ($node->inner_html as $childNode) { |
|
282 | + $string = $this->recursiveCleanFromEquivText($childNode, $string); |
|
283 | 283 | } |
284 | 284 | } else { |
285 | - if ( isset( $node->attributes[ 'dataRef' ] ) && array_key_exists( $node->attributes[ 'dataRef' ], $this->map ) ) { |
|
286 | - $cleaned = preg_replace( '/ equiv-text="(.*?)"/', '', $node->node ); |
|
287 | - $string = str_replace( $node->node, $cleaned, $string ); |
|
285 | + if (isset($node->attributes['dataRef']) && array_key_exists($node->attributes['dataRef'], $this->map)) { |
|
286 | + $cleaned = preg_replace('/ equiv-text="(.*?)"/', '', $node->node); |
|
287 | + $string = str_replace($node->node, $cleaned, $string); |
|
288 | 288 | } |
289 | 289 | } |
290 | 290 | |
@@ -299,35 +299,35 @@ discard block |
||
299 | 299 | * |
300 | 300 | * @return string |
301 | 301 | */ |
302 | - private function replaceOpeningPcTags( $string, $toBeEscaped ) { |
|
303 | - $regex = ( $toBeEscaped ) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
304 | - preg_match_all( $regex, $string, $openingPcMatches ); |
|
302 | + private function replaceOpeningPcTags($string, $toBeEscaped) { |
|
303 | + $regex = ($toBeEscaped) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
304 | + preg_match_all($regex, $string, $openingPcMatches); |
|
305 | 305 | |
306 | - foreach ( $openingPcMatches[ 0 ] as $index => $match ) { |
|
307 | - $attr = HtmlParser::getAttributes( $openingPcMatches[ 1 ][ $index ] ); |
|
306 | + foreach ($openingPcMatches[0] as $index => $match) { |
|
307 | + $attr = HtmlParser::getAttributes($openingPcMatches[1][$index]); |
|
308 | 308 | |
309 | 309 | // CASE 1 - Missing `dataRefStart` |
310 | - if ( isset( $attr[ 'dataRefEnd' ] ) && !isset( $attr[ 'dataRefStart' ] ) ) { |
|
311 | - $attr[ 'dataRefStart' ] = $attr[ 'dataRefEnd' ]; |
|
310 | + if (isset($attr['dataRefEnd']) && !isset($attr['dataRefStart'])) { |
|
311 | + $attr['dataRefStart'] = $attr['dataRefEnd']; |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | // CASE 2 - Missing `dataRefEnd` |
315 | - if ( isset( $attr[ 'dataRefStart' ] ) && !isset( $attr[ 'dataRefEnd' ] ) ) { |
|
316 | - $attr[ 'dataRefEnd' ] = $attr[ 'dataRefStart' ]; |
|
315 | + if (isset($attr['dataRefStart']) && !isset($attr['dataRefEnd'])) { |
|
316 | + $attr['dataRefEnd'] = $attr['dataRefStart']; |
|
317 | 317 | } |
318 | 318 | |
319 | - if ( isset( $attr[ 'dataRefStart' ] ) ) { |
|
319 | + if (isset($attr['dataRefStart'])) { |
|
320 | 320 | $startOriginalData = $match; // opening <pc> |
321 | - $startValue = $this->map[ $attr[ 'dataRefStart' ] ] ? $this->map[ $attr[ 'dataRefStart' ] ] : 'NULL'; //handling null values in original data map |
|
322 | - $base64EncodedStartValue = base64_encode( $startValue ); |
|
323 | - $base64StartOriginalData = base64_encode( $startOriginalData ); |
|
321 | + $startValue = $this->map[$attr['dataRefStart']] ? $this->map[$attr['dataRefStart']] : 'NULL'; //handling null values in original data map |
|
322 | + $base64EncodedStartValue = base64_encode($startValue); |
|
323 | + $base64StartOriginalData = base64_encode($startOriginalData); |
|
324 | 324 | |
325 | 325 | // conversion for opening <pc> tag |
326 | - $openingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_1"' : '' ) . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
327 | - . $attr[ 'dataRefStart' ] . '" equiv-text="base64:' |
|
326 | + $openingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_1"' : '') . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
327 | + . $attr['dataRefStart'] . '" equiv-text="base64:' |
|
328 | 328 | . $base64EncodedStartValue . '"/>'; |
329 | 329 | |
330 | - $string = str_replace( $startOriginalData, $openingPcConverted, $string ); |
|
330 | + $string = str_replace($startOriginalData, $openingPcConverted, $string); |
|
331 | 331 | } |
332 | 332 | } |
333 | 333 | |
@@ -344,34 +344,34 @@ discard block |
||
344 | 344 | * |
345 | 345 | * @return string |
346 | 346 | */ |
347 | - private function replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap = [] ) { |
|
348 | - $regex = ( $toBeEscaped ) ? '/<\/pc>/iu' : '/<\/pc>/iu'; |
|
349 | - preg_match_all( $regex, $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
347 | + private function replaceClosingPcTags($string, $toBeEscaped, $dataRefEndMap = []) { |
|
348 | + $regex = ($toBeEscaped) ? '/<\/pc>/iu' : '/<\/pc>/iu'; |
|
349 | + preg_match_all($regex, $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
350 | 350 | $delta = 0; |
351 | 351 | |
352 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
353 | - $offset = $match[ 1 ]; |
|
354 | - $length = strlen( $match[ 0 ] ); |
|
355 | - $attr = $dataRefEndMap[ $index ]; |
|
352 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
353 | + $offset = $match[1]; |
|
354 | + $length = strlen($match[0]); |
|
355 | + $attr = $dataRefEndMap[$index]; |
|
356 | 356 | |
357 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
358 | - $endOriginalData = $match[ 0 ]; // </pc> |
|
359 | - $endValue = $this->map[ $attr[ 'dataRefEnd' ] ] ?: 'NULL'; |
|
360 | - $base64EncodedEndValue = base64_encode( $endValue ); |
|
361 | - $base64EndOriginalData = base64_encode( $endOriginalData ); |
|
357 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
358 | + $endOriginalData = $match[0]; // </pc> |
|
359 | + $endValue = $this->map[$attr['dataRefEnd']] ?: 'NULL'; |
|
360 | + $base64EncodedEndValue = base64_encode($endValue); |
|
361 | + $base64EndOriginalData = base64_encode($endOriginalData); |
|
362 | 362 | |
363 | 363 | // conversion for closing <pc> tag |
364 | - $closingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_2"' : '' ) . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
365 | - . $attr[ 'dataRefEnd' ] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
364 | + $closingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_2"' : '') . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
365 | + . $attr['dataRefEnd'] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
366 | 366 | |
367 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
367 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
368 | 368 | |
369 | - $string = substr_replace( $string, $closingPcConverted, $realOffset, $length ); |
|
370 | - $delta = $delta + strlen( $closingPcConverted ) - $length; |
|
369 | + $string = substr_replace($string, $closingPcConverted, $realOffset, $length); |
|
370 | + $delta = $delta + strlen($closingPcConverted) - $length; |
|
371 | 371 | } |
372 | 372 | } |
373 | 373 | |
374 | - return !is_array( $string ) ? $string : implode( $string ); |
|
374 | + return !is_array($string) ? $string : implode($string); |
|
375 | 375 | } |
376 | 376 | |
377 | 377 | /** |
@@ -379,13 +379,13 @@ discard block |
||
379 | 379 | * |
380 | 380 | * @return bool |
381 | 381 | */ |
382 | - private function nodeContainsNestedPcTags( $node ) { |
|
383 | - if ( !$node->has_children ) { |
|
382 | + private function nodeContainsNestedPcTags($node) { |
|
383 | + if (!$node->has_children) { |
|
384 | 384 | return false; |
385 | 385 | } |
386 | 386 | |
387 | - foreach ( $node->inner_html as $nestedNode ) { |
|
388 | - if ( $nestedNode->tagname === 'pc' && ( isset( $node->attributes[ 'dataRefEnd' ] ) || isset( $node->attributes[ 'dataRefStart' ] ) ) ) { |
|
387 | + foreach ($node->inner_html as $nestedNode) { |
|
388 | + if ($nestedNode->tagname === 'pc' && (isset($node->attributes['dataRefEnd']) || isset($node->attributes['dataRefStart']))) { |
|
389 | 389 | return true; |
390 | 390 | } |
391 | 391 | } |
@@ -398,18 +398,18 @@ discard block |
||
398 | 398 | * |
399 | 399 | * @return string |
400 | 400 | */ |
401 | - public function restore( $string ) { |
|
401 | + public function restore($string) { |
|
402 | 402 | // if map is empty return string as is |
403 | - if ( empty( $this->map ) ) { |
|
403 | + if (empty($this->map)) { |
|
404 | 404 | return $string; |
405 | 405 | } |
406 | 406 | |
407 | 407 | // replace eventual empty equiv-text="" |
408 | - $string = str_replace( ' equiv-text=""', '', $string ); |
|
409 | - $html = HtmlParser::parse( $string ); |
|
408 | + $string = str_replace(' equiv-text=""', '', $string); |
|
409 | + $html = HtmlParser::parse($string); |
|
410 | 410 | |
411 | - foreach ( $html as $node ) { |
|
412 | - $string = $this->recursiveRemoveOriginalData( $node, $string ); |
|
411 | + foreach ($html as $node) { |
|
412 | + $string = $this->recursiveRemoveOriginalData($node, $string); |
|
413 | 413 | } |
414 | 414 | |
415 | 415 | return $string; |
@@ -421,76 +421,76 @@ discard block |
||
421 | 421 | * |
422 | 422 | * @return string|string[] |
423 | 423 | */ |
424 | - private function recursiveRemoveOriginalData( $node, $string ) { |
|
425 | - if ( $node->has_children ) { |
|
426 | - foreach ( $node->inner_html as $childNode ) { |
|
427 | - $string = $this->recursiveRemoveOriginalData( $childNode, $string ); |
|
424 | + private function recursiveRemoveOriginalData($node, $string) { |
|
425 | + if ($node->has_children) { |
|
426 | + foreach ($node->inner_html as $childNode) { |
|
427 | + $string = $this->recursiveRemoveOriginalData($childNode, $string); |
|
428 | 428 | } |
429 | 429 | } else { |
430 | 430 | |
431 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
431 | + if (!isset($node->attributes['dataRef'])) { |
|
432 | 432 | return $string; |
433 | 433 | } |
434 | 434 | |
435 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
436 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
437 | - $c = $node->terminator; // terminator: Eg: > |
|
435 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
436 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
437 | + $c = $node->terminator; // terminator: Eg: > |
|
438 | 438 | |
439 | 439 | // if isset a value in the map calculate base64 encoded value |
440 | 440 | // or it is an empty string |
441 | 441 | // otherwise skip |
442 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
442 | + if (!in_array($b, array_keys($this->map))) { |
|
443 | 443 | return $string; |
444 | 444 | } |
445 | 445 | |
446 | 446 | // check if is null, in this case convert it to NULL string |
447 | - if ( is_null( $this->map[ $b ] ) ) { |
|
448 | - $this->map[ $b ] = 'NULL'; |
|
447 | + if (is_null($this->map[$b])) { |
|
448 | + $this->map[$b] = 'NULL'; |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | // remove id? |
452 | - $removeId = ( isset( $node->attributes[ 'removeId' ] ) && $node->attributes[ 'removeId' ] === "true" ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
452 | + $removeId = (isset($node->attributes['removeId']) && $node->attributes['removeId'] === "true") ? ' id="' . $b . '" removeId="true"' : ''; |
|
453 | 453 | |
454 | 454 | // grab dataType attribute for <ec>/<sc> tag handling |
455 | - $dataType = ( $this->wasAEcOrScTag( $node ) ) ? ' dataType="' . $node->attributes[ 'dataType' ] . '"' : ''; |
|
455 | + $dataType = ($this->wasAEcOrScTag($node)) ? ' dataType="' . $node->attributes['dataType'] . '"' : ''; |
|
456 | 456 | |
457 | - $d = str_replace( $removeId . $dataType . ' equiv-text="base64:' . base64_encode( $this->map[ $b ] ) . '"/' . $c, '/' . $c, $a ); |
|
457 | + $d = str_replace($removeId . $dataType . ' equiv-text="base64:' . base64_encode($this->map[$b]) . '"/' . $c, '/' . $c, $a); |
|
458 | 458 | |
459 | 459 | // replace original <ec>/<sc> tag |
460 | - if ( $this->wasAEcOrScTag( $node ) ) { |
|
461 | - $d = $node->attributes[ 'dataType' ] . substr( $d, 3 ); |
|
462 | - $d = trim( $d ); |
|
460 | + if ($this->wasAEcOrScTag($node)) { |
|
461 | + $d = $node->attributes['dataType'] . substr($d, 3); |
|
462 | + $d = trim($d); |
|
463 | 463 | } |
464 | 464 | |
465 | 465 | // replace only content tag, no matter if the string is encoded or not |
466 | 466 | // in this way we can handle string with mixed tags (encoded and not-encoded) |
467 | 467 | // in the same string |
468 | - $a = $this->purgeTags( $a ); |
|
469 | - $d = $this->purgeTags( $d ); |
|
468 | + $a = $this->purgeTags($a); |
|
469 | + $d = $this->purgeTags($d); |
|
470 | 470 | |
471 | - $string = str_replace( $a, $d, $string ); |
|
471 | + $string = str_replace($a, $d, $string); |
|
472 | 472 | |
473 | 473 | // restoring <pc/> self-closed here |
474 | - if ( Strings::contains( 'dataType="pcSelf"', $d ) ) { |
|
475 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
474 | + if (Strings::contains('dataType="pcSelf"', $d)) { |
|
475 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
476 | 476 | |
477 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
478 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
479 | - $originalData = $this->purgeTags( $originalData ); |
|
480 | - $string = str_replace( $d, $originalData, $string ); |
|
477 | + if (isset($originalDataMatches[1])) { |
|
478 | + $originalData = base64_decode($originalDataMatches[1]); |
|
479 | + $originalData = $this->purgeTags($originalData); |
|
480 | + $string = str_replace($d, $originalData, $string); |
|
481 | 481 | } |
482 | 482 | } |
483 | 483 | |
484 | 484 | // restoring <pc> tags here |
485 | 485 | // if <ph> tag has originalData and originalType is pcStart or pcEnd, |
486 | 486 | // replace with original data |
487 | - if ( Strings::contains( 'dataType="pcStart"', $d ) || Strings::contains( 'dataType="pcEnd"', $d ) ) { |
|
488 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
487 | + if (Strings::contains('dataType="pcStart"', $d) || Strings::contains('dataType="pcEnd"', $d)) { |
|
488 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
489 | 489 | |
490 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
491 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
492 | - $originalData = $this->purgeTags( $originalData ); |
|
493 | - $string = str_replace( $d, $originalData, $string ); |
|
490 | + if (isset($originalDataMatches[1])) { |
|
491 | + $originalData = base64_decode($originalDataMatches[1]); |
|
492 | + $originalData = $this->purgeTags($originalData); |
|
493 | + $string = str_replace($d, $originalData, $string); |
|
494 | 494 | } |
495 | 495 | } |
496 | 496 | } |
@@ -503,8 +503,8 @@ discard block |
||
503 | 503 | * |
504 | 504 | * @return string |
505 | 505 | */ |
506 | - private function purgeTags( $string ) { |
|
507 | - return str_replace( [ '<', '>', '<', '>' ], '', $string ); |
|
506 | + private function purgeTags($string) { |
|
507 | + return str_replace(['<', '>', '<', '>'], '', $string); |
|
508 | 508 | } |
509 | 509 | |
510 | 510 | /** |
@@ -514,8 +514,8 @@ discard block |
||
514 | 514 | * |
515 | 515 | * @return bool |
516 | 516 | */ |
517 | - private function isAEcOrScTag( $node ) { |
|
518 | - return ( $node->tagname === 'ec' || $node->tagname === 'sc' ); |
|
517 | + private function isAEcOrScTag($node) { |
|
518 | + return ($node->tagname === 'ec' || $node->tagname === 'sc'); |
|
519 | 519 | } |
520 | 520 | |
521 | 521 | /** |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | * |
527 | 527 | * @return bool |
528 | 528 | */ |
529 | - private function wasAEcOrScTag( $node ) { |
|
530 | - return ( isset( $node->attributes[ 'dataType' ] ) && ( $node->attributes[ 'dataType' ] === 'ec' || $node->attributes[ 'dataType' ] === 'sc' ) ); |
|
529 | + private function wasAEcOrScTag($node) { |
|
530 | + return (isset($node->attributes['dataType']) && ($node->attributes['dataType'] === 'ec' || $node->attributes['dataType'] === 'sc')); |
|
531 | 531 | } |
532 | 532 | } |
@@ -27,65 +27,65 @@ discard block |
||
27 | 27 | * @throws InvalidXmlException When parsing of XML with schema or callable produces any errors unrelated to the XML parsing itself |
28 | 28 | * @throws RuntimeException When DOM extension is missing |
29 | 29 | */ |
30 | - public static function parse( $content, $schemaOrCallable = null ) { |
|
31 | - if ( !extension_loaded( 'dom' ) ) { |
|
32 | - throw new RuntimeException( 'Extension DOM is required.' ); |
|
30 | + public static function parse($content, $schemaOrCallable = null) { |
|
31 | + if (!extension_loaded('dom')) { |
|
32 | + throw new RuntimeException('Extension DOM is required.'); |
|
33 | 33 | } |
34 | 34 | |
35 | - $internalErrors = libxml_use_internal_errors( true ); |
|
36 | - $disableEntities = libxml_disable_entity_loader( true ); |
|
35 | + $internalErrors = libxml_use_internal_errors(true); |
|
36 | + $disableEntities = libxml_disable_entity_loader(true); |
|
37 | 37 | libxml_clear_errors(); |
38 | 38 | |
39 | - $dom = new DOMDocument( '1.0', 'UTF-8' ); |
|
39 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
40 | 40 | $dom->validateOnParse = true; |
41 | - if ( !$dom->loadXML( $content, LIBXML_NONET | ( defined( 'LIBXML_COMPACT' ) ? LIBXML_COMPACT : 0 ) ) ) { |
|
42 | - libxml_disable_entity_loader( $disableEntities ); |
|
41 | + if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { |
|
42 | + libxml_disable_entity_loader($disableEntities); |
|
43 | 43 | |
44 | - throw new XmlParsingException( implode( "\n", static::getXmlErrors( $internalErrors ) ) ); |
|
44 | + throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors))); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | $dom->normalizeDocument(); |
48 | 48 | |
49 | - libxml_use_internal_errors( $internalErrors ); |
|
50 | - libxml_disable_entity_loader( $disableEntities ); |
|
49 | + libxml_use_internal_errors($internalErrors); |
|
50 | + libxml_disable_entity_loader($disableEntities); |
|
51 | 51 | |
52 | - foreach ( $dom->childNodes as $child ) { |
|
53 | - if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) { |
|
54 | - throw new XmlParsingException( 'Document types are not allowed.' ); |
|
52 | + foreach ($dom->childNodes as $child) { |
|
53 | + if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) { |
|
54 | + throw new XmlParsingException('Document types are not allowed.'); |
|
55 | 55 | } |
56 | 56 | } |
57 | 57 | |
58 | - if ( null !== $schemaOrCallable ) { |
|
59 | - $internalErrors = libxml_use_internal_errors( true ); |
|
58 | + if (null !== $schemaOrCallable) { |
|
59 | + $internalErrors = libxml_use_internal_errors(true); |
|
60 | 60 | libxml_clear_errors(); |
61 | 61 | |
62 | 62 | $e = null; |
63 | - if ( is_callable( $schemaOrCallable ) ) { |
|
63 | + if (is_callable($schemaOrCallable)) { |
|
64 | 64 | try { |
65 | - $valid = call_user_func( $schemaOrCallable, $dom, $internalErrors ); |
|
66 | - } catch ( Exception $e ) { |
|
65 | + $valid = call_user_func($schemaOrCallable, $dom, $internalErrors); |
|
66 | + } catch (Exception $e) { |
|
67 | 67 | $valid = false; |
68 | 68 | } |
69 | - } elseif ( !is_array( $schemaOrCallable ) && is_file( (string)$schemaOrCallable ) ) { |
|
70 | - $schemaSource = file_get_contents( (string)$schemaOrCallable ); |
|
71 | - $valid = @$dom->schemaValidateSource( $schemaSource ); |
|
69 | + } elseif (!is_array($schemaOrCallable) && is_file((string)$schemaOrCallable)) { |
|
70 | + $schemaSource = file_get_contents((string)$schemaOrCallable); |
|
71 | + $valid = @$dom->schemaValidateSource($schemaSource); |
|
72 | 72 | } else { |
73 | - libxml_use_internal_errors( $internalErrors ); |
|
73 | + libxml_use_internal_errors($internalErrors); |
|
74 | 74 | |
75 | - throw new XmlParsingException( 'The schemaOrCallable argument has to be a valid path to XSD file or callable.' ); |
|
75 | + throw new XmlParsingException('The schemaOrCallable argument has to be a valid path to XSD file or callable.'); |
|
76 | 76 | } |
77 | 77 | |
78 | - if ( !$valid ) { |
|
79 | - $messages = static::getXmlErrors( $internalErrors ); |
|
80 | - if ( empty( $messages ) ) { |
|
81 | - throw new InvalidXmlException( 'The XML is not valid.', 0, $e ); |
|
78 | + if (!$valid) { |
|
79 | + $messages = static::getXmlErrors($internalErrors); |
|
80 | + if (empty($messages)) { |
|
81 | + throw new InvalidXmlException('The XML is not valid.', 0, $e); |
|
82 | 82 | } |
83 | - throw new XmlParsingException( implode( "\n", $messages ), 0, $e ); |
|
83 | + throw new XmlParsingException(implode("\n", $messages), 0, $e); |
|
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
87 | 87 | libxml_clear_errors(); |
88 | - libxml_use_internal_errors( $internalErrors ); |
|
88 | + libxml_use_internal_errors($internalErrors); |
|
89 | 89 | |
90 | 90 | return $dom; |
91 | 91 | } |
@@ -95,14 +95,14 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @return array |
97 | 97 | */ |
98 | - private static function getXmlErrors( $internalErrors ) { |
|
98 | + private static function getXmlErrors($internalErrors) { |
|
99 | 99 | $errors = []; |
100 | - foreach ( libxml_get_errors() as $error ) { |
|
100 | + foreach (libxml_get_errors() as $error) { |
|
101 | 101 | $errors[] = sprintf( |
102 | 102 | '[%s %s] %s (in %s - line %d, column %d)', |
103 | 103 | LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', |
104 | 104 | $error->code, |
105 | - trim( $error->message ), |
|
105 | + trim($error->message), |
|
106 | 106 | $error->file ?: 'n/a', |
107 | 107 | $error->line, |
108 | 108 | $error->column |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | } |
111 | 111 | |
112 | 112 | libxml_clear_errors(); |
113 | - libxml_use_internal_errors( $internalErrors ); |
|
113 | + libxml_use_internal_errors($internalErrors); |
|
114 | 114 | |
115 | 115 | return $errors; |
116 | 116 | } |
@@ -19,15 +19,15 @@ discard block |
||
19 | 19 | |
20 | 20 | // get id |
21 | 21 | // trim to first 100 characters because this is the limit on Matecat's DB |
22 | - $this->currentTransUnitId = substr($attr[ 'id' ], 0, 100); |
|
22 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
23 | 23 | |
24 | 24 | // current 'translate' attribute of the current trans-unit |
25 | - $this->currentTransUnitTranslate = isset($attr[ 'translate' ]) ? $attr[ 'translate' ] : 'yes'; |
|
25 | + $this->currentTransUnitTranslate = isset($attr['translate']) ? $attr['translate'] : 'yes'; |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | // check if we are entering into a <target> |
29 | 29 | if ('target' == $name) { |
30 | - if($this->currentTransUnitTranslate === 'no'){ |
|
30 | + if ($this->currentTransUnitTranslate === 'no') { |
|
31 | 31 | $this->inTarget = false; |
32 | 32 | } else { |
33 | 33 | $this->inTarget = true; |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | } elseif ('sdl:seg' == $name) { |
65 | 65 | |
66 | 66 | // write the confidence level for this segment ( Translated, Draft, etc. ) |
67 | - if (isset($this->segments[ 'matecat|' . $this->currentTransUnitId ]) && $_sdlStatus_confWritten === false) { |
|
67 | + if (isset($this->segments['matecat|' . $this->currentTransUnitId]) && $_sdlStatus_confWritten === false) { |
|
68 | 68 | |
69 | 69 | // append definition attribute |
70 | - $tag .= $this->prepareTargetStatuses($this->lastTransUnit[ $this->markerPos ]); |
|
70 | + $tag .= $this->prepareTargetStatuses($this->lastTransUnit[$this->markerPos]); |
|
71 | 71 | |
72 | 72 | //prepare for an eventual next cycle |
73 | 73 | $this->markerPos++; |
@@ -94,15 +94,15 @@ discard block |
||
94 | 94 | // is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
95 | 95 | // this check is necessary because we may have truncated a tag in half with current read, |
96 | 96 | // and the other half may be encountered in the next buffer it will be passed |
97 | - if (isset($this->currentBuffer[ $idx - $this->offset ])) { |
|
97 | + if (isset($this->currentBuffer[$idx - $this->offset])) { |
|
98 | 98 | // if this tag entire lenght fitted in the buffer, the last char must be the last |
99 | 99 | // symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
100 | 100 | $tmp_offset = $idx - $this->offset; |
101 | - $lastChar = $this->currentBuffer[ $tmp_offset ]; |
|
101 | + $lastChar = $this->currentBuffer[$tmp_offset]; |
|
102 | 102 | } else { |
103 | 103 | //if it's out, simple use the last character of the chunk |
104 | 104 | $tmp_offset = $this->len - 1; |
105 | - $lastChar = $this->currentBuffer[ $tmp_offset ]; |
|
105 | + $lastChar = $this->currentBuffer[$tmp_offset]; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | // trim last space |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | } else { |
137 | 137 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
138 | 138 | $this->bufferIsActive = true; |
139 | - $this->CDATABuffer .= $tag; |
|
139 | + $this->CDATABuffer .= $tag; |
|
140 | 140 | } |
141 | 141 | } else { |
142 | 142 | $this->postProcAndFlush($this->outputFP, $tag); |
@@ -156,11 +156,11 @@ discard block |
||
156 | 156 | { |
157 | 157 | $endTags = ""; |
158 | 158 | |
159 | - $segment = Strings::removeDangerousChars($seg [ 'segment' ]); |
|
160 | - $translation = Strings::removeDangerousChars($seg [ 'translation' ]); |
|
159 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
160 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
161 | 161 | $dataRefMap = (isset($seg['data_ref_map']) && $seg['data_ref_map'] !== null) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
162 | 162 | |
163 | - if (is_null($seg [ 'translation' ]) || $seg [ 'translation' ] == '') { |
|
163 | + if (is_null($seg ['translation']) || $seg ['translation'] == '') { |
|
164 | 164 | $translation = $segment; |
165 | 165 | } else { |
166 | 166 | if ($this->callback) { |
@@ -173,20 +173,20 @@ discard block |
||
173 | 173 | // for Trados the trailing spaces after </mrk> are meaningful |
174 | 174 | // so we trim the translation from Matecat DB and add them after </mrk> |
175 | 175 | $trailingSpaces = ''; |
176 | - for ($s=0; $s < Strings::getTheNumberOfTrailingSpaces($translation); $s++){ |
|
176 | + for ($s = 0; $s < Strings::getTheNumberOfTrailingSpaces($translation); $s++) { |
|
177 | 177 | $trailingSpaces .= ' '; |
178 | 178 | } |
179 | 179 | |
180 | - if ($seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '') { |
|
180 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
181 | 181 | if ($this->targetLang === 'ja-JP') { |
182 | - $seg[ 'mrk_succ_tags' ] = ltrim($seg[ 'mrk_succ_tags' ]); |
|
182 | + $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']); |
|
183 | 183 | } |
184 | 184 | |
185 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim($translation) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces; |
|
185 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . rtrim($translation) . $seg['mrk_succ_tags'] . "</mrk>" . $trailingSpaces; |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | // we need to trim succ_tags here because we already added the trailing spaces after </mrk> |
189 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . ltrim($seg[ 'succ_tags' ]); |
|
189 | + $transUnitTranslation .= $seg['prev_tags'] . $translation . $endTags . ltrim($seg['succ_tags']); |
|
190 | 190 | |
191 | 191 | return $transUnitTranslation; |
192 | 192 | } |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | 'REJECTED' => 'RejectedTranslation', |
207 | 207 | ]; |
208 | 208 | |
209 | - return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" "; |
|
209 | + return "conf=\"{$statusMap[$segment['status']]}\" "; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | */ |
219 | 219 | protected function setTransUnitState($seg, $state_prop, $lastMrkState) |
220 | 220 | { |
221 | - return [ null, null ]; |
|
221 | + return [null, null]; |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | /** |
@@ -8,28 +8,28 @@ discard block |
||
8 | 8 | abstract class AbstractXliffReplacer { |
9 | 9 | protected $originalFP; |
10 | 10 | |
11 | - protected $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
12 | - protected $inTU = false; // flag to check whether we are in a <trans-unit> |
|
13 | - protected $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
14 | - protected $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
15 | - protected $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
16 | - protected $segmentPositionInTu = -1; // the current position of segment in the current <unit> (forXliff v 2.*) |
|
17 | - |
|
18 | - protected $CDATABuffer = ""; // buffer for special tag |
|
19 | - protected $bufferIsActive = false; // buffer for special tag |
|
20 | - |
|
21 | - protected $offset = 0; // offset for SAX pointer |
|
22 | - protected $outputFP; // output stream pointer |
|
23 | - protected $currentBuffer; // the current piece of text it's been parsed |
|
24 | - protected $len; // length of the currentBuffer |
|
25 | - protected $segments; // array of translations |
|
11 | + protected $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
12 | + protected $inTU = false; // flag to check whether we are in a <trans-unit> |
|
13 | + protected $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
14 | + protected $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
15 | + protected $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
16 | + protected $segmentPositionInTu = -1; // the current position of segment in the current <unit> (forXliff v 2.*) |
|
17 | + |
|
18 | + protected $CDATABuffer = ""; // buffer for special tag |
|
19 | + protected $bufferIsActive = false; // buffer for special tag |
|
20 | + |
|
21 | + protected $offset = 0; // offset for SAX pointer |
|
22 | + protected $outputFP; // output stream pointer |
|
23 | + protected $currentBuffer; // the current piece of text it's been parsed |
|
24 | + protected $len; // length of the currentBuffer |
|
25 | + protected $segments; // array of translations |
|
26 | 26 | protected $lastTransUnit = []; |
27 | - protected $currentTransUnitId; // id of current <trans-unit> |
|
27 | + protected $currentTransUnitId; // id of current <trans-unit> |
|
28 | 28 | protected $currentTransUnitTranslate; // 'translate' attribute of current <trans-unit> |
29 | - protected $currentSegmentArray = []; // id of current <segment> (forXliff v 2.*) |
|
30 | - protected $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
31 | - protected $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
32 | - protected $sourceAttributes = []; // current <source> attributes (needed when handling xliff files without <target>) |
|
29 | + protected $currentSegmentArray = []; // id of current <segment> (forXliff v 2.*) |
|
30 | + protected $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
31 | + protected $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
32 | + protected $sourceAttributes = []; // current <source> attributes (needed when handling xliff files without <target>) |
|
33 | 33 | |
34 | 34 | protected $targetLang; |
35 | 35 | |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | XliffReplacerCallbackInterface $callback = null |
77 | 77 | ) { |
78 | 78 | self::$INTERNAL_TAG_PLACEHOLDER = $this->getInternalTagPlaceholder(); |
79 | - $this->createOutputFileIfDoesNotExist( $outputFilePath ); |
|
80 | - $this->setFileDescriptors( $originalXliffPath, $outputFilePath ); |
|
79 | + $this->createOutputFileIfDoesNotExist($outputFilePath); |
|
80 | + $this->setFileDescriptors($originalXliffPath, $outputFilePath); |
|
81 | 81 | $this->xliffVersion = $xliffVersion; |
82 | 82 | $this->setTuTagName(); |
83 | 83 | $this->segments = $segments; |
@@ -95,19 +95,19 @@ discard block |
||
95 | 95 | return "§" . |
96 | 96 | substr( |
97 | 97 | str_replace( |
98 | - [ '+', '/' ], |
|
98 | + ['+', '/'], |
|
99 | 99 | '', |
100 | - base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) ) |
|
100 | + base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong)) |
|
101 | 101 | ), |
102 | 102 | 0, |
103 | 103 | 4 |
104 | 104 | ); |
105 | 105 | } |
106 | 106 | |
107 | - private function createOutputFileIfDoesNotExist( $outputFilePath ) { |
|
107 | + private function createOutputFileIfDoesNotExist($outputFilePath) { |
|
108 | 108 | // create output file |
109 | - if ( !file_exists( $outputFilePath ) ) { |
|
110 | - touch( $outputFilePath ); |
|
109 | + if (!file_exists($outputFilePath)) { |
|
110 | + touch($outputFilePath); |
|
111 | 111 | } |
112 | 112 | } |
113 | 113 | |
@@ -115,13 +115,13 @@ discard block |
||
115 | 115 | * @param $originalXliffPath |
116 | 116 | * @param $outputFilePath |
117 | 117 | */ |
118 | - private function setFileDescriptors( $originalXliffPath, $outputFilePath ) { |
|
119 | - $this->outputFP = fopen( $outputFilePath, 'w+' ); |
|
118 | + private function setFileDescriptors($originalXliffPath, $outputFilePath) { |
|
119 | + $this->outputFP = fopen($outputFilePath, 'w+'); |
|
120 | 120 | |
121 | 121 | $streamArgs = null; |
122 | 122 | |
123 | - if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) { |
|
124 | - throw new RuntimeException( "could not open XML input" ); |
|
123 | + if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) { |
|
124 | + throw new RuntimeException("could not open XML input"); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * <trans-unit> (xliff v1.*) || <unit> (xliff v2.*) |
131 | 131 | */ |
132 | 132 | private function setTuTagName() { |
133 | - $this->tuTagName = ( $this->xliffVersion === 2 ) ? 'unit' : 'trans-unit'; |
|
133 | + $this->tuTagName = ($this->xliffVersion === 2) ? 'unit' : 'trans-unit'; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -139,8 +139,8 @@ discard block |
||
139 | 139 | public function __destruct() { |
140 | 140 | //this stream can be closed outside the class |
141 | 141 | //to permit multiple concurrent downloads, so suppress warnings |
142 | - @fclose( $this->originalFP ); |
|
143 | - fclose( $this->outputFP ); |
|
142 | + @fclose($this->originalFP); |
|
143 | + fclose($this->outputFP); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | abstract public function replaceTranslation(); |
@@ -151,11 +151,11 @@ discard block |
||
151 | 151 | * @return resource |
152 | 152 | */ |
153 | 153 | protected function initSaxParser() { |
154 | - $xmlSaxParser = xml_parser_create( 'UTF-8' ); |
|
155 | - xml_set_object( $xmlSaxParser, $this ); |
|
156 | - xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false ); |
|
157 | - xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' ); |
|
158 | - xml_set_character_data_handler( $xmlSaxParser, 'characterData' ); |
|
154 | + $xmlSaxParser = xml_parser_create('UTF-8'); |
|
155 | + xml_set_object($xmlSaxParser, $this); |
|
156 | + xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false); |
|
157 | + xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose'); |
|
158 | + xml_set_character_data_handler($xmlSaxParser, 'characterData'); |
|
159 | 159 | |
160 | 160 | return $xmlSaxParser; |
161 | 161 | } |
@@ -163,8 +163,8 @@ discard block |
||
163 | 163 | /** |
164 | 164 | * @param resource $xmlSaxParser |
165 | 165 | */ |
166 | - protected function closeSaxParser( $xmlSaxParser ) { |
|
167 | - xml_parser_free( $xmlSaxParser ); |
|
166 | + protected function closeSaxParser($xmlSaxParser) { |
|
167 | + xml_parser_free($xmlSaxParser); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * |
175 | 175 | * @return mixed |
176 | 176 | */ |
177 | - abstract protected function tagOpen( $parser, $name, $attr ); |
|
177 | + abstract protected function tagOpen($parser, $name, $attr); |
|
178 | 178 | |
179 | 179 | /** |
180 | 180 | * @param $parser |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return mixed |
184 | 184 | */ |
185 | - abstract protected function tagClose( $parser, $name ); |
|
185 | + abstract protected function tagClose($parser, $name); |
|
186 | 186 | |
187 | 187 | /** |
188 | 188 | * @param $parser |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | * |
191 | 191 | * @return mixed |
192 | 192 | */ |
193 | - abstract protected function characterData( $parser, $data ); |
|
193 | + abstract protected function characterData($parser, $data); |
|
194 | 194 | |
195 | 195 | /** |
196 | 196 | * postprocess escaped data and write to disk |
@@ -199,18 +199,18 @@ discard block |
||
199 | 199 | * @param string $data |
200 | 200 | * @param bool $treatAsCDATA |
201 | 201 | */ |
202 | - protected function postProcAndFlush( $fp, $data, $treatAsCDATA = false ) { |
|
202 | + protected function postProcAndFlush($fp, $data, $treatAsCDATA = false) { |
|
203 | 203 | //postprocess string |
204 | - $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data ); |
|
205 | - $data = str_replace( ' ', ' ', $data ); |
|
206 | - if ( !$treatAsCDATA ) { |
|
204 | + $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data); |
|
205 | + $data = str_replace(' ', ' ', $data); |
|
206 | + if (!$treatAsCDATA) { |
|
207 | 207 | //unix2dos |
208 | - $data = str_replace( "\r\n", "\r", $data ); |
|
209 | - $data = str_replace( "\n", "\r", $data ); |
|
210 | - $data = str_replace( "\r", "\r\n", $data ); |
|
208 | + $data = str_replace("\r\n", "\r", $data); |
|
209 | + $data = str_replace("\n", "\r", $data); |
|
210 | + $data = str_replace("\r", "\r\n", $data); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | //flush to disk |
214 | - fwrite( $fp, $data ); |
|
214 | + fwrite($fp, $data); |
|
215 | 215 | } |
216 | 216 | } |
@@ -31,17 +31,17 @@ discard block |
||
31 | 31 | ]; |
32 | 32 | |
33 | 33 | public function replaceTranslation() { |
34 | - fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' ); |
|
34 | + fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>'); |
|
35 | 35 | |
36 | 36 | //create Sax parser |
37 | 37 | $xmlParser = $this->initSaxParser(); |
38 | 38 | |
39 | - while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) { |
|
39 | + while ($this->currentBuffer = fread($this->originalFP, 4096)) { |
|
40 | 40 | /* |
41 | 41 | preprocess file |
42 | 42 | */ |
43 | 43 | // obfuscate entities because sax automatically does html_entity_decode |
44 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
44 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
45 | 45 | |
46 | 46 | //avoid cutting entities in half: |
47 | 47 | //the last fread could have truncated an entity (say, '<' in '&l'), thus invalidating the escaping |
@@ -53,84 +53,84 @@ discard block |
||
53 | 53 | // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end |
54 | 54 | //it can't be an entity, exit the loop |
55 | 55 | |
56 | - while ( true ) { |
|
57 | - $_ampPos = strpos( $temporary_check_buffer, '&' ); |
|
56 | + while (true) { |
|
57 | + $_ampPos = strpos($temporary_check_buffer, '&'); |
|
58 | 58 | |
59 | 59 | //check for real entity or escape it to safely exit from the loop!!! |
60 | - if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) { |
|
60 | + if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) { |
|
61 | 61 | $escape_AMP = true; |
62 | 62 | break; |
63 | 63 | } |
64 | 64 | |
65 | 65 | //if an entity is still present, fetch some more and repeat the escaping |
66 | - $this->currentBuffer .= fread( $this->originalFP, 9 ); |
|
67 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
66 | + $this->currentBuffer .= fread($this->originalFP, 9); |
|
67 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | //free stuff outside the loop |
71 | - unset( $temporary_check_buffer ); |
|
71 | + unset($temporary_check_buffer); |
|
72 | 72 | |
73 | - $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
74 | - if ( $escape_AMP ) { |
|
75 | - $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
73 | + $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
74 | + if ($escape_AMP) { |
|
75 | + $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | //get length of chunk |
79 | - $this->len = strlen( $this->currentBuffer ); |
|
79 | + $this->len = strlen($this->currentBuffer); |
|
80 | 80 | |
81 | 81 | //parse chunk of text |
82 | - if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) { |
|
82 | + if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) { |
|
83 | 83 | //if unable, raise an exception |
84 | - throw new RuntimeException( sprintf( |
|
84 | + throw new RuntimeException(sprintf( |
|
85 | 85 | "XML error: %s at line %d", |
86 | - xml_error_string( xml_get_error_code( $xmlParser ) ), |
|
87 | - xml_get_current_line_number( $xmlParser ) |
|
88 | - ) ); |
|
86 | + xml_error_string(xml_get_error_code($xmlParser)), |
|
87 | + xml_get_current_line_number($xmlParser) |
|
88 | + )); |
|
89 | 89 | } |
90 | 90 | //get accumulated this->offset in document: as long as SAX pointer advances, we keep track of total bytes it has seen so far; this way, we can translate its global pointer in an address local to the current buffer of text to retrieve last char of tag |
91 | 91 | $this->offset += $this->len; |
92 | 92 | } |
93 | 93 | |
94 | 94 | // close Sax parser |
95 | - $this->closeSaxParser( $xmlParser ); |
|
95 | + $this->closeSaxParser($xmlParser); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * @inheritDoc |
101 | 101 | */ |
102 | - protected function tagOpen( $parser, $name, $attr ) { |
|
102 | + protected function tagOpen($parser, $name, $attr) { |
|
103 | 103 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
104 | - if ( $this->tuTagName === $name ) { |
|
104 | + if ($this->tuTagName === $name) { |
|
105 | 105 | $this->inTU = true; |
106 | 106 | |
107 | 107 | // get id |
108 | 108 | // trim to first 100 characters because this is the limit on Matecat's DB |
109 | - $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
|
109 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
110 | 110 | |
111 | 111 | // `translate` attribute can be only yes or no |
112 | - if ( isset( $attr[ 'translate' ] ) && $attr[ 'translate' ] === 'no' ) { |
|
113 | - $attr[ 'translate' ] = 'no'; |
|
112 | + if (isset($attr['translate']) && $attr['translate'] === 'no') { |
|
113 | + $attr['translate'] = 'no'; |
|
114 | 114 | } else { |
115 | - $attr[ 'translate' ] = 'yes'; |
|
115 | + $attr['translate'] = 'yes'; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | // current 'translate' attribute of the current trans-unit |
119 | - $this->currentTransUnitTranslate = $attr[ 'translate' ]; |
|
119 | + $this->currentTransUnitTranslate = $attr['translate']; |
|
120 | 120 | } |
121 | 121 | |
122 | - if ( 'source' === $name ) { |
|
122 | + if ('source' === $name) { |
|
123 | 123 | $this->sourceAttributes = $attr; |
124 | 124 | } |
125 | 125 | |
126 | - if ( 'mda:metadata' === $name ) { |
|
126 | + if ('mda:metadata' === $name) { |
|
127 | 127 | $this->unitContainsMda = true; |
128 | 128 | } |
129 | 129 | |
130 | 130 | // check if we are entering into a <target> |
131 | - if ( 'target' === $name ) { |
|
131 | + if ('target' === $name) { |
|
132 | 132 | |
133 | - if ( $this->currentTransUnitTranslate === 'no' ) { |
|
133 | + if ($this->currentTransUnitTranslate === 'no') { |
|
134 | 134 | $this->inTarget = false; |
135 | 135 | } else { |
136 | 136 | $this->inTarget = true; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
141 | 141 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
142 | - if ( !$this->inTarget ) { |
|
142 | + if (!$this->inTarget) { |
|
143 | 143 | |
144 | 144 | $tag = ''; |
145 | 145 | |
@@ -161,14 +161,14 @@ discard block |
||
161 | 161 | // |
162 | 162 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
163 | 163 | // |
164 | - if ( $this->xliffVersion === 2 && ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) && $this->unitContainsMda === false ) { |
|
165 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) && !empty( $this->transUnits[ $this->currentTransUnitId ] ) && !$this->hasWrittenCounts ) { |
|
164 | + if ($this->xliffVersion === 2 && ($name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable') && $this->unitContainsMda === false) { |
|
165 | + if (isset($this->transUnits[$this->currentTransUnitId]) && !empty($this->transUnits[$this->currentTransUnitId]) && !$this->hasWrittenCounts) { |
|
166 | 166 | |
167 | 167 | // we need to update counts here |
168 | 168 | $this->updateCounts(); |
169 | 169 | $this->hasWrittenCounts = true; |
170 | 170 | |
171 | - $tag .= $this->getWordCountGroupForXliffV2( $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
|
171 | + $tag .= $this->getWordCountGroupForXliffV2($this->counts['raw_word_count'], $this->counts['eq_word_count']); |
|
172 | 172 | $this->unitContainsMda = true; |
173 | 173 | } |
174 | 174 | } |
@@ -179,88 +179,88 @@ discard block |
||
179 | 179 | $lastMrkState = null; |
180 | 180 | $stateProp = ''; |
181 | 181 | |
182 | - foreach ( $attr as $k => $v ) { |
|
182 | + foreach ($attr as $k => $v) { |
|
183 | 183 | |
184 | 184 | //if tag name is file, we must replace the target-language attribute |
185 | - if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
|
185 | + if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) { |
|
186 | 186 | //replace Target language with job language provided from constructor |
187 | 187 | $tag .= "$k=\"$this->targetLang\" "; |
188 | 188 | } else { |
189 | 189 | $pos = 0; |
190 | - if ( $this->currentTransUnitId ) { |
|
191 | - $pos = current( $this->transUnits[ $this->currentTransUnitId ] ); |
|
190 | + if ($this->currentTransUnitId) { |
|
191 | + $pos = current($this->transUnits[$this->currentTransUnitId]); |
|
192 | 192 | } |
193 | 193 | |
194 | - if ( $name === $this->tuTagName ) { |
|
194 | + if ($name === $this->tuTagName) { |
|
195 | 195 | |
196 | - $sid = $this->segments[ $pos ][ 'sid' ]; |
|
196 | + $sid = $this->segments[$pos]['sid']; |
|
197 | 197 | |
198 | 198 | // add `help-id` to xliff v.1* |
199 | 199 | // add `mtc:segment-id` to xliff v.2* |
200 | - if ( $this->xliffVersion === 1 && strpos( $tag, 'help-id' ) === false ) { |
|
201 | - if ( !empty( $sid ) ) { |
|
200 | + if ($this->xliffVersion === 1 && strpos($tag, 'help-id') === false) { |
|
201 | + if (!empty($sid)) { |
|
202 | 202 | $tag .= "help-id=\"$sid\" "; |
203 | 203 | } |
204 | - } elseif ( $this->xliffVersion === 2 && strpos( $tag, 'mtc:segment-id' ) === false ) { |
|
205 | - if ( !empty( $sid ) ) { |
|
204 | + } elseif ($this->xliffVersion === 2 && strpos($tag, 'mtc:segment-id') === false) { |
|
205 | + if (!empty($sid)) { |
|
206 | 206 | $tag .= "mtc:segment-id=\"$sid\" "; |
207 | 207 | } |
208 | 208 | } |
209 | 209 | |
210 | - } elseif ( 'segment' === $name && $this->xliffVersion === 2 ) { // add state to segment in Xliff v2 |
|
211 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $this->segments[ $pos ], $stateProp, $lastMrkState ); |
|
210 | + } elseif ('segment' === $name && $this->xliffVersion === 2) { // add state to segment in Xliff v2 |
|
211 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($this->segments[$pos], $stateProp, $lastMrkState); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | //normal tag flux, put attributes in it |
215 | 215 | $tag .= "$k=\"$v\" "; |
216 | 216 | |
217 | 217 | // replace state for xliff v2 |
218 | - if ( $stateProp ) { |
|
218 | + if ($stateProp) { |
|
219 | 219 | $pattern = '/state=\"(.*)\"/i'; |
220 | - $tag = preg_replace( $pattern, $stateProp, $tag ); |
|
220 | + $tag = preg_replace($pattern, $stateProp, $tag); |
|
221 | 221 | } |
222 | 222 | } |
223 | 223 | } |
224 | 224 | |
225 | 225 | // add oasis xliff 20 namespace |
226 | - if ( $this->xliffVersion === 2 && $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
|
226 | + if ($this->xliffVersion === 2 && $name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) { |
|
227 | 227 | $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
228 | 228 | } |
229 | 229 | |
230 | 230 | // add MateCat specific namespace, we want maybe add non-XLIFF attributes |
231 | - if ( $name === 'xliff' && !array_key_exists( 'xmlns:mtc', $attr ) ) { |
|
231 | + if ($name === 'xliff' && !array_key_exists('xmlns:mtc', $attr)) { |
|
232 | 232 | $tag .= ' xmlns:mtc="https://www.matecat.com" '; |
233 | 233 | } |
234 | 234 | |
235 | 235 | // trgLang |
236 | - if ( $name === 'xliff' ) { |
|
237 | - $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag ); |
|
236 | + if ($name === 'xliff') { |
|
237 | + $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag); |
|
238 | 238 | } |
239 | 239 | |
240 | 240 | //this logic helps detecting empty tags |
241 | 241 | //get current position of SAX pointer in all the stream of data is has read so far: |
242 | 242 | //it points at the end of current tag |
243 | - $idx = xml_get_current_byte_index( $parser ); |
|
243 | + $idx = xml_get_current_byte_index($parser); |
|
244 | 244 | |
245 | 245 | //check whether the bounds of current tag are entirely in current buffer or the end of the current tag |
246 | 246 | //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
247 | 247 | //this check is necessary because we may have truncated a tag in half with current read, |
248 | 248 | //and the other half may be encountered in the next buffer it will be passed |
249 | - if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) { |
|
249 | + if (isset($this->currentBuffer[$idx - $this->offset])) { |
|
250 | 250 | //if this tag entire lenght fitted in the buffer, the last char must be the last |
251 | 251 | //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
252 | - $lastChar = $this->currentBuffer[ $idx - $this->offset ]; |
|
252 | + $lastChar = $this->currentBuffer[$idx - $this->offset]; |
|
253 | 253 | } else { |
254 | 254 | //if it's out, simple use the last character of the chunk |
255 | - $lastChar = $this->currentBuffer[ $this->len - 1 ]; |
|
255 | + $lastChar = $this->currentBuffer[$this->len - 1]; |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | //trim last space |
259 | - $tag = rtrim( $tag ); |
|
259 | + $tag = rtrim($tag); |
|
260 | 260 | |
261 | 261 | //detect empty tag |
262 | - $this->isEmpty = ( $lastChar == '/' || $name == 'x' ); |
|
263 | - if ( $this->isEmpty ) { |
|
262 | + $this->isEmpty = ($lastChar == '/' || $name == 'x'); |
|
263 | + if ($this->isEmpty) { |
|
264 | 264 | $tag .= '/'; |
265 | 265 | } |
266 | 266 | |
@@ -268,29 +268,29 @@ discard block |
||
268 | 268 | $tag .= ">"; |
269 | 269 | |
270 | 270 | //set a a Buffer for the segSource Source tag |
271 | - if ( $this->bufferIsActive || in_array( $name, $this->nodesToCopy ) ) { // we are opening a critical CDATA section |
|
271 | + if ($this->bufferIsActive || in_array($name, $this->nodesToCopy)) { // we are opening a critical CDATA section |
|
272 | 272 | |
273 | 273 | //WARNING BECAUSE SOURCE AND SEG-SOURCE TAGS CAN BE EMPTY IN SOME CASES!!!!! |
274 | 274 | //so check for isEmpty also in conjunction with name |
275 | - if ( $this->isEmpty && ( 'source' === $name || 'seg-source' === $name ) ) { |
|
276 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
275 | + if ($this->isEmpty && ('source' === $name || 'seg-source' === $name)) { |
|
276 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
277 | 277 | } else { |
278 | 278 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
279 | 279 | $this->bufferIsActive = true; |
280 | - $this->CDATABuffer .= $tag; |
|
280 | + $this->CDATABuffer .= $tag; |
|
281 | 281 | } |
282 | 282 | } else { |
283 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
283 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
284 | 284 | } |
285 | 285 | } |
286 | 286 | |
287 | 287 | // update segmentPositionInTu |
288 | 288 | |
289 | - if ( $this->xliffVersion === 1 && $this->inTU && $name === 'source' ) { |
|
289 | + if ($this->xliffVersion === 1 && $this->inTU && $name === 'source') { |
|
290 | 290 | $this->segmentPositionInTu++; |
291 | 291 | } |
292 | 292 | |
293 | - if ( $this->xliffVersion === 2 && $this->inTU && $name === 'segment' ) { |
|
293 | + if ($this->xliffVersion === 2 && $this->inTU && $name === 'segment') { |
|
294 | 294 | $this->segmentPositionInTu++; |
295 | 295 | } |
296 | 296 | } |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | /** |
299 | 299 | * @inheritDoc |
300 | 300 | */ |
301 | - protected function tagClose( $parser, $name ) { |
|
301 | + protected function tagClose($parser, $name) { |
|
302 | 302 | $tag = ''; |
303 | 303 | |
304 | 304 | /** |
@@ -307,26 +307,26 @@ discard block |
||
307 | 307 | * |
308 | 308 | * self::tagOpen method |
309 | 309 | */ |
310 | - if ( !$this->isEmpty && !( $this->inTarget && $name !== 'target' ) ) { |
|
310 | + if (!$this->isEmpty && !($this->inTarget && $name !== 'target')) { |
|
311 | 311 | |
312 | - if ( !$this->inTarget ) { |
|
312 | + if (!$this->inTarget) { |
|
313 | 313 | $tag = "</$name>"; |
314 | 314 | } |
315 | 315 | |
316 | - if ( 'target' == $name ) { |
|
316 | + if ('target' == $name) { |
|
317 | 317 | |
318 | - if ( $this->currentTransUnitTranslate === 'no' ) { |
|
318 | + if ($this->currentTransUnitTranslate === 'no') { |
|
319 | 319 | // do nothing |
320 | - } elseif ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
320 | + } elseif (isset($this->transUnits[$this->currentTransUnitId])) { |
|
321 | 321 | |
322 | 322 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
323 | 323 | // actually there may be more that one segment to that ID if there are two mrk of the same source segment |
324 | 324 | |
325 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
325 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
326 | 326 | |
327 | 327 | // $currentSegmentId |
328 | - if ( !empty( $listOfSegmentsIds ) ) { |
|
329 | - $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
|
328 | + if (!empty($listOfSegmentsIds)) { |
|
329 | + $this->setCurrentSegmentArray($listOfSegmentsIds); |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | /* |
@@ -343,12 +343,12 @@ discard block |
||
343 | 343 | $this->lastTransUnit = []; |
344 | 344 | |
345 | 345 | $last_value = null; |
346 | - $segmentsCount = count( $listOfSegmentsIds ); |
|
347 | - for ( $i = 0; $i < $segmentsCount; $i++ ) { |
|
348 | - $id = $listOfSegmentsIds[ $i ]; |
|
349 | - if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
|
350 | - $last_value = $listOfSegmentsIds[ $i ]; |
|
351 | - $this->lastTransUnit[] = $this->segments[ $id ]; |
|
346 | + $segmentsCount = count($listOfSegmentsIds); |
|
347 | + for ($i = 0; $i < $segmentsCount; $i++) { |
|
348 | + $id = $listOfSegmentsIds[$i]; |
|
349 | + if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) { |
|
350 | + $last_value = $listOfSegmentsIds[$i]; |
|
351 | + $this->lastTransUnit[] = $this->segments[$id]; |
|
352 | 352 | } |
353 | 353 | } |
354 | 354 | |
@@ -360,27 +360,27 @@ discard block |
||
360 | 360 | // we must reset the lastMrkId found because this is a new segment. |
361 | 361 | $lastMrkId = -1; |
362 | 362 | |
363 | - if ( $this->xliffVersion === 2 ) { |
|
364 | - $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
|
363 | + if ($this->xliffVersion === 2) { |
|
364 | + $seg = $this->segments[$this->currentSegmentArray['sid']]; |
|
365 | 365 | |
366 | 366 | // update counts |
367 | - if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
|
368 | - $this->updateSegmentCounts( $seg ); |
|
367 | + if (!$this->hasWrittenCounts && !empty($seg)) { |
|
368 | + $this->updateSegmentCounts($seg); |
|
369 | 369 | } |
370 | 370 | |
371 | 371 | // delete translations so the prepareSegment |
372 | 372 | // will put source content in target tag |
373 | - if ( $this->sourceInTarget ) { |
|
374 | - $seg[ 'translation' ] = ''; |
|
373 | + if ($this->sourceInTarget) { |
|
374 | + $seg['translation'] = ''; |
|
375 | 375 | $this->resetCounts(); |
376 | 376 | } |
377 | 377 | |
378 | 378 | // append $translation |
379 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
379 | + $translation = $this->prepareTranslation($seg, $translation); |
|
380 | 380 | |
381 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
381 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
382 | 382 | } else { |
383 | - foreach ( $listOfSegmentsIds as $pos => $id ) { |
|
383 | + foreach ($listOfSegmentsIds as $pos => $id) { |
|
384 | 384 | |
385 | 385 | /* |
386 | 386 | * This routine works to respect the positional orders of markers. |
@@ -393,8 +393,8 @@ discard block |
||
393 | 393 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
394 | 394 | * If it is null leave it NULL |
395 | 395 | */ |
396 | - if ( (int)$this->segments[ $id ][ "mrk_id" ] < 0 && $this->segments[ $id ][ "mrk_id" ] !== null ) { |
|
397 | - $this->segments[ $id ][ "mrk_id" ] = 0; |
|
396 | + if ((int)$this->segments[$id]["mrk_id"] < 0 && $this->segments[$id]["mrk_id"] !== null) { |
|
397 | + $this->segments[$id]["mrk_id"] = 0; |
|
398 | 398 | } |
399 | 399 | |
400 | 400 | /* |
@@ -403,65 +403,65 @@ discard block |
||
403 | 403 | * ( null <= -1 ) === true |
404 | 404 | * so, cast to int |
405 | 405 | */ |
406 | - if ( (int)$this->segments[ $id ][ "mrk_id" ] <= $lastMrkId ) { |
|
406 | + if ((int)$this->segments[$id]["mrk_id"] <= $lastMrkId) { |
|
407 | 407 | break; |
408 | 408 | } |
409 | 409 | |
410 | 410 | // set $this->currentSegment |
411 | - $seg = $this->segments[ $id ]; |
|
411 | + $seg = $this->segments[$id]; |
|
412 | 412 | |
413 | 413 | // update counts |
414 | - if ( !empty( $seg ) ) { |
|
415 | - $this->updateSegmentCounts( $seg ); |
|
414 | + if (!empty($seg)) { |
|
415 | + $this->updateSegmentCounts($seg); |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | // delete translations so the prepareSegment |
419 | 419 | // will put source content in target tag |
420 | - if ( $this->sourceInTarget ) { |
|
421 | - $seg[ 'translation' ] = ''; |
|
420 | + if ($this->sourceInTarget) { |
|
421 | + $seg['translation'] = ''; |
|
422 | 422 | $this->resetCounts(); |
423 | 423 | } |
424 | 424 | |
425 | 425 | // append $translation |
426 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
426 | + $translation = $this->prepareTranslation($seg, $translation); |
|
427 | 427 | |
428 | 428 | // for xliff 2 we need $this->transUnits[ $this->currentId ] [ $pos ] for populating metadata |
429 | 429 | |
430 | - unset( $this->transUnits[ $this->currentTransUnitId ] [ $pos ] ); |
|
430 | + unset($this->transUnits[$this->currentTransUnitId] [$pos]); |
|
431 | 431 | |
432 | - $lastMrkId = $this->segments[ $id ][ "mrk_id" ]; |
|
432 | + $lastMrkId = $this->segments[$id]["mrk_id"]; |
|
433 | 433 | |
434 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
434 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
435 | 435 | } |
436 | 436 | } |
437 | 437 | |
438 | 438 | //append translation |
439 | 439 | $targetLang = ''; |
440 | - if ( $this->xliffVersion === 1 ) { |
|
440 | + if ($this->xliffVersion === 1) { |
|
441 | 441 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
442 | 442 | } |
443 | 443 | |
444 | - $tag = $this->buildTranslateTag( $targetLang, $stateProp, $translation, $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
|
444 | + $tag = $this->buildTranslateTag($targetLang, $stateProp, $translation, $this->counts['raw_word_count'], $this->counts['eq_word_count']); |
|
445 | 445 | } |
446 | 446 | |
447 | 447 | // signal we are leaving a target |
448 | 448 | $this->targetWasWritten = true; |
449 | 449 | $this->inTarget = false; |
450 | - $this->postProcAndFlush( $this->outputFP, $tag, $treatAsCDATA = true ); |
|
451 | - } elseif ( in_array( $name, $this->nodesToCopy ) ) { // we are closing a critical CDATA section |
|
450 | + $this->postProcAndFlush($this->outputFP, $tag, $treatAsCDATA = true); |
|
451 | + } elseif (in_array($name, $this->nodesToCopy)) { // we are closing a critical CDATA section |
|
452 | 452 | |
453 | 453 | $this->bufferIsActive = false; |
454 | 454 | |
455 | 455 | // only for Xliff 2.* |
456 | 456 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
457 | - if ( 'mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts ) { |
|
457 | + if ('mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts) { |
|
458 | 458 | |
459 | 459 | // we need to update counts here |
460 | 460 | $this->updateCounts(); |
461 | 461 | $this->hasWrittenCounts = true; |
462 | 462 | |
463 | 463 | $tag = $this->CDATABuffer; |
464 | - $tag .= $this->getWordCountGroupForXliffV2( $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ], false ); |
|
464 | + $tag .= $this->getWordCountGroupForXliffV2($this->counts['raw_word_count'], $this->counts['eq_word_count'], false); |
|
465 | 465 | $tag .= " </mda:metadata>"; |
466 | 466 | |
467 | 467 | } else { |
@@ -471,59 +471,59 @@ discard block |
||
471 | 471 | $this->CDATABuffer = ""; |
472 | 472 | |
473 | 473 | //flush to pointer |
474 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
475 | - } elseif ( 'segment' === $name ) { |
|
474 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
475 | + } elseif ('segment' === $name) { |
|
476 | 476 | |
477 | 477 | // only for Xliff 2.* |
478 | 478 | // if segment has no <target> add it BEFORE </segment> |
479 | - if ( $this->xliffVersion === 2 && !$this->targetWasWritten ) { |
|
479 | + if ($this->xliffVersion === 2 && !$this->targetWasWritten) { |
|
480 | 480 | |
481 | 481 | $seg = $this->getCurrentSegment(); |
482 | 482 | |
483 | 483 | // copy attr from <source> |
484 | 484 | $tag = '<target'; |
485 | - foreach ( $this->sourceAttributes as $k => $v ) { |
|
485 | + foreach ($this->sourceAttributes as $k => $v) { |
|
486 | 486 | $tag .= " $k=\"$v\""; |
487 | 487 | } |
488 | 488 | |
489 | - $tag .= '>' . $seg[ 'translation' ] . '</target></segment>'; |
|
489 | + $tag .= '>' . $seg['translation'] . '</target></segment>'; |
|
490 | 490 | } |
491 | 491 | |
492 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
492 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
493 | 493 | |
494 | 494 | // we are leaving <segment>, reset $segmentHasTarget |
495 | 495 | $this->targetWasWritten = false; |
496 | 496 | |
497 | - } elseif ( $name === 'trans-unit' ) { |
|
497 | + } elseif ($name === 'trans-unit') { |
|
498 | 498 | |
499 | 499 | // only for Xliff 1.* |
500 | 500 | // handling </trans-unit> closure |
501 | - if ( !$this->targetWasWritten ) { |
|
501 | + if (!$this->targetWasWritten) { |
|
502 | 502 | $seg = $this->getCurrentSegment(); |
503 | 503 | $lastMrkState = null; |
504 | 504 | $stateProp = ''; |
505 | 505 | $tag = ''; |
506 | 506 | |
507 | 507 | // if there is translation available insert <target> BEFORE </trans-unit> |
508 | - if ( isset( $seg[ 'translation' ] ) ) { |
|
509 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
510 | - $tag .= $this->createTargetTag( $seg[ 'translation' ], $stateProp ); |
|
508 | + if (isset($seg['translation'])) { |
|
509 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
510 | + $tag .= $this->createTargetTag($seg['translation'], $stateProp); |
|
511 | 511 | } |
512 | 512 | |
513 | 513 | $tag .= '</trans-unit>'; |
514 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
514 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
515 | 515 | } else { |
516 | - $this->postProcAndFlush( $this->outputFP, '</trans-unit>' ); |
|
516 | + $this->postProcAndFlush($this->outputFP, '</trans-unit>'); |
|
517 | 517 | } |
518 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
518 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
519 | 519 | $this->CDATABuffer .= "</$name>"; |
520 | 520 | // Do NOT Flush |
521 | 521 | } else { //generic tag closure do Nothing |
522 | 522 | // flush to pointer |
523 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
523 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
524 | 524 | } |
525 | - } elseif ( $this->CDATABuffer === '<note/>' && $this->bufferIsActive === true ) { |
|
526 | - $this->postProcAndFlush( $this->outputFP, '<note/>' ); |
|
525 | + } elseif ($this->CDATABuffer === '<note/>' && $this->bufferIsActive === true) { |
|
526 | + $this->postProcAndFlush($this->outputFP, '<note/>'); |
|
527 | 527 | $this->bufferIsActive = false; |
528 | 528 | $this->CDATABuffer = ''; |
529 | 529 | $this->isEmpty = false; |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | } |
534 | 534 | |
535 | 535 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
536 | - if ( $this->tuTagName === $name ) { |
|
536 | + if ($this->tuTagName === $name) { |
|
537 | 537 | $this->currentTransUnitTranslate = null; |
538 | 538 | $this->inTU = false; |
539 | 539 | $this->segmentPositionInTu = -1; |
@@ -548,21 +548,21 @@ discard block |
||
548 | 548 | * |
549 | 549 | * @param array $listOfSegmentsIds |
550 | 550 | */ |
551 | - private function setCurrentSegmentArray( array $listOfSegmentsIds = [] ) { |
|
551 | + private function setCurrentSegmentArray(array $listOfSegmentsIds = []) { |
|
552 | 552 | // $currentSegmentId |
553 | - if ( empty( $this->currentSegmentArray ) ) { |
|
553 | + if (empty($this->currentSegmentArray)) { |
|
554 | 554 | $this->currentSegmentArray = [ |
555 | - 'sid' => $listOfSegmentsIds[ 0 ], |
|
555 | + 'sid' => $listOfSegmentsIds[0], |
|
556 | 556 | 'tid' => $this->currentTransUnitId, |
557 | 557 | ]; |
558 | 558 | } else { |
559 | - if ( $this->currentSegmentArray[ 'tid' ] === $this->currentTransUnitId ) { |
|
560 | - $key = array_search( $this->currentSegmentArray[ 'sid' ], $listOfSegmentsIds ); |
|
561 | - $this->currentSegmentArray[ 'sid' ] = $listOfSegmentsIds[ $key + 1 ]; |
|
562 | - $this->currentSegmentArray[ 'tid' ] = $this->currentTransUnitId; |
|
559 | + if ($this->currentSegmentArray['tid'] === $this->currentTransUnitId) { |
|
560 | + $key = array_search($this->currentSegmentArray['sid'], $listOfSegmentsIds); |
|
561 | + $this->currentSegmentArray['sid'] = $listOfSegmentsIds[$key + 1]; |
|
562 | + $this->currentSegmentArray['tid'] = $this->currentTransUnitId; |
|
563 | 563 | } else { |
564 | 564 | $this->currentSegmentArray = [ |
565 | - 'sid' => $listOfSegmentsIds[ 0 ], |
|
565 | + 'sid' => $listOfSegmentsIds[0], |
|
566 | 566 | 'tid' => $this->currentTransUnitId, |
567 | 567 | ]; |
568 | 568 | } |
@@ -574,23 +574,23 @@ discard block |
||
574 | 574 | */ |
575 | 575 | private function updateCounts() { |
576 | 576 | // populate counts |
577 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
577 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
578 | 578 | |
579 | 579 | // $currentSegmentId |
580 | - if ( !empty( $listOfSegmentsIds ) ) { |
|
581 | - $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
|
580 | + if (!empty($listOfSegmentsIds)) { |
|
581 | + $this->setCurrentSegmentArray($listOfSegmentsIds); |
|
582 | 582 | } |
583 | 583 | |
584 | - if ( $this->xliffVersion === 2 ) { |
|
585 | - $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
|
586 | - if ( !empty( $seg ) ) { |
|
587 | - $this->updateSegmentCounts( $seg ); |
|
584 | + if ($this->xliffVersion === 2) { |
|
585 | + $seg = $this->segments[$this->currentSegmentArray['sid']]; |
|
586 | + if (!empty($seg)) { |
|
587 | + $this->updateSegmentCounts($seg); |
|
588 | 588 | } |
589 | 589 | } else { |
590 | - foreach ( $listOfSegmentsIds as $pos => $id ) { |
|
591 | - $seg = $this->segments[ $id ]; |
|
592 | - if ( !empty( $seg ) ) { |
|
593 | - $this->updateSegmentCounts( $seg ); |
|
590 | + foreach ($listOfSegmentsIds as $pos => $id) { |
|
591 | + $seg = $this->segments[$id]; |
|
592 | + if (!empty($seg)) { |
|
593 | + $this->updateSegmentCounts($seg); |
|
594 | 594 | } |
595 | 595 | } |
596 | 596 | } |
@@ -601,14 +601,14 @@ discard block |
||
601 | 601 | /** |
602 | 602 | * @param array $seg |
603 | 603 | */ |
604 | - private function updateSegmentCounts( array $seg = [] ) { |
|
605 | - $this->counts[ 'raw_word_count' ] += $seg[ 'raw_word_count' ]; |
|
606 | - $this->counts[ 'eq_word_count' ] += ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 ); |
|
604 | + private function updateSegmentCounts(array $seg = []) { |
|
605 | + $this->counts['raw_word_count'] += $seg['raw_word_count']; |
|
606 | + $this->counts['eq_word_count'] += (floor($seg['eq_word_count'] * 100) / 100); |
|
607 | 607 | } |
608 | 608 | |
609 | 609 | private function resetCounts() { |
610 | - $this->counts[ 'raw_word_count' ] = 0; |
|
611 | - $this->counts[ 'eq_word_count' ] = 0; |
|
610 | + $this->counts['raw_word_count'] = 0; |
|
611 | + $this->counts['eq_word_count'] = 0; |
|
612 | 612 | } |
613 | 613 | |
614 | 614 | /** |
@@ -619,37 +619,37 @@ discard block |
||
619 | 619 | * |
620 | 620 | * @return string |
621 | 621 | */ |
622 | - protected function prepareTranslation( $seg, $transUnitTranslation = "" ) { |
|
622 | + protected function prepareTranslation($seg, $transUnitTranslation = "") { |
|
623 | 623 | $endTags = ""; |
624 | 624 | |
625 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
626 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
627 | - $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) && $seg[ 'data_ref_map' ] !== null ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
|
625 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
626 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
627 | + $dataRefMap = (isset($seg['data_ref_map']) && $seg['data_ref_map'] !== null) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
|
628 | 628 | |
629 | - if ( is_null( $seg [ 'translation' ] ) || $seg [ 'translation' ] == '' ) { |
|
629 | + if (is_null($seg ['translation']) || $seg ['translation'] == '') { |
|
630 | 630 | $translation = $segment; |
631 | 631 | } else { |
632 | - if ( $this->callback ) { |
|
633 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap ) ) { |
|
632 | + if ($this->callback) { |
|
633 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap)) { |
|
634 | 634 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
635 | 635 | } |
636 | 636 | } |
637 | 637 | } |
638 | 638 | |
639 | 639 | // for xliff v2 we ignore the marks on purpose |
640 | - if ( $this->xliffVersion === 2 ) { |
|
640 | + if ($this->xliffVersion === 2) { |
|
641 | 641 | return $translation; |
642 | 642 | } |
643 | 643 | |
644 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
645 | - if ( $this->targetLang === 'ja-JP' ) { |
|
646 | - $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] ); |
|
644 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
645 | + if ($this->targetLang === 'ja-JP') { |
|
646 | + $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']); |
|
647 | 647 | } |
648 | 648 | |
649 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
|
649 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . $translation . $seg['mrk_succ_tags'] . "</mrk>"; |
|
650 | 650 | } |
651 | 651 | |
652 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . $seg[ 'succ_tags' ]; |
|
652 | + $transUnitTranslation .= $seg['prev_tags'] . $translation . $endTags . $seg['succ_tags']; |
|
653 | 653 | |
654 | 654 | return $transUnitTranslation; |
655 | 655 | } |
@@ -663,15 +663,15 @@ discard block |
||
663 | 663 | * |
664 | 664 | * @return string |
665 | 665 | */ |
666 | - private function buildTranslateTag( $targetLang, $stateProp, $translation, $rawWordCount, $eqWordCount ) { |
|
667 | - switch ( $this->xliffVersion ) { |
|
666 | + private function buildTranslateTag($targetLang, $stateProp, $translation, $rawWordCount, $eqWordCount) { |
|
667 | + switch ($this->xliffVersion) { |
|
668 | 668 | case 1: |
669 | 669 | default: |
670 | 670 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
671 | 671 | |
672 | 672 | // if it's a Trados file don't append count group |
673 | - if ( get_class( $this ) !== SdlXliffSAXTranslationReplacer::class ) { |
|
674 | - $tag .= $this->getWordCountGroup( $rawWordCount, $eqWordCount ); |
|
673 | + if (get_class($this) !== SdlXliffSAXTranslationReplacer::class) { |
|
674 | + $tag .= $this->getWordCountGroup($rawWordCount, $eqWordCount); |
|
675 | 675 | } |
676 | 676 | |
677 | 677 | return $tag; |
@@ -687,7 +687,7 @@ discard block |
||
687 | 687 | * |
688 | 688 | * @return string |
689 | 689 | */ |
690 | - private function getWordCountGroup( $raw_word_count, $eq_word_count ) { |
|
690 | + private function getWordCountGroup($raw_word_count, $eq_word_count) { |
|
691 | 691 | return "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">$raw_word_count</count><count count-type=\"x-matecat-weighted\">$eq_word_count</count></count-group>"; |
692 | 692 | } |
693 | 693 | |
@@ -695,11 +695,11 @@ discard block |
||
695 | 695 | * @return array |
696 | 696 | */ |
697 | 697 | private function getCurrentSegment() { |
698 | - if ( $this->currentTransUnitTranslate === 'yes' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
699 | - $index = $this->transUnits[ $this->currentTransUnitId ][ $this->segmentPositionInTu ]; |
|
698 | + if ($this->currentTransUnitTranslate === 'yes' && isset($this->transUnits[$this->currentTransUnitId])) { |
|
699 | + $index = $this->transUnits[$this->currentTransUnitId][$this->segmentPositionInTu]; |
|
700 | 700 | |
701 | - if ( isset( $this->segments[ $index ] ) ) { |
|
702 | - return $this->segments[ $index ]; |
|
701 | + if (isset($this->segments[$index])) { |
|
702 | + return $this->segments[$index]; |
|
703 | 703 | } |
704 | 704 | } |
705 | 705 | |
@@ -714,10 +714,10 @@ discard block |
||
714 | 714 | * |
715 | 715 | * @return string |
716 | 716 | */ |
717 | - private function createTargetTag( $translation, $stateProp ) { |
|
717 | + private function createTargetTag($translation, $stateProp) { |
|
718 | 718 | $targetLang = 'xml:lang="' . $this->targetLang . '"'; |
719 | 719 | |
720 | - return $this->buildTranslateTag( $targetLang, $stateProp, $translation, $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
|
720 | + return $this->buildTranslateTag($targetLang, $stateProp, $translation, $this->counts['raw_word_count'], $this->counts['eq_word_count']); |
|
721 | 721 | } |
722 | 722 | |
723 | 723 | /** |
@@ -727,11 +727,11 @@ discard block |
||
727 | 727 | * |
728 | 728 | * @return string |
729 | 729 | */ |
730 | - private function getWordCountGroupForXliffV2( $raw_word_count, $eq_word_count, $withMetadataTag = true ) { |
|
730 | + private function getWordCountGroupForXliffV2($raw_word_count, $eq_word_count, $withMetadataTag = true) { |
|
731 | 731 | $this->mdaGroupCounter++; |
732 | 732 | $id = 'word_count_tu_' . $this->mdaGroupCounter; |
733 | 733 | |
734 | - if ( $withMetadataTag === false ) { |
|
734 | + if ($withMetadataTag === false) { |
|
735 | 735 | return " <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\"> |
736 | 736 | <mda:meta type=\"x-matecat-raw\">$raw_word_count</mda:meta> |
737 | 737 | <mda:meta type=\"x-matecat-weighted\">$eq_word_count</mda:meta> |
@@ -754,19 +754,19 @@ discard block |
||
754 | 754 | * |
755 | 755 | * @return array |
756 | 756 | */ |
757 | - private function setTransUnitState( $seg, $state_prop, $lastMrkState ) { |
|
758 | - switch ( $seg[ 'status' ] ) { |
|
757 | + private function setTransUnitState($seg, $state_prop, $lastMrkState) { |
|
758 | + switch ($seg['status']) { |
|
759 | 759 | |
760 | 760 | case TranslationStatus::STATUS_FIXED: |
761 | 761 | case TranslationStatus::STATUS_APPROVED: |
762 | - if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
|
763 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\""; |
|
762 | + if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED) { |
|
763 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"reviewed\"" : "state=\"signed-off\""; |
|
764 | 764 | $lastMrkState = TranslationStatus::STATUS_APPROVED; |
765 | 765 | } |
766 | 766 | break; |
767 | 767 | |
768 | 768 | case TranslationStatus::STATUS_TRANSLATED: |
769 | - if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
|
769 | + if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED) { |
|
770 | 770 | $state_prop = "state=\"translated\""; |
771 | 771 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
772 | 772 | } |
@@ -774,15 +774,15 @@ discard block |
||
774 | 774 | |
775 | 775 | case TranslationStatus::STATUS_REJECTED: // if there is a mark REJECTED and there is not a DRAFT, all the trans-unit is REJECTED. In V2 there is no way to mark |
776 | 776 | case TranslationStatus::STATUS_REBUTTED: |
777 | - if ( ( $lastMrkState == null ) || ( $lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT ) ) { |
|
778 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\""; |
|
777 | + if (($lastMrkState == null) || ($lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT)) { |
|
778 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"initial\"" : "state=\"needs-review-translation\""; |
|
779 | 779 | $lastMrkState = TranslationStatus::STATUS_REJECTED; |
780 | 780 | } |
781 | 781 | break; |
782 | 782 | |
783 | 783 | case TranslationStatus::STATUS_NEW: |
784 | - if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) { |
|
785 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\""; |
|
784 | + if (($lastMrkState == null) || $lastMrkState != TranslationStatus::STATUS_DRAFT) { |
|
785 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\""; |
|
786 | 786 | $lastMrkState = TranslationStatus::STATUS_NEW; |
787 | 787 | } |
788 | 788 | break; |
@@ -795,7 +795,7 @@ discard block |
||
795 | 795 | // this is the case when a segment is not showed in cattool, so the row in |
796 | 796 | // segment_translations does not exists and |
797 | 797 | // ---> $seg[ 'status' ] is NULL |
798 | - if ( $lastMrkState == null ) { //this is the first MRK ID |
|
798 | + if ($lastMrkState == null) { //this is the first MRK ID |
|
799 | 799 | $state_prop = "state=\"translated\""; |
800 | 800 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
801 | 801 | } else { |
@@ -804,17 +804,17 @@ discard block |
||
804 | 804 | break; |
805 | 805 | } |
806 | 806 | |
807 | - return [ $state_prop, $lastMrkState ]; |
|
807 | + return [$state_prop, $lastMrkState]; |
|
808 | 808 | } |
809 | 809 | |
810 | 810 | /** |
811 | 811 | * @inheritDoc |
812 | 812 | */ |
813 | - protected function characterData( $parser, $data ) { |
|
813 | + protected function characterData($parser, $data) { |
|
814 | 814 | // don't write <target> data |
815 | - if ( !$this->inTarget && !$this->bufferIsActive ) { |
|
816 | - $this->postProcAndFlush( $this->outputFP, $data ); |
|
817 | - } elseif ( $this->bufferIsActive ) { |
|
815 | + if (!$this->inTarget && !$this->bufferIsActive) { |
|
816 | + $this->postProcAndFlush($this->outputFP, $data); |
|
817 | + } elseif ($this->bufferIsActive) { |
|
818 | 818 | $this->CDATABuffer .= $data; |
819 | 819 | } |
820 | 820 | } |
@@ -64,22 +64,22 @@ discard block |
||
64 | 64 | * @throws Exception\NotValidFileException |
65 | 65 | * @throws Exception\XmlParsingException |
66 | 66 | */ |
67 | - public function xliffToArray( $xliffContent, $collapseEmptyTags = false) |
|
67 | + public function xliffToArray($xliffContent, $collapseEmptyTags = false) |
|
68 | 68 | { |
69 | 69 | $xliff = []; |
70 | 70 | $xliffContent = self::forceUft8Encoding($xliffContent, $xliff); |
71 | 71 | $xliffVersion = XliffVersionDetector::detect($xliffContent); |
72 | 72 | $info = XliffProprietaryDetect::getInfoFromXliffContent($xliffContent); |
73 | 73 | |
74 | - if($xliffVersion === 1){ |
|
74 | + if ($xliffVersion === 1) { |
|
75 | 75 | $xliffContent = self::removeInternalFileTagFromContent($xliffContent, $xliff); |
76 | 76 | } |
77 | 77 | |
78 | - if($xliffVersion === 2){ |
|
78 | + if ($xliffVersion === 2) { |
|
79 | 79 | $xliffContent = self::escapeDataInOriginalMap($xliffContent); |
80 | 80 | } |
81 | 81 | |
82 | - if($collapseEmptyTags === false){ |
|
82 | + if ($collapseEmptyTags === false) { |
|
83 | 83 | $xliffContent = self::insertPlaceholderInEmptyTags($xliffContent); |
84 | 84 | } |
85 | 85 | |
@@ -104,8 +104,8 @@ discard block |
||
104 | 104 | $enc = mb_detect_encoding($xliffContent); |
105 | 105 | |
106 | 106 | if ($enc !== 'UTF-8') { |
107 | - $xliff[ 'parser-warnings' ][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
108 | - $s = iconv( $enc, 'UTF-8', $xliffContent ); |
|
107 | + $xliff['parser-warnings'][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
108 | + $s = iconv($enc, 'UTF-8', $xliffContent); |
|
109 | 109 | $xliffContent = $s !== false ? $s : ""; |
110 | 110 | } |
111 | 111 | |
@@ -125,22 +125,22 @@ discard block |
||
125 | 125 | private static function removeInternalFileTagFromContent($xliffContent, &$xliff) |
126 | 126 | { |
127 | 127 | $index = 1; |
128 | - $a = Strings::preg_split( '|<internal-file[\s>]|si', $xliffContent ); |
|
128 | + $a = Strings::preg_split('|<internal-file[\s>]|si', $xliffContent); |
|
129 | 129 | |
130 | 130 | // no match, return original string |
131 | - if(count($a) === 1){ |
|
131 | + if (count($a) === 1) { |
|
132 | 132 | return $a[0]; |
133 | 133 | } |
134 | 134 | |
135 | - $b = Strings::preg_split( '|</internal-file>|si', $a[1] ); |
|
136 | - $strippedContent = $a[0].$b[1]; |
|
137 | - $xliff['files'][$index][ 'reference' ][] = self::extractBase64($b[0]); |
|
135 | + $b = Strings::preg_split('|</internal-file>|si', $a[1]); |
|
136 | + $strippedContent = $a[0] . $b[1]; |
|
137 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
138 | 138 | $index++; |
139 | 139 | |
140 | - if(isset($a[2])){ |
|
141 | - $c = Strings::preg_split( '|</internal-file[\s>]|si', $a[2] ); |
|
140 | + if (isset($a[2])) { |
|
141 | + $c = Strings::preg_split('|</internal-file[\s>]|si', $a[2]); |
|
142 | 142 | $strippedContent .= $c[1]; |
143 | - $xliff['files'][$index][ 'reference' ][] = self::extractBase64($c[0]); |
|
143 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | return $strippedContent; |
@@ -177,8 +177,8 @@ discard block |
||
177 | 177 | */ |
178 | 178 | private static function escapeDataInOriginalMap($xliffContent) |
179 | 179 | { |
180 | - $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceSpace' ], $xliffContent); |
|
181 | - $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceXliffTags' ], $xliffContent); |
|
180 | + $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
181 | + $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
182 | 182 | |
183 | 183 | return $xliffContent; |
184 | 184 | } |
@@ -201,11 +201,11 @@ discard block |
||
201 | 201 | { |
202 | 202 | preg_match_all('/<([a-zA-Z0-9._-]+)[^>]*><\/\1>/sm', $xliffContent, $emptyTagMatches); |
203 | 203 | |
204 | - if(!empty($emptyTagMatches[0])){ |
|
205 | - foreach ($emptyTagMatches[0] as $index => $emptyTagMatch){ |
|
204 | + if (!empty($emptyTagMatches[0])) { |
|
205 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
206 | 206 | $matchedTag = $emptyTagMatches[1][$index]; |
207 | - $subst = Placeholder::EMPTY_TAG_PLACEHOLDER.'</'.$matchedTag.'>'; |
|
208 | - $replacedTag = str_replace('</'.$matchedTag.'>', $subst, $emptyTagMatch); |
|
207 | + $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
|
208 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
209 | 209 | $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
210 | 210 | } |
211 | 211 | } |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
227 | 227 | $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
228 | 228 | |
229 | - return '<data'.$matches[1].'>'.$content.'</data>'; |
|
229 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
@@ -239,11 +239,11 @@ discard block |
||
239 | 239 | $xliffTags = XliffTags::$tags; |
240 | 240 | $content = $matches[2]; |
241 | 241 | |
242 | - foreach ($xliffTags as $xliffTag){ |
|
243 | - $content = preg_replace( '|<('.$xliffTag.'.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
244 | - $content = preg_replace( '|<(/'.$xliffTag.')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
242 | + foreach ($xliffTags as $xliffTag) { |
|
243 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
244 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
245 | 245 | } |
246 | 246 | |
247 | - return '<data'.$matches[1].'>'.$content.'</data>'; |
|
247 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
248 | 248 | } |
249 | 249 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @param string|null $xliffProprietary |
35 | 35 | * @param LoggerInterface|null $logger |
36 | 36 | */ |
37 | - public function __construct( $xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null ) { |
|
37 | + public function __construct($xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null) { |
|
38 | 38 | $this->xliffVersion = $xliffVersion; |
39 | 39 | $this->logger = $logger; |
40 | 40 | $this->xliffProprietary = $xliffProprietary; |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @return string |
45 | 45 | */ |
46 | 46 | protected function getTuTagName() { |
47 | - return ( $this->xliffVersion === 1 ) ? 'trans-unit' : 'unit'; |
|
47 | + return ($this->xliffVersion === 1) ? 'trans-unit' : 'unit'; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return array |
54 | 54 | */ |
55 | - abstract public function parse( DOMDocument $dom, $output = [] ); |
|
55 | + abstract public function parse(DOMDocument $dom, $output = []); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * Extract trans-unit content from the current node |
@@ -64,17 +64,17 @@ discard block |
||
64 | 64 | * @param $i |
65 | 65 | * @param $j |
66 | 66 | */ |
67 | - protected function extractTuFromNode( $childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j ) { |
|
68 | - if ( $childNode->nodeName === 'group' ) { |
|
69 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
70 | - if ( $nestedChildNode->nodeName === 'group' ) { |
|
71 | - $this->extractTuFromNode( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
72 | - } elseif ( $nestedChildNode->nodeName === $this->getTuTagName() ) { |
|
73 | - $this->extractTransUnit( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
67 | + protected function extractTuFromNode($childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j) { |
|
68 | + if ($childNode->nodeName === 'group') { |
|
69 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
70 | + if ($nestedChildNode->nodeName === 'group') { |
|
71 | + $this->extractTuFromNode($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
72 | + } elseif ($nestedChildNode->nodeName === $this->getTuTagName()) { |
|
73 | + $this->extractTransUnit($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
74 | 74 | } |
75 | 75 | } |
76 | - } elseif ( $childNode->nodeName === $this->getTuTagName() ) { |
|
77 | - $this->extractTransUnit( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
76 | + } elseif ($childNode->nodeName === $this->getTuTagName()) { |
|
77 | + $this->extractTransUnit($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - abstract protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ); |
|
93 | + abstract protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j); |
|
94 | 94 | |
95 | 95 | /** |
96 | 96 | * @param DOMDocument $dom |
@@ -98,10 +98,10 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @return array |
100 | 100 | */ |
101 | - protected function extractContent( DOMDocument $dom, DOMNode $node ) { |
|
101 | + protected function extractContent(DOMDocument $dom, DOMNode $node) { |
|
102 | 102 | return [ |
103 | - 'raw-content' => $this->extractTagContent( $dom, $node ), |
|
104 | - 'attr' => $this->extractTagAttributes( $node ) |
|
103 | + 'raw-content' => $this->extractTagContent($dom, $node), |
|
104 | + 'attr' => $this->extractTagAttributes($node) |
|
105 | 105 | ]; |
106 | 106 | } |
107 | 107 | |
@@ -118,12 +118,12 @@ discard block |
||
118 | 118 | * |
119 | 119 | * @return array |
120 | 120 | */ |
121 | - protected function extractTagAttributes( DOMNode $element ) { |
|
121 | + protected function extractTagAttributes(DOMNode $element) { |
|
122 | 122 | $tagAttributes = []; |
123 | 123 | |
124 | - if ( $element->hasAttributes() ) { |
|
125 | - foreach ( $element->attributes as $attr ) { |
|
126 | - $tagAttributes[ $attr->nodeName ] = $attr->nodeValue; |
|
124 | + if ($element->hasAttributes()) { |
|
125 | + foreach ($element->attributes as $attr) { |
|
126 | + $tagAttributes[$attr->nodeName] = $attr->nodeValue; |
|
127 | 127 | } |
128 | 128 | } |
129 | 129 | |
@@ -138,17 +138,17 @@ discard block |
||
138 | 138 | * |
139 | 139 | * @return string |
140 | 140 | */ |
141 | - protected function extractTagContent( DOMDocument $dom, DOMNode $element ) { |
|
141 | + protected function extractTagContent(DOMDocument $dom, DOMNode $element) { |
|
142 | 142 | $childNodes = $element->hasChildNodes(); |
143 | 143 | $extractedContent = ''; |
144 | 144 | |
145 | - if ( !empty( $childNodes ) ) { |
|
146 | - foreach ( $element->childNodes as $node ) { |
|
147 | - $extractedContent .= Emoji::toEntity( Strings::fixNonWellFormedXml( $dom->saveXML( $node ) ) ); |
|
145 | + if (!empty($childNodes)) { |
|
146 | + foreach ($element->childNodes as $node) { |
|
147 | + $extractedContent .= Emoji::toEntity(Strings::fixNonWellFormedXml($dom->saveXML($node))); |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | - return str_replace( Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent ); |
|
151 | + return str_replace(Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
@@ -161,50 +161,50 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @return array |
163 | 163 | */ |
164 | - protected function extractContentWithMarksAndExtTags( DOMDocument $dom, DOMElement $childNode, $originalRawContent, array $originalData = [] ) { |
|
164 | + protected function extractContentWithMarksAndExtTags(DOMDocument $dom, DOMElement $childNode, $originalRawContent, array $originalData = []) { |
|
165 | 165 | $source = []; |
166 | 166 | |
167 | 167 | // example: |
168 | 168 | // <g id="1"><mrk mid="0" mtype="seg">An English string with g tags</mrk></g> |
169 | - $raw = $this->extractTagContent( $dom, $childNode ); |
|
169 | + $raw = $this->extractTagContent($dom, $childNode); |
|
170 | 170 | |
171 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
171 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
172 | 172 | |
173 | 173 | $mi = 0; |
174 | - while ( isset( $markers[ $mi + 1 ] ) ) { |
|
175 | - unset( $mid ); |
|
174 | + while (isset($markers[$mi + 1])) { |
|
175 | + unset($mid); |
|
176 | 176 | |
177 | - preg_match( '|mid\s?=\s?["\'](.*?)["\']|si', $markers[ $mi + 1 ], $mid ); |
|
177 | + preg_match('|mid\s?=\s?["\'](.*?)["\']|si', $markers[$mi + 1], $mid); |
|
178 | 178 | |
179 | 179 | // if it's a Trados file the trailing spaces after </mrk> are meaningful |
180 | 180 | // so we add them to |
181 | 181 | $trailingSpaces = ''; |
182 | - if ( $this->xliffProprietary === 'trados' ) { |
|
183 | - preg_match_all( '/<\/mrk>[\s]+/iu', $markers[ $mi + 1 ], $trailingSpacesMatches ); |
|
182 | + if ($this->xliffProprietary === 'trados') { |
|
183 | + preg_match_all('/<\/mrk>[\s]+/iu', $markers[$mi + 1], $trailingSpacesMatches); |
|
184 | 184 | |
185 | - if ( isset( $trailingSpacesMatches[ 0 ] ) && count( $trailingSpacesMatches[ 0 ] ) > 0 ) { |
|
186 | - foreach ( $trailingSpacesMatches[ 0 ] as $match ) { |
|
187 | - $trailingSpaces = str_replace( '</mrk>', '', $match ); |
|
185 | + if (isset($trailingSpacesMatches[0]) && count($trailingSpacesMatches[0]) > 0) { |
|
186 | + foreach ($trailingSpacesMatches[0] as $match) { |
|
187 | + $trailingSpaces = str_replace('</mrk>', '', $match); |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | 192 | //re-build the mrk tag after the split |
193 | - $originalMark = trim( '<mrk ' . $markers[ $mi + 1 ] ); |
|
193 | + $originalMark = trim('<mrk ' . $markers[$mi + 1]); |
|
194 | 194 | |
195 | - $mark_string = preg_replace( '#^<mrk\s[^>]+>(.*)#', '$1', $originalMark ); // at this point we have: ---> 'Test </mrk> </g>>' |
|
196 | - $mark_content = preg_split( '#</mrk>#si', $mark_string ); |
|
195 | + $mark_string = preg_replace('#^<mrk\s[^>]+>(.*)#', '$1', $originalMark); // at this point we have: ---> 'Test </mrk> </g>>' |
|
196 | + $mark_content = preg_split('#</mrk>#si', $mark_string); |
|
197 | 197 | |
198 | 198 | $sourceArray = [ |
199 | - 'mid' => ( isset( $mid[ 1 ] ) ) ? $mid[ 1 ] : $mi, |
|
200 | - 'ext-prec-tags' => ( $mi == 0 ? $markers[ 0 ] : "" ), |
|
201 | - 'raw-content' => ( isset( $mark_content[ 0 ] ) ) ? $mark_content[ 0 ] . $trailingSpaces : '', |
|
202 | - 'ext-succ-tags' => ( isset( $mark_content[ 1 ] ) ) ? $mark_content[ 1 ] : '', |
|
199 | + 'mid' => (isset($mid[1])) ? $mid[1] : $mi, |
|
200 | + 'ext-prec-tags' => ($mi == 0 ? $markers[0] : ""), |
|
201 | + 'raw-content' => (isset($mark_content[0])) ? $mark_content[0] . $trailingSpaces : '', |
|
202 | + 'ext-succ-tags' => (isset($mark_content[1])) ? $mark_content[1] : '', |
|
203 | 203 | ]; |
204 | 204 | |
205 | - if ( !empty( $originalData ) ) { |
|
206 | - $dataRefMap = $this->getDataRefMap( $originalData ); |
|
207 | - $sourceArray[ 'replaced-content' ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $mark_content[ 0 ] ); |
|
205 | + if (!empty($originalData)) { |
|
206 | + $dataRefMap = $this->getDataRefMap($originalData); |
|
207 | + $sourceArray['replaced-content'] = (new DataRefReplacer($dataRefMap))->replace($mark_content[0]); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | $source[] = $sourceArray; |
@@ -220,12 +220,12 @@ discard block |
||
220 | 220 | * |
221 | 221 | * @return array |
222 | 222 | */ |
223 | - protected function getDataRefMap( $originalData ) { |
|
223 | + protected function getDataRefMap($originalData) { |
|
224 | 224 | // dataRef map |
225 | 225 | $dataRefMap = []; |
226 | - foreach ( $originalData as $datum ) { |
|
227 | - if ( isset( $datum[ 'attr' ][ 'id' ] ) ) { |
|
228 | - $dataRefMap[ $datum[ 'attr' ][ 'id' ] ] = $datum[ 'raw-content' ]; |
|
226 | + foreach ($originalData as $datum) { |
|
227 | + if (isset($datum['attr']['id'])) { |
|
228 | + $dataRefMap[$datum['attr']['id']] = $datum['raw-content']; |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | |
@@ -237,10 +237,10 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return bool |
239 | 239 | */ |
240 | - protected function stringContainsMarks( $raw ) { |
|
241 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
240 | + protected function stringContainsMarks($raw) { |
|
241 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
242 | 242 | |
243 | - return isset( $markers[ 1 ] ); |
|
243 | + return isset($markers[1]); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | /** |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | * @return array |
251 | 251 | * @throws \Exception |
252 | 252 | */ |
253 | - protected function JSONOrRawContentArray( $noteValue, $escapeStrings = true ) { |
|
253 | + protected function JSONOrRawContentArray($noteValue, $escapeStrings = true) { |
|
254 | 254 | // |
255 | 255 | // convert double escaped entites |
256 | 256 | // |
@@ -260,17 +260,17 @@ discard block |
||
260 | 260 | // &amp; ---> & |
261 | 261 | // &apos ---> ' |
262 | 262 | // |
263 | - if ( Strings::isADoubleEscapedEntity( $noteValue ) ) { |
|
264 | - $noteValue = Strings::htmlspecialchars_decode( $noteValue, true ); |
|
263 | + if (Strings::isADoubleEscapedEntity($noteValue)) { |
|
264 | + $noteValue = Strings::htmlspecialchars_decode($noteValue, true); |
|
265 | 265 | } else { |
266 | 266 | // for non escaped entities $escapeStrings is always true for security reasons |
267 | 267 | $escapeStrings = true; |
268 | 268 | } |
269 | 269 | |
270 | - if ( Strings::isJSON( $noteValue ) ) { |
|
271 | - return [ 'json' => Strings::cleanCDATA( $noteValue ) ]; |
|
270 | + if (Strings::isJSON($noteValue)) { |
|
271 | + return ['json' => Strings::cleanCDATA($noteValue)]; |
|
272 | 272 | } |
273 | 273 | |
274 | - return [ 'raw-content' => Strings::fixNonWellFormedXml( $noteValue, $escapeStrings ) ]; |
|
274 | + return ['raw-content' => Strings::fixNonWellFormedXml($noteValue, $escapeStrings)]; |
|
275 | 275 | } |
276 | 276 | } |
@@ -15,33 +15,33 @@ 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( $file ); |
|
24 | + $output['files'][$i]['attr'] = $this->extractMetadata($file); |
|
25 | 25 | |
26 | 26 | // reference |
27 | - if ( !empty( $this->extractReference( $file ) ) ) { |
|
28 | - $output[ 'files' ][ $i ][ 'reference' ] = $this->extractReference( $file ); |
|
27 | + if (!empty($this->extractReference($file))) { |
|
28 | + $output['files'][$i]['reference'] = $this->extractReference($file); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | // trans-units |
32 | 32 | $transUnitIdArrayForUniquenessCheck = []; |
33 | 33 | $j = 1; |
34 | - foreach ( $file->childNodes as $body ) { |
|
35 | - if ( $body->nodeName === 'body' ) { |
|
36 | - foreach ( $body->childNodes as $childNode ) { |
|
37 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
34 | + foreach ($file->childNodes as $body) { |
|
35 | + if ($body->nodeName === 'body') { |
|
36 | + foreach ($body->childNodes as $childNode) { |
|
37 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | // trans-unit re-count check |
41 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
42 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
43 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
44 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
41 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
42 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
43 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
44 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | $i++; |
@@ -57,50 +57,50 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @return array |
59 | 59 | */ |
60 | - private function extractMetadata( DOMElement $file ) { |
|
60 | + private function extractMetadata(DOMElement $file) { |
|
61 | 61 | $metadata = []; |
62 | 62 | $customAttr = []; |
63 | 63 | |
64 | 64 | /** @var \DOMAttr $attribute */ |
65 | - foreach ( $file->attributes as $attribute ) { |
|
66 | - switch ( $attribute->localName ) { |
|
65 | + foreach ($file->attributes as $attribute) { |
|
66 | + switch ($attribute->localName) { |
|
67 | 67 | // original |
68 | 68 | case 'original': |
69 | - $metadata[ 'original' ] = $attribute->value; |
|
69 | + $metadata['original'] = $attribute->value; |
|
70 | 70 | break; |
71 | 71 | |
72 | 72 | // source-language |
73 | 73 | case 'source-language': |
74 | - $metadata[ 'source-language' ] = $attribute->value; |
|
74 | + $metadata['source-language'] = $attribute->value; |
|
75 | 75 | break; |
76 | 76 | |
77 | 77 | // data-type |
78 | 78 | case 'datatype': |
79 | - $metadata[ 'data-type' ] = $attribute->value; |
|
79 | + $metadata['data-type'] = $attribute->value; |
|
80 | 80 | break; |
81 | 81 | |
82 | 82 | // target-language |
83 | 83 | case 'target-language': |
84 | - $metadata[ 'target-language' ] = $attribute->value; |
|
84 | + $metadata['target-language'] = $attribute->value; |
|
85 | 85 | break; |
86 | 86 | } |
87 | 87 | |
88 | 88 | // Custom MateCat x-Attribute |
89 | - preg_match( '|x-(.*?)|si', $attribute->localName, $temp ); |
|
90 | - if ( isset( $temp[ 1 ] ) ) { |
|
91 | - $customAttr[ $attribute->localName ] = $attribute->value; |
|
89 | + preg_match('|x-(.*?)|si', $attribute->localName, $temp); |
|
90 | + if (isset($temp[1])) { |
|
91 | + $customAttr[$attribute->localName] = $attribute->value; |
|
92 | 92 | } |
93 | - unset( $temp ); |
|
93 | + unset($temp); |
|
94 | 94 | |
95 | 95 | // Custom MateCat namespace Attribute mtc: |
96 | - preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp ); |
|
97 | - if ( isset( $temp[ 1 ] ) ) { |
|
98 | - $customAttr[ $attribute->nodeName ] = $attribute->value; |
|
96 | + preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp); |
|
97 | + if (isset($temp[1])) { |
|
98 | + $customAttr[$attribute->nodeName] = $attribute->value; |
|
99 | 99 | } |
100 | - unset( $temp ); |
|
100 | + unset($temp); |
|
101 | 101 | |
102 | - if ( !empty( $customAttr ) ) { |
|
103 | - $metadata[ 'custom' ] = $customAttr; |
|
102 | + if (!empty($customAttr)) { |
|
103 | + $metadata['custom'] = $customAttr; |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 | |
@@ -112,16 +112,16 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return array |
114 | 114 | */ |
115 | - private function extractReference( DOMElement $file ) { |
|
115 | + private function extractReference(DOMElement $file) { |
|
116 | 116 | $reference = []; |
117 | 117 | |
118 | 118 | $order = 0; |
119 | - foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) { |
|
119 | + foreach ($file->getElementsByTagName('reference') as $ref) { |
|
120 | 120 | /** @var DOMNode $childNode */ |
121 | - foreach ( $ref->childNodes as $childNode ) { |
|
122 | - if ( $childNode->nodeName === 'internal-file' ) { |
|
123 | - $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue; |
|
124 | - $reference[ $order ][ 'base64' ] = trim( $childNode->nodeValue ); |
|
121 | + foreach ($ref->childNodes as $childNode) { |
|
122 | + if ($childNode->nodeName === 'internal-file') { |
|
123 | + $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue; |
|
124 | + $reference[$order]['base64'] = trim($childNode->nodeValue); |
|
125 | 125 | $order++; |
126 | 126 | } |
127 | 127 | } |
@@ -142,53 +142,53 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @throws Exception |
144 | 144 | */ |
145 | - protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ) { |
|
145 | + protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j) { |
|
146 | 146 | // metadata |
147 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
147 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
148 | 148 | |
149 | 149 | // notes |
150 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit ); |
|
150 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit); |
|
151 | 151 | |
152 | 152 | // content |
153 | 153 | /** @var DOMElement $childNode */ |
154 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
154 | + foreach ($transUnit->childNodes as $childNode) { |
|
155 | 155 | // source |
156 | - if ( $childNode->nodeName === 'source' ) { |
|
157 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode ); |
|
156 | + if ($childNode->nodeName === 'source') { |
|
157 | + $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | // seg-source |
161 | - if ( $childNode->nodeName === 'seg-source' ) { |
|
162 | - $rawSegment = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ]; |
|
163 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment ); |
|
161 | + if ($childNode->nodeName === 'seg-source') { |
|
162 | + $rawSegment = $output['files'][$i]['trans-units'][$j]['source']['raw-content']; |
|
163 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | // target |
167 | - if ( $childNode->nodeName === 'target' ) { |
|
168 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode ); |
|
167 | + if ($childNode->nodeName === 'target') { |
|
168 | + $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode); |
|
169 | 169 | |
170 | 170 | // seg-target |
171 | - $targetRawContent = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ]; |
|
172 | - $segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ]; |
|
173 | - if ( isset( $targetRawContent ) && !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) { |
|
174 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $targetRawContent ); |
|
171 | + $targetRawContent = @$output['files'][$i]['trans-units'][$j]['target']['raw-content']; |
|
172 | + $segSource = @$output['files'][$i]['trans-units'][$j]['seg-source']; |
|
173 | + if (isset($targetRawContent) && !empty($targetRawContent) && isset($segSource) && count($segSource) > 0) { |
|
174 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $targetRawContent); |
|
175 | 175 | } |
176 | 176 | } |
177 | 177 | |
178 | 178 | // locked |
179 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
180 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
179 | + if ($childNode->nodeName === 'sdl:seg') { |
|
180 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
184 | 184 | // context-group |
185 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
186 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
185 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
186 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | // alt-trans |
190 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
191 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
190 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
191 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | $j++; |
@@ -201,32 +201,32 @@ discard block |
||
201 | 201 | * @return array |
202 | 202 | * @throws Exception |
203 | 203 | */ |
204 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ) { |
|
204 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck) { |
|
205 | 205 | $metadata = []; |
206 | 206 | |
207 | 207 | // id |
208 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
209 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
208 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
209 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
210 | 210 | } |
211 | 211 | |
212 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
212 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
213 | 213 | |
214 | - if ( strlen( $id ) > 100 ) { |
|
215 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
214 | + if (strlen($id) > 100) { |
|
215 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | $transUnitIdArrayForUniquenessCheck[] = $id; |
219 | - $metadata[ 'id' ] = $id; |
|
219 | + $metadata['id'] = $id; |
|
220 | 220 | |
221 | 221 | // translate |
222 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
223 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
222 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
223 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | // approved |
227 | 227 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
228 | - if ( null !== $transUnit->attributes->getNamedItem( 'approved' ) ) { |
|
229 | - $metadata[ 'approved' ] = filter_var( $transUnit->attributes->getNamedItem( 'approved' )->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
228 | + if (null !== $transUnit->attributes->getNamedItem('approved')) { |
|
229 | + $metadata['approved'] = filter_var($transUnit->attributes->getNamedItem('approved')->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | return $metadata; |
@@ -238,19 +238,19 @@ discard block |
||
238 | 238 | * @return array |
239 | 239 | * @throws Exception |
240 | 240 | */ |
241 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ) { |
|
241 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit) { |
|
242 | 242 | $notes = []; |
243 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
243 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
244 | 244 | |
245 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
245 | + $noteValue = $this->extractTagContent($dom, $note); |
|
246 | 246 | |
247 | - if ( '' !== $noteValue ) { |
|
247 | + if ('' !== $noteValue) { |
|
248 | 248 | |
249 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
249 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
250 | 250 | |
251 | 251 | // extract all the attributes |
252 | - foreach ( $note->attributes as $attribute ) { |
|
253 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
252 | + foreach ($note->attributes as $attribute) { |
|
253 | + $extractedNote[$attribute->name] = $attribute->value; |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | $notes[] = $extractedNote; |
@@ -265,14 +265,14 @@ discard block |
||
265 | 265 | * |
266 | 266 | * @return array |
267 | 267 | */ |
268 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ) { |
|
268 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup) { |
|
269 | 269 | $cg = []; |
270 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
270 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
271 | 271 | |
272 | 272 | /** @var DOMNode $context */ |
273 | - foreach ( $contextGroup->childNodes as $context ) { |
|
274 | - if ( $context->nodeName === 'context' ) { |
|
275 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
273 | + foreach ($contextGroup->childNodes as $context) { |
|
274 | + if ($context->nodeName === 'context') { |
|
275 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | |
@@ -284,16 +284,16 @@ discard block |
||
284 | 284 | * |
285 | 285 | * @return array |
286 | 286 | */ |
287 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
287 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
288 | 288 | $at = []; |
289 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
289 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
290 | 290 | |
291 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
292 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
291 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
292 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
293 | 293 | } |
294 | 294 | |
295 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
296 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
295 | + if ($altTrans->getElementsByTagName('target')) { |
|
296 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | return $at; |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * |
305 | 305 | * @return bool |
306 | 306 | */ |
307 | - private function extractLocked( DOMElement $locked ) { |
|
308 | - return null !== $locked->getAttribute( 'locked' ); |
|
307 | + private function extractLocked(DOMElement $locked) { |
|
308 | + return null !== $locked->getAttribute('locked'); |
|
309 | 309 | } |
310 | 310 | } |
@@ -16,30 +16,30 @@ 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( $dom ); |
|
25 | + $output['files'][$i]['attr'] = $this->extractMetadata($dom); |
|
26 | 26 | |
27 | 27 | // notes |
28 | - $output[ 'files' ][ $i ][ 'notes' ] = $this->extractNotes( $file ); |
|
28 | + $output['files'][$i]['notes'] = $this->extractNotes($file); |
|
29 | 29 | |
30 | 30 | // trans-units |
31 | 31 | $transUnitIdArrayForUniquenessCheck = []; |
32 | 32 | $j = 1; |
33 | 33 | /** @var DOMElement $transUnit */ |
34 | - foreach ( $file->childNodes as $childNode ) { |
|
35 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
34 | + foreach ($file->childNodes as $childNode) { |
|
35 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // trans-unit re-count check |
39 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
40 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
41 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
42 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
39 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
40 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
41 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
42 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | $i++; |
@@ -53,23 +53,23 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return array |
55 | 55 | */ |
56 | - private function extractMetadata( DOMDocument $dom ) { |
|
56 | + private function extractMetadata(DOMDocument $dom) { |
|
57 | 57 | $metadata = []; |
58 | 58 | |
59 | - $xliffNode = $dom->getElementsByTagName( 'xliff' )->item( 0 ); |
|
60 | - $fileNode = $dom->getElementsByTagName( 'file' )->item( 0 ); |
|
59 | + $xliffNode = $dom->getElementsByTagName('xliff')->item(0); |
|
60 | + $fileNode = $dom->getElementsByTagName('file')->item(0); |
|
61 | 61 | |
62 | 62 | // original |
63 | - $metadata[ 'original' ] = ( null !== $fileNode->attributes->getNamedItem( 'original' ) ) ? $fileNode->attributes->getNamedItem( 'original' )->nodeValue : 'no-name'; |
|
63 | + $metadata['original'] = (null !== $fileNode->attributes->getNamedItem('original')) ? $fileNode->attributes->getNamedItem('original')->nodeValue : 'no-name'; |
|
64 | 64 | |
65 | 65 | // source-language |
66 | - $metadata[ 'source-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'srcLang' ) ) ? $xliffNode->attributes->getNamedItem( 'srcLang' )->nodeValue : 'en-US'; |
|
66 | + $metadata['source-language'] = (null !== $xliffNode->attributes->getNamedItem('srcLang')) ? $xliffNode->attributes->getNamedItem('srcLang')->nodeValue : 'en-US'; |
|
67 | 67 | |
68 | 68 | // datatype |
69 | 69 | // @TODO to be implemented |
70 | 70 | |
71 | 71 | // target-language |
72 | - $metadata[ 'target-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'trgLang' ) ) ? $xliffNode->attributes->getNamedItem( 'trgLang' )->nodeValue : 'en-US'; |
|
72 | + $metadata['target-language'] = (null !== $xliffNode->attributes->getNamedItem('trgLang')) ? $xliffNode->attributes->getNamedItem('trgLang')->nodeValue : 'en-US'; |
|
73 | 73 | |
74 | 74 | // custom MateCat x-attribute |
75 | 75 | // @TODO to be implemented |
@@ -83,16 +83,16 @@ discard block |
||
83 | 83 | * @return array |
84 | 84 | * @throws \Exception |
85 | 85 | */ |
86 | - private function extractNotes( DOMElement $file ) { |
|
86 | + private function extractNotes(DOMElement $file) { |
|
87 | 87 | $notes = []; |
88 | 88 | |
89 | 89 | // loop <notes> to get nested <note> tag |
90 | - foreach ( $file->childNodes as $childNode ) { |
|
91 | - if ( $childNode->nodeName === 'notes' ) { |
|
92 | - foreach ( $childNode->childNodes as $note ) { |
|
93 | - $noteValue = trim( $note->nodeValue ); |
|
94 | - if ( '' !== $noteValue ) { |
|
95 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
90 | + foreach ($file->childNodes as $childNode) { |
|
91 | + if ($childNode->nodeName === 'notes') { |
|
92 | + foreach ($childNode->childNodes as $note) { |
|
93 | + $noteValue = trim($note->nodeValue); |
|
94 | + if ('' !== $noteValue) { |
|
95 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | } |
@@ -113,33 +113,33 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @throws \Exception |
115 | 115 | */ |
116 | - protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ) { |
|
116 | + protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j) { |
|
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; |
|
136 | - $dataRefMap = $this->getDataRefMap( $originalData ); |
|
133 | + $originalData = $this->extractTransUnitOriginalData($transUnit); |
|
134 | + if (!empty($originalData)) { |
|
135 | + $output['files'][$i]['trans-units'][$j]['original-data'] = $originalData; |
|
136 | + $dataRefMap = $this->getDataRefMap($originalData); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // additionalTagData (exclusive for V2) |
140 | - $additionalTagData = $this->extractTransUnitAdditionalTagData( $transUnit ); |
|
141 | - if ( !empty( $additionalTagData ) ) { |
|
142 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'additional-tag-data' ] = $additionalTagData; |
|
140 | + $additionalTagData = $this->extractTransUnitAdditionalTagData($transUnit); |
|
141 | + if (!empty($additionalTagData)) { |
|
142 | + $output['files'][$i]['trans-units'][$j]['additional-tag-data'] = $additionalTagData; |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | // content |
@@ -159,62 +159,62 @@ discard block |
||
159 | 159 | |
160 | 160 | /** @var DOMElement $segment */ |
161 | 161 | $c = 0; |
162 | - foreach ( $transUnit->childNodes as $segment ) { |
|
163 | - if ( $segment->nodeName === 'segment' ) { |
|
162 | + foreach ($transUnit->childNodes as $segment) { |
|
163 | + if ($segment->nodeName === 'segment') { |
|
164 | 164 | |
165 | 165 | // check segment id consistency |
166 | - $attr = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ]; |
|
167 | - $this->checkSegmentIdConsistency( $segment, $attr ); |
|
166 | + $attr = $output['files'][$i]['trans-units'][$j]['attr']; |
|
167 | + $this->checkSegmentIdConsistency($segment, $attr); |
|
168 | 168 | |
169 | 169 | // loop <segment> to get nested <source> and <target> tag |
170 | - foreach ( $segment->childNodes as $childNode ) { |
|
171 | - if ( $childNode->nodeName === 'source' ) { |
|
172 | - $extractedSource = $this->extractContent( $dom, $childNode ); |
|
173 | - $source[ 'raw-content' ][ $c ] = $extractedSource[ 'raw-content' ]; |
|
170 | + foreach ($segment->childNodes as $childNode) { |
|
171 | + if ($childNode->nodeName === 'source') { |
|
172 | + $extractedSource = $this->extractContent($dom, $childNode); |
|
173 | + $source['raw-content'][$c] = $extractedSource['raw-content']; |
|
174 | 174 | |
175 | - if ( !empty( $originalData ) ) { |
|
176 | - $source[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $source[ 'raw-content' ][ $c ] ); |
|
175 | + if (!empty($originalData)) { |
|
176 | + $source['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($source['raw-content'][$c]); |
|
177 | 177 | } |
178 | 178 | |
179 | - if ( !empty( $extractedSource[ 'attr' ] ) ) { |
|
180 | - $source[ 'attr' ][ $c ] = $extractedSource[ 'attr' ]; |
|
179 | + if (!empty($extractedSource['attr'])) { |
|
180 | + $source['attr'][$c] = $extractedSource['attr']; |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | // append value to 'seg-source' |
184 | - if ( $this->stringContainsMarks( $extractedSource[ 'raw-content' ] ) ) { |
|
185 | - $segSource = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedSource[ 'raw-content' ], $originalData ); |
|
184 | + if ($this->stringContainsMarks($extractedSource['raw-content'])) { |
|
185 | + $segSource = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedSource['raw-content'], $originalData); |
|
186 | 186 | } else { |
187 | 187 | $segSource[] = [ |
188 | - 'mid' => count( $segSource ) > 0 ? count( $segSource ) : 0, |
|
188 | + 'mid' => count($segSource) > 0 ? count($segSource) : 0, |
|
189 | 189 | 'ext-prec-tags' => '', |
190 | - 'raw-content' => $extractedSource[ 'raw-content' ], |
|
191 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedSource[ 'raw-content' ] ) : null, |
|
190 | + 'raw-content' => $extractedSource['raw-content'], |
|
191 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedSource['raw-content']) : null, |
|
192 | 192 | 'ext-succ-tags' => '', |
193 | 193 | ]; |
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | - if ( $childNode->nodeName === 'target' ) { |
|
198 | - $extractedTarget = $this->extractContent( $dom, $childNode ); |
|
199 | - $target[ 'raw-content' ][ $c ] = $extractedTarget[ 'raw-content' ]; |
|
197 | + if ($childNode->nodeName === 'target') { |
|
198 | + $extractedTarget = $this->extractContent($dom, $childNode); |
|
199 | + $target['raw-content'][$c] = $extractedTarget['raw-content']; |
|
200 | 200 | |
201 | - if ( !empty( $originalData ) ) { |
|
202 | - $target[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $target[ 'raw-content' ][ $c ] ); |
|
201 | + if (!empty($originalData)) { |
|
202 | + $target['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($target['raw-content'][$c]); |
|
203 | 203 | } |
204 | 204 | |
205 | - if ( !empty( $extractedTarget[ 'attr' ] ) ) { |
|
206 | - $target[ 'attr' ][ $c ] = $extractedTarget[ 'attr' ]; |
|
205 | + if (!empty($extractedTarget['attr'])) { |
|
206 | + $target['attr'][$c] = $extractedTarget['attr']; |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | // append value to 'seg-target' |
210 | - if ( $this->stringContainsMarks( $extractedTarget[ 'raw-content' ] ) ) { |
|
211 | - $segTarget = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedTarget[ 'raw-content' ], $originalData ); |
|
210 | + if ($this->stringContainsMarks($extractedTarget['raw-content'])) { |
|
211 | + $segTarget = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedTarget['raw-content'], $originalData); |
|
212 | 212 | } else { |
213 | 213 | $segTarget[] = [ |
214 | - 'mid' => count( $segTarget ) > 0 ? count( $segTarget ) : 0, |
|
214 | + 'mid' => count($segTarget) > 0 ? count($segTarget) : 0, |
|
215 | 215 | 'ext-prec-tags' => '', |
216 | - 'raw-content' => $extractedTarget[ 'raw-content' ], |
|
217 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedTarget[ 'raw-content' ] ) : null, |
|
216 | + 'raw-content' => $extractedTarget['raw-content'], |
|
217 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedTarget['raw-content']) : null, |
|
218 | 218 | 'ext-succ-tags' => '', |
219 | 219 | ]; |
220 | 220 | } |
@@ -225,10 +225,10 @@ discard block |
||
225 | 225 | } |
226 | 226 | } |
227 | 227 | |
228 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $source; |
|
229 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $target; |
|
230 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $segSource; |
|
231 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $segTarget; |
|
228 | + $output['files'][$i]['trans-units'][$j]['source'] = $source; |
|
229 | + $output['files'][$i]['trans-units'][$j]['target'] = $target; |
|
230 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $segSource; |
|
231 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $segTarget; |
|
232 | 232 | |
233 | 233 | $j++; |
234 | 234 | } |
@@ -239,41 +239,41 @@ discard block |
||
239 | 239 | * |
240 | 240 | * @return array |
241 | 241 | */ |
242 | - private function extractTransUnitMetadata( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck ) { |
|
242 | + private function extractTransUnitMetadata(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck) { |
|
243 | 243 | $metadata = []; |
244 | 244 | |
245 | 245 | // id |
246 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
247 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
246 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
247 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
248 | 248 | } |
249 | 249 | |
250 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
250 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
251 | 251 | |
252 | - if ( strlen( $id ) > 100 ) { |
|
253 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
252 | + if (strlen($id) > 100) { |
|
253 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | $transUnitIdArrayForUniquenessCheck[] = $id; |
257 | - $metadata[ 'id' ] = $id; |
|
257 | + $metadata['id'] = $id; |
|
258 | 258 | |
259 | 259 | // translate |
260 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
261 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
260 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
261 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | // tGroupBegin |
265 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupBegin' ) ) { |
|
266 | - $metadata[ 'tGroupBegin' ] = $transUnit->attributes->getNamedItem( 'tGroupBegin' )->nodeValue; |
|
265 | + if (null !== $transUnit->attributes->getNamedItem('tGroupBegin')) { |
|
266 | + $metadata['tGroupBegin'] = $transUnit->attributes->getNamedItem('tGroupBegin')->nodeValue; |
|
267 | 267 | } |
268 | 268 | |
269 | 269 | // tGroupEnd |
270 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupEnd' ) ) { |
|
271 | - $metadata[ 'tGroupEnd' ] = $transUnit->attributes->getNamedItem( 'tGroupEnd' )->nodeValue; |
|
270 | + if (null !== $transUnit->attributes->getNamedItem('tGroupEnd')) { |
|
271 | + $metadata['tGroupEnd'] = $transUnit->attributes->getNamedItem('tGroupEnd')->nodeValue; |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | // sizeRestriction |
275 | - if ( null !== $transUnit->attributes->getNamedItem( 'sizeRestriction' ) && '' !== $transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue ) { |
|
276 | - $metadata[ 'sizeRestriction' ] = (int)$transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue; |
|
275 | + if (null !== $transUnit->attributes->getNamedItem('sizeRestriction') && '' !== $transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue) { |
|
276 | + $metadata['sizeRestriction'] = (int)$transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue; |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | return $metadata; |
@@ -285,31 +285,31 @@ discard block |
||
285 | 285 | * @return array |
286 | 286 | * @throws \Exception |
287 | 287 | */ |
288 | - private function extractTransUnitOriginalData( DOMElement $transUnit ) { |
|
288 | + private function extractTransUnitOriginalData(DOMElement $transUnit) { |
|
289 | 289 | $originalData = []; |
290 | 290 | |
291 | 291 | // loop <originalData> to get nested content |
292 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
293 | - if ( $childNode->nodeName === 'originalData' ) { |
|
294 | - foreach ( $childNode->childNodes as $data ) { |
|
295 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
296 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
292 | + foreach ($transUnit->childNodes as $childNode) { |
|
293 | + if ($childNode->nodeName === 'originalData') { |
|
294 | + foreach ($childNode->childNodes as $data) { |
|
295 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
296 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
297 | 297 | |
298 | - $dataValue = str_replace( Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue ); |
|
299 | - $dataValue = str_replace( Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue ); |
|
300 | - $dataValue = str_replace( Placeholder::TAB_PLACEHOLDER, '\t', $dataValue ); |
|
298 | + $dataValue = str_replace(Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue); |
|
299 | + $dataValue = str_replace(Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue); |
|
300 | + $dataValue = str_replace(Placeholder::TAB_PLACEHOLDER, '\t', $dataValue); |
|
301 | 301 | |
302 | - if ( '' !== $dataValue ) { |
|
302 | + if ('' !== $dataValue) { |
|
303 | 303 | |
304 | - $jsonOrRawContentArray = $this->JSONOrRawContentArray( $dataValue, false ); |
|
304 | + $jsonOrRawContentArray = $this->JSONOrRawContentArray($dataValue, false); |
|
305 | 305 | |
306 | 306 | // restore xliff tags |
307 | - if ( isset( $jsonOrRawContentArray[ 'json' ] ) ) { |
|
308 | - $jsonOrRawContentArray[ 'json' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'json' ] ); |
|
307 | + if (isset($jsonOrRawContentArray['json'])) { |
|
308 | + $jsonOrRawContentArray['json'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['json']); |
|
309 | 309 | } |
310 | 310 | |
311 | - if ( isset( $jsonOrRawContentArray[ 'raw-content' ] ) ) { |
|
312 | - $jsonOrRawContentArray[ 'raw-content' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'raw-content' ] ); |
|
311 | + if (isset($jsonOrRawContentArray['raw-content'])) { |
|
312 | + $jsonOrRawContentArray['raw-content'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['raw-content']); |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | $originalData[] = array_merge( |
@@ -334,41 +334,41 @@ discard block |
||
334 | 334 | * |
335 | 335 | * @return array |
336 | 336 | */ |
337 | - private function extractTransUnitAdditionalTagData( DOMElement $transUnit ) { |
|
337 | + private function extractTransUnitAdditionalTagData(DOMElement $transUnit) { |
|
338 | 338 | $additionalTagData = []; |
339 | 339 | |
340 | 340 | // loop <originalData> to get nested content |
341 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
342 | - if ( $childNode->nodeName === 'memsource:additionalTagData' ) { |
|
343 | - foreach ( $childNode->childNodes as $data ) { |
|
341 | + foreach ($transUnit->childNodes as $childNode) { |
|
342 | + if ($childNode->nodeName === 'memsource:additionalTagData') { |
|
343 | + foreach ($childNode->childNodes as $data) { |
|
344 | 344 | $dataArray = []; |
345 | 345 | |
346 | 346 | // id |
347 | - if ( $data->nodeName === 'memsource:tag' ) { |
|
348 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
349 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
350 | - $dataArray[ 'attr' ][ 'id' ] = $dataId; |
|
347 | + if ($data->nodeName === 'memsource:tag') { |
|
348 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
349 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
350 | + $dataArray['attr']['id'] = $dataId; |
|
351 | 351 | } |
352 | 352 | } |
353 | 353 | |
354 | 354 | // in PHP 7.4 $data->childNodes is an empty DomNodeList, it is iterable with size 0 |
355 | 355 | // PHP 5.6 check: in php 5.6 $data->childNodes can be null |
356 | - if ( $data->childNodes != null ) { |
|
356 | + if ($data->childNodes != null) { |
|
357 | 357 | |
358 | 358 | // content |
359 | - foreach ( $data->childNodes as $datum ) { |
|
360 | - if ( $datum->nodeName === 'memsource:tagId' ) { |
|
361 | - $dataArray[ 'raw-content' ][ 'tagId' ] = $datum->nodeValue; |
|
359 | + foreach ($data->childNodes as $datum) { |
|
360 | + if ($datum->nodeName === 'memsource:tagId') { |
|
361 | + $dataArray['raw-content']['tagId'] = $datum->nodeValue; |
|
362 | 362 | } |
363 | 363 | |
364 | - if ( $datum->nodeName === 'memsource:type' ) { |
|
365 | - $dataArray[ 'raw-content' ][ 'type' ] = $datum->nodeValue; |
|
364 | + if ($datum->nodeName === 'memsource:type') { |
|
365 | + $dataArray['raw-content']['type'] = $datum->nodeValue; |
|
366 | 366 | } |
367 | 367 | } |
368 | 368 | |
369 | 369 | } |
370 | 370 | |
371 | - if ( !empty( $dataArray ) ) { |
|
371 | + if (!empty($dataArray)) { |
|
372 | 372 | $additionalTagData[] = $dataArray; |
373 | 373 | } |
374 | 374 | } |
@@ -384,15 +384,15 @@ discard block |
||
384 | 384 | * @param DOMElement $segment |
385 | 385 | * @param array $attr |
386 | 386 | */ |
387 | - private function checkSegmentIdConsistency( DOMElement $segment, array $attr ) { |
|
388 | - if ( isset( $attr[ 'tGroupBegin' ] ) && isset( $attr[ 'tGroupEnd' ] ) && $segment->attributes->getNamedItem( 'id' ) ) { |
|
389 | - $id = $segment->attributes->getNamedItem( 'id' )->nodeValue; |
|
390 | - $min = (int)$attr[ 'tGroupBegin' ]; |
|
391 | - $max = (int)$attr[ 'tGroupEnd' ]; |
|
392 | - |
|
393 | - if ( false === ( ( $min <= $id ) && ( $id <= $max ) ) ) { |
|
394 | - if ( $this->logger ) { |
|
395 | - $this->logger->warning( 'Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd' ); |
|
387 | + private function checkSegmentIdConsistency(DOMElement $segment, array $attr) { |
|
388 | + if (isset($attr['tGroupBegin']) && isset($attr['tGroupEnd']) && $segment->attributes->getNamedItem('id')) { |
|
389 | + $id = $segment->attributes->getNamedItem('id')->nodeValue; |
|
390 | + $min = (int)$attr['tGroupBegin']; |
|
391 | + $max = (int)$attr['tGroupEnd']; |
|
392 | + |
|
393 | + if (false === (($min <= $id) && ($id <= $max))) { |
|
394 | + if ($this->logger) { |
|
395 | + $this->logger->warning('Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd'); |
|
396 | 396 | } |
397 | 397 | } |
398 | 398 | } |
@@ -404,31 +404,31 @@ discard block |
||
404 | 404 | * @return array |
405 | 405 | * @throws \Exception |
406 | 406 | */ |
407 | - private function extractTransUnitNotes( DOMElement $transUnit ) { |
|
407 | + private function extractTransUnitNotes(DOMElement $transUnit) { |
|
408 | 408 | $notes = []; |
409 | 409 | |
410 | 410 | // loop <notes> to get nested <note> tag |
411 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
412 | - if ( $childNode->nodeName == 'notes' ) { |
|
413 | - foreach ( $childNode->childNodes as $note ) { |
|
414 | - $noteValue = trim( $note->nodeValue ); |
|
415 | - if ( '' !== $noteValue ) { |
|
416 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
411 | + foreach ($transUnit->childNodes as $childNode) { |
|
412 | + if ($childNode->nodeName == 'notes') { |
|
413 | + foreach ($childNode->childNodes as $note) { |
|
414 | + $noteValue = trim($note->nodeValue); |
|
415 | + if ('' !== $noteValue) { |
|
416 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
417 | 417 | } |
418 | 418 | } |
419 | 419 | } |
420 | 420 | |
421 | - if ( $childNode->nodeName === 'mda:metadata' ) { |
|
422 | - foreach ( $childNode->childNodes as $metadata ) { |
|
423 | - if ( $metadata->nodeName === 'mda:metaGroup' ) { |
|
424 | - foreach ( $metadata->childNodes as $meta ) { |
|
425 | - if ( null !== $meta->attributes && null !== $meta->attributes->getNamedItem( 'type' ) ) { |
|
426 | - $type = $meta->attributes->getNamedItem( 'type' )->nodeValue; |
|
427 | - $metaValue = trim( $meta->nodeValue ); |
|
421 | + if ($childNode->nodeName === 'mda:metadata') { |
|
422 | + foreach ($childNode->childNodes as $metadata) { |
|
423 | + if ($metadata->nodeName === 'mda:metaGroup') { |
|
424 | + foreach ($metadata->childNodes as $meta) { |
|
425 | + if (null !== $meta->attributes && null !== $meta->attributes->getNamedItem('type')) { |
|
426 | + $type = $meta->attributes->getNamedItem('type')->nodeValue; |
|
427 | + $metaValue = trim($meta->nodeValue); |
|
428 | 428 | |
429 | - if ( '' !== $metaValue ) { |
|
429 | + if ('' !== $metaValue) { |
|
430 | 430 | $notes[] = array_merge( |
431 | - $this->JSONOrRawContentArray( $metaValue ), |
|
431 | + $this->JSONOrRawContentArray($metaValue), |
|
432 | 432 | [ |
433 | 433 | 'attr' => [ |
434 | 434 | 'type' => $type |