@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @param array $map |
29 | 29 | */ |
30 | - public function __construct( array $map = [] ) { |
|
31 | - $this->map = Map::instance( $this->sanitizeMap( $map ) ); |
|
30 | + public function __construct(array $map = []) { |
|
31 | + $this->map = Map::instance($this->sanitizeMap($map)); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -42,42 +42,42 @@ discard block |
||
42 | 42 | * |
43 | 43 | * @return string |
44 | 44 | */ |
45 | - public function replace( $string ) { |
|
45 | + public function replace($string) { |
|
46 | 46 | |
47 | 47 | // if the map is empty |
48 | 48 | // or the string has not a dataRef attribute |
49 | 49 | // return string as is |
50 | - if ( $this->map->isEmpty() || !$this->hasAnyDataRefAttribute( $string ) ) { |
|
50 | + if ($this->map->isEmpty() || !$this->hasAnyDataRefAttribute($string)) { |
|
51 | 51 | return $string; |
52 | 52 | } |
53 | 53 | |
54 | 54 | // try not to throw exception for wrong segments with opening tags and no closing |
55 | 55 | try { |
56 | 56 | |
57 | - $html = XmlParser::parse( $string, true ); |
|
57 | + $html = XmlParser::parse($string, true); |
|
58 | 58 | |
59 | 59 | $dataRefEndMap = new ArrayList(); |
60 | 60 | |
61 | - foreach ( $html as $node ) { |
|
61 | + foreach ($html as $node) { |
|
62 | 62 | |
63 | 63 | // 1. Replace <ph>|<sc>|<ec> tags |
64 | - $string = $this->recursiveTransformDataRefToPhTag( $node, $string ); |
|
64 | + $string = $this->recursiveTransformDataRefToPhTag($node, $string); |
|
65 | 65 | |
66 | 66 | // 2. Replace self-closed <pc dataRefStart="xyz" /> tags |
67 | - $string = $this->recursiveReplaceSelfClosedPcTags( $node, $string ); |
|
67 | + $string = $this->recursiveReplaceSelfClosedPcTags($node, $string); |
|
68 | 68 | |
69 | 69 | // 3. Build the DataRefEndMap needed by replaceClosingPcTags function |
70 | 70 | // (needed for correct handling of </pc> closing tags) |
71 | 71 | // make this inline with one foreach cycle |
72 | - $this->extractDataRefMapRecursively( $node, $dataRefEndMap ); |
|
72 | + $this->extractDataRefMapRecursively($node, $dataRefEndMap); |
|
73 | 73 | |
74 | 74 | } |
75 | 75 | |
76 | 76 | // 4. replace pc tags |
77 | - $string = $this->replaceOpeningPcTags( $string ); |
|
78 | - $string = $this->replaceClosingPcTags( $string, $dataRefEndMap ); |
|
77 | + $string = $this->replaceOpeningPcTags($string); |
|
78 | + $string = $this->replaceClosingPcTags($string, $dataRefEndMap); |
|
79 | 79 | |
80 | - } catch ( Exception $ignore ) { |
|
80 | + } catch (Exception $ignore) { |
|
81 | 81 | // if something fails here, do not throw exception and return the original string instead |
82 | 82 | // var_dump( $ignore ); |
83 | 83 | } finally { |
@@ -91,8 +91,8 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return bool |
93 | 93 | */ |
94 | - private function hasAnyDataRefAttribute( $string ) { |
|
95 | - return (bool)preg_match( '/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string ); |
|
94 | + private function hasAnyDataRefAttribute($string) { |
|
95 | + return (bool) preg_match('/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -108,18 +108,18 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @return string |
110 | 110 | */ |
111 | - private function recursiveTransformDataRefToPhTag( $node, $string ) { |
|
111 | + private function recursiveTransformDataRefToPhTag($node, $string) { |
|
112 | 112 | |
113 | - if ( $node->has_children ) { |
|
113 | + if ($node->has_children) { |
|
114 | 114 | |
115 | - foreach ( $node->inner_html as $childNode ) { |
|
116 | - $string = $this->recursiveTransformDataRefToPhTag( $childNode, $string ); |
|
115 | + foreach ($node->inner_html as $childNode) { |
|
116 | + $string = $this->recursiveTransformDataRefToPhTag($childNode, $string); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | } else { |
120 | 120 | |
121 | 121 | // accept only those tags |
122 | - switch ( $node->tagName ) { |
|
122 | + switch ($node->tagName) { |
|
123 | 123 | case 'ph': |
124 | 124 | $ctype = CTypeEnum::PH_DATA_REF; |
125 | 125 | break; |
@@ -134,17 +134,17 @@ discard block |
||
134 | 134 | } |
135 | 135 | |
136 | 136 | // if isset a value in the map proceed with conversion otherwise skip |
137 | - $attributesMap = Map::instance( $node->attributes ); |
|
138 | - if ( !$this->map->get( $attributesMap->get( 'dataRef' ) ) ) { |
|
137 | + $attributesMap = Map::instance($node->attributes); |
|
138 | + if (!$this->map->get($attributesMap->get('dataRef'))) { |
|
139 | 139 | return $string; |
140 | 140 | } |
141 | 141 | |
142 | - $dataRefName = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
142 | + $dataRefName = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
143 | 143 | |
144 | 144 | return $this->replaceNewTagString( |
145 | 145 | $node->node, |
146 | - $attributesMap->getOrDefault( 'id', $dataRefName ), |
|
147 | - $this->map->getOrDefault( $node->attributes[ 'dataRef' ], 'NULL' ), |
|
146 | + $attributesMap->getOrDefault('id', $dataRefName), |
|
147 | + $this->map->getOrDefault($node->attributes['dataRef'], 'NULL'), |
|
148 | 148 | $ctype, |
149 | 149 | $string, |
150 | 150 | null |
@@ -162,11 +162,11 @@ discard block |
||
162 | 162 | * |
163 | 163 | * @return array |
164 | 164 | */ |
165 | - private function sanitizeMap( $map ) { |
|
165 | + private function sanitizeMap($map) { |
|
166 | 166 | |
167 | - foreach ( $map as $name => $value ) { |
|
168 | - if ( is_null( $value ) || $value === '' ) { |
|
169 | - $map[ $name ] = 'NULL'; |
|
167 | + foreach ($map as $name => $value) { |
|
168 | + if (is_null($value) || $value === '') { |
|
169 | + $map[$name] = 'NULL'; |
|
170 | 170 | } |
171 | 171 | } |
172 | 172 | |
@@ -182,23 +182,23 @@ discard block |
||
182 | 182 | * @throws InvalidXmlException |
183 | 183 | * @throws XmlParsingException |
184 | 184 | */ |
185 | - private function recursiveReplaceSelfClosedPcTags( $node, $string ) { |
|
185 | + private function recursiveReplaceSelfClosedPcTags($node, $string) { |
|
186 | 186 | |
187 | - if ( $node->has_children ) { |
|
187 | + if ($node->has_children) { |
|
188 | 188 | |
189 | - foreach ( $node->inner_html as $childNode ) { |
|
190 | - $string = $this->recursiveReplaceSelfClosedPcTags( $childNode, $string ); |
|
189 | + foreach ($node->inner_html as $childNode) { |
|
190 | + $string = $this->recursiveReplaceSelfClosedPcTags($childNode, $string); |
|
191 | 191 | } |
192 | 192 | |
193 | - } elseif ( $node->tagName == 'pc' && $node->self_closed === true ) { |
|
193 | + } elseif ($node->tagName == 'pc' && $node->self_closed === true) { |
|
194 | 194 | |
195 | - $attributesMap = Map::instance( $node->attributes ); |
|
195 | + $attributesMap = Map::instance($node->attributes); |
|
196 | 196 | |
197 | - if ( $dataRefStartValue = $this->map->get( $node->attributes[ 'dataRefStart' ] ) ) { |
|
197 | + if ($dataRefStartValue = $this->map->get($node->attributes['dataRefStart'])) { |
|
198 | 198 | |
199 | 199 | $string = $this->replaceNewTagString( |
200 | 200 | $node->node, |
201 | - $attributesMap->get( 'id' ), |
|
201 | + $attributesMap->get('id'), |
|
202 | 202 | $dataRefStartValue, |
203 | 203 | CTypeEnum::PC_SELF_CLOSE_DATA_REF, |
204 | 204 | $string |
@@ -218,23 +218,23 @@ discard block |
||
218 | 218 | * @param object $node |
219 | 219 | * @param ArrayList $dataRefEndMap |
220 | 220 | */ |
221 | - private function extractDataRefMapRecursively( $node, ArrayList $dataRefEndMap ) { |
|
221 | + private function extractDataRefMapRecursively($node, ArrayList $dataRefEndMap) { |
|
222 | 222 | |
223 | 223 | // we have to build the map for the closing pc tag, so get the children first |
224 | - if ( $node->has_children ) { |
|
225 | - foreach ( $node->inner_html as $nestedNode ) { |
|
226 | - $this->extractDataRefMapRecursively( $nestedNode, $dataRefEndMap ); |
|
224 | + if ($node->has_children) { |
|
225 | + foreach ($node->inner_html as $nestedNode) { |
|
226 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
227 | 227 | } |
228 | 228 | } |
229 | 229 | |
230 | 230 | // EXCLUDE self closed <pc/> |
231 | - if ( $node->tagName === 'pc' && $node->self_closed === false ) { |
|
231 | + if ($node->tagName === 'pc' && $node->self_closed === false) { |
|
232 | 232 | |
233 | - $attributesMap = Map::instance( $node->attributes ); |
|
234 | - $dataRefEnd = $attributesMap->getOrDefault( 'dataRefEnd', $attributesMap->get( 'dataRefStart' ) ); |
|
233 | + $attributesMap = Map::instance($node->attributes); |
|
234 | + $dataRefEnd = $attributesMap->getOrDefault('dataRefEnd', $attributesMap->get('dataRefStart')); |
|
235 | 235 | |
236 | 236 | $dataRefEndMap[] = [ |
237 | - 'id' => $attributesMap->get( 'id' ), |
|
237 | + 'id' => $attributesMap->get('id'), |
|
238 | 238 | 'dataRefEnd' => $dataRefEnd, |
239 | 239 | ]; |
240 | 240 | |
@@ -252,31 +252,31 @@ discard block |
||
252 | 252 | * @throws InvalidXmlException |
253 | 253 | * @throws XmlParsingException |
254 | 254 | */ |
255 | - private function replaceOpeningPcTags( $string ) { |
|
255 | + private function replaceOpeningPcTags($string) { |
|
256 | 256 | |
257 | - preg_match_all( '|<pc ([^>/]+?)>|iu', $string, $openingPcMatches ); |
|
257 | + preg_match_all('|<pc ([^>/]+?)>|iu', $string, $openingPcMatches); |
|
258 | 258 | |
259 | - foreach ( $openingPcMatches[ 0 ] as $match ) { |
|
259 | + foreach ($openingPcMatches[0] as $match) { |
|
260 | 260 | |
261 | - $node = XmlParser::parse( $match . '</pc>', true )[ 0 ]; // add a closing tag to not break xml integrity |
|
261 | + $node = XmlParser::parse($match.'</pc>', true)[0]; // add a closing tag to not break xml integrity |
|
262 | 262 | |
263 | 263 | // CASE 1 - Missing `dataRefStart` |
264 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) && !isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
265 | - $node->attributes[ 'dataRefStart' ] = $node->attributes[ 'dataRefEnd' ]; |
|
264 | + if (isset($node->attributes['dataRefEnd']) && !isset($node->attributes['dataRefStart'])) { |
|
265 | + $node->attributes['dataRefStart'] = $node->attributes['dataRefEnd']; |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | // CASE 2 - Missing `dataRefEnd` |
269 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && !isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
270 | - $node->attributes[ 'dataRefEnd' ] = $node->attributes[ 'dataRefStart' ]; |
|
269 | + if (isset($node->attributes['dataRefStart']) && !isset($node->attributes['dataRefEnd'])) { |
|
270 | + $node->attributes['dataRefEnd'] = $node->attributes['dataRefStart']; |
|
271 | 271 | } |
272 | 272 | |
273 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
273 | + if (isset($node->attributes['dataRefStart'])) { |
|
274 | 274 | |
275 | - $attributesMap = Map::instance( $node->attributes ); |
|
275 | + $attributesMap = Map::instance($node->attributes); |
|
276 | 276 | $string = $this->replaceNewTagString( |
277 | 277 | $match, |
278 | - $attributesMap->get( 'id' ), |
|
279 | - $this->map->getOrDefault( $node->attributes[ 'dataRefStart' ], 'NULL' ), |
|
278 | + $attributesMap->get('id'), |
|
279 | + $this->map->getOrDefault($node->attributes['dataRefStart'], 'NULL'), |
|
280 | 280 | CTypeEnum::PC_OPEN_DATA_REF, |
281 | 281 | $string |
282 | 282 | ); |
@@ -296,31 +296,31 @@ discard block |
||
296 | 296 | * |
297 | 297 | * @return string |
298 | 298 | */ |
299 | - private function replaceClosingPcTags( $string, ArrayList $dataRefEndMap ) { |
|
299 | + private function replaceClosingPcTags($string, ArrayList $dataRefEndMap) { |
|
300 | 300 | |
301 | - preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
301 | + preg_match_all('|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
302 | 302 | $delta = 0; |
303 | 303 | |
304 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
304 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
305 | 305 | |
306 | - $offset = $match[ 1 ]; |
|
306 | + $offset = $match[1]; |
|
307 | 307 | $length = 5; // strlen of '</pc>' |
308 | 308 | |
309 | - $attr = $dataRefEndMap->get( $index ); |
|
310 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
309 | + $attr = $dataRefEndMap->get($index); |
|
310 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
311 | 311 | |
312 | 312 | // conversion for opening <pc> tag |
313 | 313 | $completeTag = $this->getNewTagString( |
314 | 314 | '</pc>', |
315 | - $attr[ 'id' ], |
|
316 | - $this->map->getOrDefault( $attr[ 'dataRefEnd' ], 'NULL' ), |
|
315 | + $attr['id'], |
|
316 | + $this->map->getOrDefault($attr['dataRefEnd'], 'NULL'), |
|
317 | 317 | CTypeEnum::PC_CLOSE_DATA_REF, |
318 | 318 | '_2' |
319 | 319 | ); |
320 | 320 | |
321 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
322 | - $string = substr_replace( $string, $completeTag, $realOffset, $length ); |
|
323 | - $delta = $delta + strlen( $completeTag ) - $length; |
|
321 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
322 | + $string = substr_replace($string, $completeTag, $realOffset, $length); |
|
323 | + $delta = $delta + strlen($completeTag) - $length; |
|
324 | 324 | |
325 | 325 | } |
326 | 326 | |
@@ -338,17 +338,17 @@ discard block |
||
338 | 338 | * @throws InvalidXmlException |
339 | 339 | * @throws XmlParsingException |
340 | 340 | */ |
341 | - public function restore( $string ) { |
|
341 | + public function restore($string) { |
|
342 | 342 | |
343 | 343 | // if the map is empty return string as is |
344 | - if ( empty( $this->map ) ) { |
|
344 | + if (empty($this->map)) { |
|
345 | 345 | return $string; |
346 | 346 | } |
347 | 347 | |
348 | - $html = XmlParser::parse( $string, true ); |
|
348 | + $html = XmlParser::parse($string, true); |
|
349 | 349 | |
350 | - foreach ( $html as $node ) { |
|
351 | - $string = $this->recursiveRestoreOriginalTags( $node, $string ); |
|
350 | + foreach ($html as $node) { |
|
351 | + $string = $this->recursiveRestoreOriginalTags($node, $string); |
|
352 | 352 | } |
353 | 353 | |
354 | 354 | return $string; |
@@ -360,26 +360,26 @@ discard block |
||
360 | 360 | * |
361 | 361 | * @return string |
362 | 362 | */ |
363 | - private function recursiveRestoreOriginalTags( $node, $string ) { |
|
363 | + private function recursiveRestoreOriginalTags($node, $string) { |
|
364 | 364 | |
365 | - if ( $node->has_children ) { |
|
365 | + if ($node->has_children) { |
|
366 | 366 | |
367 | - foreach ( $node->inner_html as $childNode ) { |
|
368 | - $string = $this->recursiveRestoreOriginalTags( $childNode, $string ); |
|
367 | + foreach ($node->inner_html as $childNode) { |
|
368 | + $string = $this->recursiveRestoreOriginalTags($childNode, $string); |
|
369 | 369 | } |
370 | 370 | |
371 | 371 | } else { |
372 | 372 | |
373 | - $nodeAttributesMap = Map::instance( $node->attributes ); |
|
373 | + $nodeAttributesMap = Map::instance($node->attributes); |
|
374 | 374 | |
375 | - if ( !$nodeAttributesMap->get( 'x-orig' ) ) { |
|
375 | + if (!$nodeAttributesMap->get('x-orig')) { |
|
376 | 376 | return $string; |
377 | 377 | } |
378 | 378 | |
379 | - $cType = $nodeAttributesMap->get( 'ctype' ); |
|
379 | + $cType = $nodeAttributesMap->get('ctype'); |
|
380 | 380 | |
381 | - if ( CTypeEnum::isLayer2Constant( $cType ) ) { |
|
382 | - return preg_replace( '/' . preg_quote( $node->node, '/' ) . '/', base64_decode( $nodeAttributesMap->get( 'x-orig' ) ), $string, 1 ); |
|
381 | + if (CTypeEnum::isLayer2Constant($cType)) { |
|
382 | + return preg_replace('/'.preg_quote($node->node, '/').'/', base64_decode($nodeAttributesMap->get('x-orig')), $string, 1); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | } |
@@ -397,24 +397,24 @@ discard block |
||
397 | 397 | * |
398 | 398 | * @return string |
399 | 399 | */ |
400 | - private function getNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue = null ) { |
|
400 | + private function getNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue = null) { |
|
401 | 401 | |
402 | - $newTag = [ '<ph' ]; |
|
402 | + $newTag = ['<ph']; |
|
403 | 403 | |
404 | - if ( isset( $id ) ) { |
|
405 | - $newTag[] = 'id="' . $id . $upCountIdValue . '"'; |
|
404 | + if (isset($id)) { |
|
405 | + $newTag[] = 'id="'.$id.$upCountIdValue.'"'; |
|
406 | 406 | } |
407 | 407 | |
408 | - $newTag[] = 'ctype="' . $ctype . '"'; |
|
409 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"'; |
|
410 | - $newTag[] = 'x-orig="' . base64_encode( $actualNodeString ) . '"'; |
|
408 | + $newTag[] = 'ctype="'.$ctype.'"'; |
|
409 | + $newTag[] = 'equiv-text="base64:'.base64_encode($dataRefValue).'"'; |
|
410 | + $newTag[] = 'x-orig="'.base64_encode($actualNodeString).'"'; |
|
411 | 411 | |
412 | - return implode( " ", $newTag ) . '/>'; |
|
412 | + return implode(" ", $newTag).'/>'; |
|
413 | 413 | |
414 | 414 | } |
415 | 415 | |
416 | - private function replaceNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1' ) { |
|
417 | - return str_replace( $actualNodeString, $this->getNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue ), $originalString ); |
|
416 | + private function replaceNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1') { |
|
417 | + return str_replace($actualNodeString, $this->getNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue), $originalString); |
|
418 | 418 | } |
419 | 419 | |
420 | 420 | } |
421 | 421 | \ No newline at end of file |