@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @param array $map |
18 | 18 | */ |
19 | - public function __construct( array $map = null ) { |
|
19 | + public function __construct(array $map = null) { |
|
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 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * |
21 | 21 | * @param $tmp |
22 | 22 | */ |
23 | - public function __construct( $tmp ) { |
|
23 | + public function __construct($tmp) { |
|
24 | 24 | $this->tmp = $tmp; |
25 | 25 | $this->steps = []; |
26 | 26 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * @param CheckInterface $step |
30 | 30 | */ |
31 | - public function addCheck( CheckInterface $step ) { |
|
31 | + public function addCheck(CheckInterface $step) { |
|
32 | 32 | $this->steps[] = $step; |
33 | 33 | } |
34 | 34 | |
@@ -39,13 +39,13 @@ discard block |
||
39 | 39 | $fileType = []; |
40 | 40 | |
41 | 41 | /** @var CheckInterface $step */ |
42 | - foreach ( $this->steps as $step ) { |
|
43 | - if ( null !== $step->check( $this->tmp ) ) { |
|
44 | - $fileType = $step->check( $this->tmp ); |
|
42 | + foreach ($this->steps as $step) { |
|
43 | + if (null !== $step->check($this->tmp)) { |
|
44 | + $fileType = $step->check($this->tmp); |
|
45 | 45 | } |
46 | 46 | } |
47 | 47 | |
48 | - if ( !empty( $fileType ) && $this->isValid( $fileType ) ) { |
|
48 | + if (!empty($fileType) && $this->isValid($fileType)) { |
|
49 | 49 | return $fileType; |
50 | 50 | } |
51 | 51 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return bool |
64 | 64 | */ |
65 | - private function isValid( $fileType ) { |
|
65 | + private function isValid($fileType) { |
|
66 | 66 | $mandatoryKeys = [ |
67 | 67 | 'proprietary', |
68 | 68 | 'proprietary_name', |
@@ -70,6 +70,6 @@ discard block |
||
70 | 70 | 'converter_version', |
71 | 71 | ]; |
72 | 72 | |
73 | - return array_keys( $fileType ) === $mandatoryKeys; |
|
73 | + return array_keys($fileType) === $mandatoryKeys; |
|
74 | 74 | } |
75 | 75 | } |
@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @return array |
24 | 24 | */ |
25 | - public static function getInfoFromXliffContent( $xliffContent ) { |
|
25 | + public static function getInfoFromXliffContent($xliffContent) { |
|
26 | 26 | self::reset(); |
27 | - $tmp = self::getFirst1024CharsFromXliff( $xliffContent, null ); |
|
27 | + $tmp = self::getFirst1024CharsFromXliff($xliffContent, null); |
|
28 | 28 | |
29 | - return self::getInfoFromTmp( $tmp ); |
|
29 | + return self::getInfoFromTmp($tmp); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | /** |
@@ -34,12 +34,12 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @return array |
36 | 36 | */ |
37 | - public static function getInfo( $fullPathToFile ) { |
|
37 | + public static function getInfo($fullPathToFile) { |
|
38 | 38 | self::reset(); |
39 | - $tmp = self::getFirst1024CharsFromXliff( null, $fullPathToFile ); |
|
40 | - self::$fileType[ 'info' ] = Files::pathInfo( $fullPathToFile ); |
|
39 | + $tmp = self::getFirst1024CharsFromXliff(null, $fullPathToFile); |
|
40 | + self::$fileType['info'] = Files::pathInfo($fullPathToFile); |
|
41 | 41 | |
42 | - return self::getInfoFromTmp( $tmp ); |
|
42 | + return self::getInfoFromTmp($tmp); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -47,21 +47,21 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @return array |
49 | 49 | */ |
50 | - private static function getInfoFromTmp( $tmp ) { |
|
50 | + private static function getInfoFromTmp($tmp) { |
|
51 | 51 | try { |
52 | - self::checkVersion( $tmp ); |
|
53 | - } catch ( Exception $ignore ) { |
|
52 | + self::checkVersion($tmp); |
|
53 | + } catch (Exception $ignore) { |
|
54 | 54 | // do nothing |
55 | 55 | // self::$fileType[ 'version' ] is left empty |
56 | 56 | } |
57 | 57 | |
58 | 58 | // run CheckXliffProprietaryPipeline |
59 | - $pipeline = self::runPipeline( $tmp ); |
|
59 | + $pipeline = self::runPipeline($tmp); |
|
60 | 60 | |
61 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
62 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
63 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
64 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
61 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
62 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
63 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
64 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
65 | 65 | |
66 | 66 | return self::$fileType; |
67 | 67 | } |
@@ -71,12 +71,12 @@ discard block |
||
71 | 71 | * |
72 | 72 | * @return array |
73 | 73 | */ |
74 | - private static function runPipeline( $tmp ) { |
|
75 | - $pipeline = new CheckXliffProprietaryPipeline( $tmp ); |
|
76 | - $pipeline->addCheck( new CheckSDL() ); |
|
77 | - $pipeline->addCheck( new CheckGlobalSight() ); |
|
78 | - $pipeline->addCheck( new CheckMateCATConverter() ); |
|
79 | - $pipeline->addCheck( new CheckXliffVersion2() ); |
|
74 | + private static function runPipeline($tmp) { |
|
75 | + $pipeline = new CheckXliffProprietaryPipeline($tmp); |
|
76 | + $pipeline->addCheck(new CheckSDL()); |
|
77 | + $pipeline->addCheck(new CheckGlobalSight()); |
|
78 | + $pipeline->addCheck(new CheckMateCATConverter()); |
|
79 | + $pipeline->addCheck(new CheckXliffVersion2()); |
|
80 | 80 | |
81 | 81 | return $pipeline->run(); |
82 | 82 | } |
@@ -99,29 +99,29 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @return array|false |
101 | 101 | */ |
102 | - private static function getFirst1024CharsFromXliff( $stringData = null, $fullPathToFile = null ) { |
|
103 | - if ( !empty( $stringData ) && empty( $fullPathToFile ) ) { |
|
102 | + private static function getFirst1024CharsFromXliff($stringData = null, $fullPathToFile = null) { |
|
103 | + if (!empty($stringData) && empty($fullPathToFile)) { |
|
104 | 104 | $pathInfo = []; |
105 | - $stringData = substr( $stringData, 0, 1024 ); |
|
106 | - } elseif ( empty( $stringData ) && !empty( $fullPathToFile ) ) { |
|
107 | - $pathInfo = Files::pathInfo( $fullPathToFile ); |
|
105 | + $stringData = substr($stringData, 0, 1024); |
|
106 | + } elseif (empty($stringData) && !empty($fullPathToFile)) { |
|
107 | + $pathInfo = Files::pathInfo($fullPathToFile); |
|
108 | 108 | |
109 | - if ( is_file( $fullPathToFile ) ) { |
|
110 | - $file_pointer = fopen( "$fullPathToFile", 'r' ); |
|
109 | + if (is_file($fullPathToFile)) { |
|
110 | + $file_pointer = fopen("$fullPathToFile", 'r'); |
|
111 | 111 | // Checking Requirements (By specs, I know that xliff version is in the first 1KB) |
112 | - $stringData = fread( $file_pointer, 1024 ); |
|
113 | - fclose( $file_pointer ); |
|
112 | + $stringData = fread($file_pointer, 1024); |
|
113 | + fclose($file_pointer); |
|
114 | 114 | } |
115 | - } elseif ( !empty( $stringData ) && !empty( $fullPathToFile ) ) { |
|
116 | - $pathInfo = Files::pathInfo( $fullPathToFile ); |
|
115 | + } elseif (!empty($stringData) && !empty($fullPathToFile)) { |
|
116 | + $pathInfo = Files::pathInfo($fullPathToFile); |
|
117 | 117 | } |
118 | 118 | |
119 | - if ( !empty( $pathInfo ) && !Files::isXliff( $fullPathToFile ) ) { |
|
119 | + if (!empty($pathInfo) && !Files::isXliff($fullPathToFile)) { |
|
120 | 120 | return false; |
121 | 121 | } |
122 | 122 | |
123 | - if ( !empty( $stringData ) ) { |
|
124 | - return [ $stringData ]; |
|
123 | + if (!empty($stringData)) { |
|
124 | + return [$stringData]; |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | return false; |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | * @throws NotSupportedVersionException |
134 | 134 | * @throws NotValidFileException |
135 | 135 | */ |
136 | - protected static function checkVersion( $tmp ) { |
|
137 | - if ( isset( $tmp[ 0 ] ) ) { |
|
138 | - self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] ); |
|
136 | + protected static function checkVersion($tmp) { |
|
137 | + if (isset($tmp[0])) { |
|
138 | + self::$fileType['version'] = XliffVersionDetector::detect($tmp[0]); |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
@@ -146,20 +146,20 @@ discard block |
||
146 | 146 | * @throws NotSupportedVersionException |
147 | 147 | * @throws NotValidFileException |
148 | 148 | */ |
149 | - public static function getInfoByStringData( $stringData ) { |
|
149 | + public static function getInfoByStringData($stringData) { |
|
150 | 150 | self::reset(); |
151 | 151 | |
152 | - $tmp = self::getFirst1024CharsFromXliff( $stringData ); |
|
153 | - self::$fileType[ 'info' ] = []; |
|
154 | - self::checkVersion( $tmp ); |
|
152 | + $tmp = self::getFirst1024CharsFromXliff($stringData); |
|
153 | + self::$fileType['info'] = []; |
|
154 | + self::checkVersion($tmp); |
|
155 | 155 | |
156 | 156 | // run CheckXliffProprietaryPipeline |
157 | - $pipeline = self::runPipeline( $tmp ); |
|
157 | + $pipeline = self::runPipeline($tmp); |
|
158 | 158 | |
159 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
160 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
161 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
162 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
159 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
160 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
161 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
162 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
163 | 163 | |
164 | 164 | return self::$fileType; |
165 | 165 | } |
@@ -171,21 +171,21 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @return bool|int |
173 | 173 | */ |
174 | - public static function fileMustBeConverted( $fullPath, $enforceOnXliff = false, $filterAddress = null ) { |
|
174 | + public static function fileMustBeConverted($fullPath, $enforceOnXliff = false, $filterAddress = null) { |
|
175 | 175 | $convert = true; |
176 | 176 | |
177 | - $fileType = self::getInfo( $fullPath ); |
|
178 | - $memoryFileType = Files::getMemoryFileType( $fullPath ); |
|
177 | + $fileType = self::getInfo($fullPath); |
|
178 | + $memoryFileType = Files::getMemoryFileType($fullPath); |
|
179 | 179 | |
180 | - if ( Files::isXliff( $fullPath ) || $memoryFileType ) { |
|
181 | - if ( !empty( $filterAddress ) ) { |
|
180 | + if (Files::isXliff($fullPath) || $memoryFileType) { |
|
181 | + if (!empty($filterAddress)) { |
|
182 | 182 | |
183 | 183 | //conversion enforce |
184 | - if ( !$enforceOnXliff ) { |
|
184 | + if (!$enforceOnXliff) { |
|
185 | 185 | |
186 | 186 | //if file is not proprietary AND Enforce is disabled |
187 | 187 | //we take it as is |
188 | - if ( !$fileType[ 'proprietary' ] || $memoryFileType ) { |
|
188 | + if (!$fileType['proprietary'] || $memoryFileType) { |
|
189 | 189 | $convert = false; |
190 | 190 | //ok don't convert a standard sdlxliff |
191 | 191 | } |
@@ -193,16 +193,16 @@ discard block |
||
193 | 193 | //if conversion enforce is active |
194 | 194 | //we force all xliff files but not files produced by SDL Studio because we can handle them |
195 | 195 | if ( |
196 | - $fileType[ 'proprietary_short_name' ] == 'matecat_converter' |
|
197 | - || $fileType[ 'proprietary_short_name' ] == 'trados' |
|
198 | - || $fileType[ 'proprietary_short_name' ] == 'xliff_v2' |
|
196 | + $fileType['proprietary_short_name'] == 'matecat_converter' |
|
197 | + || $fileType['proprietary_short_name'] == 'trados' |
|
198 | + || $fileType['proprietary_short_name'] == 'xliff_v2' |
|
199 | 199 | || $memoryFileType |
200 | 200 | ) { |
201 | 201 | $convert = false; |
202 | 202 | //ok don't convert a standard sdlxliff |
203 | 203 | } |
204 | 204 | } |
205 | - } elseif ( $fileType[ 'proprietary' ] ) { |
|
205 | + } elseif ($fileType['proprietary']) { |
|
206 | 206 | |
207 | 207 | /** |
208 | 208 | * Application misconfiguration. |
@@ -8,15 +8,15 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - if ( stripos( $tmp[ 0 ], 'globalsight' ) !== false ) { |
|
16 | - $fileType[ 'proprietary' ] = true; |
|
17 | - $fileType[ 'proprietary_name' ] = 'GlobalSight Download File'; |
|
18 | - $fileType[ 'proprietary_short_name' ] = 'globalsight'; |
|
19 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
14 | + if (isset($tmp[0])) { |
|
15 | + if (stripos($tmp[0], 'globalsight') !== false) { |
|
16 | + $fileType['proprietary'] = true; |
|
17 | + $fileType['proprietary_name'] = 'GlobalSight Download File'; |
|
18 | + $fileType['proprietary_short_name'] = 'globalsight'; |
|
19 | + $fileType['converter_version'] = 'legacy'; |
|
20 | 20 | |
21 | 21 | return $fileType; |
22 | 22 | } |
@@ -8,22 +8,22 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|void|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $versionMatches ); |
|
16 | - preg_match( '|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $xmlnsMatches ); |
|
14 | + if (isset($tmp[0])) { |
|
15 | + preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($tmp[0], 0, 1000), $versionMatches); |
|
16 | + preg_match('|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr($tmp[0], 0, 1000), $xmlnsMatches); |
|
17 | 17 | |
18 | - if ( !empty( $versionMatches ) && !empty( $xmlnsMatches ) ) { |
|
19 | - $version = $versionMatches[ 1 ]; |
|
20 | - $xmlns = $xmlnsMatches[ 1 ]; |
|
18 | + if (!empty($versionMatches) && !empty($xmlnsMatches)) { |
|
19 | + $version = $versionMatches[1]; |
|
20 | + $xmlns = $xmlnsMatches[1]; |
|
21 | 21 | |
22 | - if ( $version === $xmlns && $version >= 2 ) { |
|
23 | - $fileType[ 'proprietary' ] = false; |
|
24 | - $fileType[ 'proprietary_name' ] = 'Xliff v' . $version . ' File'; |
|
25 | - $fileType[ 'proprietary_short_name' ] = 'xliff_v2'; |
|
26 | - $fileType[ 'converter_version' ] = '2.0'; |
|
22 | + if ($version === $xmlns && $version >= 2) { |
|
23 | + $fileType['proprietary'] = false; |
|
24 | + $fileType['proprietary_name'] = 'Xliff v' . $version . ' File'; |
|
25 | + $fileType['proprietary_short_name'] = 'xliff_v2'; |
|
26 | + $fileType['converter_version'] = '2.0'; |
|
27 | 27 | |
28 | 28 | return $fileType; |
29 | 29 | } |
@@ -3,16 +3,16 @@ |
||
3 | 3 | namespace Matecat\XliffParser\XliffUtils\CheckPipeline; |
4 | 4 | |
5 | 5 | class CheckSDL implements CheckInterface { |
6 | - public function check( $tmp ) { |
|
6 | + public function check($tmp) { |
|
7 | 7 | $fileType = []; |
8 | 8 | |
9 | - if ( isset( $tmp[ 0 ] ) ) { |
|
10 | - if ( stripos( $tmp[ 0 ], 'sdl:version' ) !== false ) { |
|
9 | + if (isset($tmp[0])) { |
|
10 | + if (stripos($tmp[0], 'sdl:version') !== false) { |
|
11 | 11 | //little trick, we consider not proprietary Sdlxliff files because we can handle them |
12 | - $fileType[ 'proprietary' ] = false; |
|
13 | - $fileType[ 'proprietary_name' ] = 'SDL Studio '; |
|
14 | - $fileType[ 'proprietary_short_name' ] = 'trados'; |
|
15 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
12 | + $fileType['proprietary'] = false; |
|
13 | + $fileType['proprietary_name'] = 'SDL Studio '; |
|
14 | + $fileType['proprietary_short_name'] = 'trados'; |
|
15 | + $fileType['converter_version'] = 'legacy'; |
|
16 | 16 | |
17 | 17 | return $fileType; |
18 | 18 | } |
@@ -8,20 +8,20 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - preg_match( '#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[ 0 ], $matches ); |
|
16 | - if ( !empty( $matches ) ) { |
|
17 | - $fileType[ 'proprietary' ] = false; |
|
18 | - $fileType[ 'proprietary_name' ] = 'MateCAT Converter'; |
|
19 | - $fileType[ 'proprietary_short_name' ] = 'matecat_converter'; |
|
20 | - if ( isset( $matches[ 2 ] ) ) { |
|
21 | - $fileType[ 'converter_version' ] = $matches[ 2 ]; |
|
14 | + if (isset($tmp[0])) { |
|
15 | + preg_match('#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[0], $matches); |
|
16 | + if (!empty($matches)) { |
|
17 | + $fileType['proprietary'] = false; |
|
18 | + $fileType['proprietary_name'] = 'MateCAT Converter'; |
|
19 | + $fileType['proprietary_short_name'] = 'matecat_converter'; |
|
20 | + if (isset($matches[2])) { |
|
21 | + $fileType['converter_version'] = $matches[2]; |
|
22 | 22 | } else { |
23 | 23 | // First converter release didn't specify version |
24 | - $fileType[ 'converter_version' ] = '1.0'; |
|
24 | + $fileType['converter_version'] = '1.0'; |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | return $fileType; |
@@ -8,5 +8,5 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ); |
|
11 | + public function check($tmp); |
|
12 | 12 | } |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | /** |
10 | 10 | * @var array |
11 | 11 | */ |
12 | - private static $versions_1 = [ '1.0', '1.1', '1.2' ]; |
|
12 | + private static $versions_1 = ['1.0', '1.1', '1.2']; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * @var array |
16 | 16 | */ |
17 | - private static $versions_2 = [ '2.0', '2.1' ]; |
|
17 | + private static $versions_2 = ['2.0', '2.1']; |
|
18 | 18 | |
19 | 19 | /** |
20 | 20 | * @param string $xliffContent |
@@ -23,16 +23,16 @@ discard block |
||
23 | 23 | * @throws NotSupportedVersionException |
24 | 24 | * @throws NotValidFileException |
25 | 25 | */ |
26 | - public static function detect( $xliffContent ) { |
|
27 | - preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $xliffContent, 0, 1000 ), $versionMatches ); |
|
26 | + public static function detect($xliffContent) { |
|
27 | + preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($xliffContent, 0, 1000), $versionMatches); |
|
28 | 28 | |
29 | - if ( empty( $versionMatches ) ) { |
|
30 | - throw new NotValidFileException( 'This is not a valid xliff file' ); |
|
29 | + if (empty($versionMatches)) { |
|
30 | + throw new NotValidFileException('This is not a valid xliff file'); |
|
31 | 31 | } |
32 | 32 | |
33 | - $version = $versionMatches[ 1 ]; |
|
33 | + $version = $versionMatches[1]; |
|
34 | 34 | |
35 | - return self::resolveVersion( $version ); |
|
35 | + return self::resolveVersion($version); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
@@ -41,15 +41,15 @@ discard block |
||
41 | 41 | * @return int |
42 | 42 | * @throws NotSupportedVersionException |
43 | 43 | */ |
44 | - private static function resolveVersion( $version ) { |
|
45 | - if ( in_array( $version, self::$versions_1 ) ) { |
|
44 | + private static function resolveVersion($version) { |
|
45 | + if (in_array($version, self::$versions_1)) { |
|
46 | 46 | return 1; |
47 | 47 | } |
48 | 48 | |
49 | - if ( in_array( $version, self::$versions_2 ) ) { |
|
49 | + if (in_array($version, self::$versions_2)) { |
|
50 | 50 | return 2; |
51 | 51 | } |
52 | 52 | |
53 | - throw new NotSupportedVersionException( 'Not supported version' ); |
|
53 | + throw new NotSupportedVersionException('Not supported version'); |
|
54 | 54 | } |
55 | 55 | } |