@@ -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, $string, $dataRefEndMap ); |
|
| 72 | + $this->extractDataRefMapRecursively($node, $string, $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 |
@@ -219,12 +219,12 @@ discard block |
||
| 219 | 219 | * @param string $completeString |
| 220 | 220 | * @param ArrayList $dataRefEndMap |
| 221 | 221 | */ |
| 222 | - private function extractDataRefMapRecursively( $node, $completeString, ArrayList $dataRefEndMap ) { |
|
| 222 | + private function extractDataRefMapRecursively($node, $completeString, ArrayList $dataRefEndMap) { |
|
| 223 | 223 | |
| 224 | 224 | // we have to build the map for the closing pc tag, so get the children first |
| 225 | - if ( $node->has_children ) { |
|
| 226 | - foreach ( $node->inner_html as $nestedNode ) { |
|
| 227 | - $this->extractDataRefMapRecursively( $nestedNode, $completeString, $dataRefEndMap ); |
|
| 225 | + if ($node->has_children) { |
|
| 226 | + foreach ($node->inner_html as $nestedNode) { |
|
| 227 | + $this->extractDataRefMapRecursively($nestedNode, $completeString, $dataRefEndMap); |
|
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | |
@@ -237,15 +237,15 @@ discard block |
||
| 237 | 237 | // becomes |
| 238 | 238 | // <pc id="source5" dataRefStart="source5"></pc> |
| 239 | 239 | // |
| 240 | - $isATagPairWithEmptyTextNode = strpos( $completeString, substr( $node->node, 0, -2 ) . '></pc>' ) !== false; |
|
| 240 | + $isATagPairWithEmptyTextNode = strpos($completeString, substr($node->node, 0, -2).'></pc>') !== false; |
|
| 241 | 241 | |
| 242 | - if ( $node->tagName === 'pc' && ( $node->self_closed === false || $isATagPairWithEmptyTextNode ) ) { |
|
| 242 | + if ($node->tagName === 'pc' && ($node->self_closed === false || $isATagPairWithEmptyTextNode)) { |
|
| 243 | 243 | |
| 244 | - $attributesMap = Map::instance( $node->attributes ); |
|
| 245 | - $dataRefEnd = $attributesMap->getOrDefault( 'dataRefEnd', $attributesMap->get( 'dataRefStart' ) ); |
|
| 244 | + $attributesMap = Map::instance($node->attributes); |
|
| 245 | + $dataRefEnd = $attributesMap->getOrDefault('dataRefEnd', $attributesMap->get('dataRefStart')); |
|
| 246 | 246 | |
| 247 | 247 | $dataRefEndMap[] = [ |
| 248 | - 'id' => $attributesMap->get( 'id' ), |
|
| 248 | + 'id' => $attributesMap->get('id'), |
|
| 249 | 249 | 'dataRefEnd' => $dataRefEnd, |
| 250 | 250 | ]; |
| 251 | 251 | |
@@ -263,31 +263,31 @@ discard block |
||
| 263 | 263 | * @throws InvalidXmlException |
| 264 | 264 | * @throws XmlParsingException |
| 265 | 265 | */ |
| 266 | - private function replaceOpeningPcTags( $string ) { |
|
| 266 | + private function replaceOpeningPcTags($string) { |
|
| 267 | 267 | |
| 268 | - preg_match_all( '|<pc ([^>/]+?)>|iu', $string, $openingPcMatches ); |
|
| 268 | + preg_match_all('|<pc ([^>/]+?)>|iu', $string, $openingPcMatches); |
|
| 269 | 269 | |
| 270 | - foreach ( $openingPcMatches[ 0 ] as $match ) { |
|
| 270 | + foreach ($openingPcMatches[0] as $match) { |
|
| 271 | 271 | |
| 272 | - $node = XmlParser::parse( $match . '</pc>', true )[ 0 ]; // add a closing tag to not break xml integrity |
|
| 272 | + $node = XmlParser::parse($match.'</pc>', true)[0]; // add a closing tag to not break xml integrity |
|
| 273 | 273 | |
| 274 | 274 | // CASE 1 - Missing `dataRefStart` |
| 275 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) && !isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 276 | - $node->attributes[ 'dataRefStart' ] = $node->attributes[ 'dataRefEnd' ]; |
|
| 275 | + if (isset($node->attributes['dataRefEnd']) && !isset($node->attributes['dataRefStart'])) { |
|
| 276 | + $node->attributes['dataRefStart'] = $node->attributes['dataRefEnd']; |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | // CASE 2 - Missing `dataRefEnd` |
| 280 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && !isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
| 281 | - $node->attributes[ 'dataRefEnd' ] = $node->attributes[ 'dataRefStart' ]; |
|
| 280 | + if (isset($node->attributes['dataRefStart']) && !isset($node->attributes['dataRefEnd'])) { |
|
| 281 | + $node->attributes['dataRefEnd'] = $node->attributes['dataRefStart']; |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 284 | + if (isset($node->attributes['dataRefStart'])) { |
|
| 285 | 285 | |
| 286 | - $attributesMap = Map::instance( $node->attributes ); |
|
| 286 | + $attributesMap = Map::instance($node->attributes); |
|
| 287 | 287 | $string = $this->replaceNewTagString( |
| 288 | 288 | $match, |
| 289 | - $attributesMap->get( 'id' ), |
|
| 290 | - $this->map->getOrDefault( $node->attributes[ 'dataRefStart' ], 'NULL' ), |
|
| 289 | + $attributesMap->get('id'), |
|
| 290 | + $this->map->getOrDefault($node->attributes['dataRefStart'], 'NULL'), |
|
| 291 | 291 | CTypeEnum::PC_OPEN_DATA_REF, |
| 292 | 292 | $string |
| 293 | 293 | ); |
@@ -307,31 +307,31 @@ discard block |
||
| 307 | 307 | * |
| 308 | 308 | * @return string |
| 309 | 309 | */ |
| 310 | - private function replaceClosingPcTags( $string, ArrayList $dataRefEndMap ) { |
|
| 310 | + private function replaceClosingPcTags($string, ArrayList $dataRefEndMap) { |
|
| 311 | 311 | |
| 312 | - preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
| 312 | + preg_match_all('|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
| 313 | 313 | $delta = 0; |
| 314 | 314 | |
| 315 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
| 315 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
| 316 | 316 | |
| 317 | - $offset = $match[ 1 ]; |
|
| 317 | + $offset = $match[1]; |
|
| 318 | 318 | $length = 5; // strlen of '</pc>' |
| 319 | 319 | |
| 320 | - $attr = $dataRefEndMap->get( $index ); |
|
| 321 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
| 320 | + $attr = $dataRefEndMap->get($index); |
|
| 321 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
| 322 | 322 | |
| 323 | 323 | // conversion for opening <pc> tag |
| 324 | 324 | $completeTag = $this->getNewTagString( |
| 325 | 325 | '</pc>', |
| 326 | - $attr[ 'id' ], |
|
| 327 | - $this->map->getOrDefault( $attr[ 'dataRefEnd' ], 'NULL' ), |
|
| 326 | + $attr['id'], |
|
| 327 | + $this->map->getOrDefault($attr['dataRefEnd'], 'NULL'), |
|
| 328 | 328 | CTypeEnum::PC_CLOSE_DATA_REF, |
| 329 | 329 | '_2' |
| 330 | 330 | ); |
| 331 | 331 | |
| 332 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
| 333 | - $string = substr_replace( $string, $completeTag, $realOffset, $length ); |
|
| 334 | - $delta = $delta + strlen( $completeTag ) - $length; |
|
| 332 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
| 333 | + $string = substr_replace($string, $completeTag, $realOffset, $length); |
|
| 334 | + $delta = $delta + strlen($completeTag) - $length; |
|
| 335 | 335 | |
| 336 | 336 | } |
| 337 | 337 | |
@@ -349,17 +349,17 @@ discard block |
||
| 349 | 349 | * @throws InvalidXmlException |
| 350 | 350 | * @throws XmlParsingException |
| 351 | 351 | */ |
| 352 | - public function restore( $string ) { |
|
| 352 | + public function restore($string) { |
|
| 353 | 353 | |
| 354 | 354 | // if the map is empty return string as is |
| 355 | - if ( empty( $this->map ) ) { |
|
| 355 | + if (empty($this->map)) { |
|
| 356 | 356 | return $string; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - $html = XmlParser::parse( $string, true ); |
|
| 359 | + $html = XmlParser::parse($string, true); |
|
| 360 | 360 | |
| 361 | - foreach ( $html as $node ) { |
|
| 362 | - $string = $this->recursiveRestoreOriginalTags( $node, $string ); |
|
| 361 | + foreach ($html as $node) { |
|
| 362 | + $string = $this->recursiveRestoreOriginalTags($node, $string); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | return $string; |
@@ -371,26 +371,26 @@ discard block |
||
| 371 | 371 | * |
| 372 | 372 | * @return string |
| 373 | 373 | */ |
| 374 | - private function recursiveRestoreOriginalTags( $node, $string ) { |
|
| 374 | + private function recursiveRestoreOriginalTags($node, $string) { |
|
| 375 | 375 | |
| 376 | - if ( $node->has_children ) { |
|
| 376 | + if ($node->has_children) { |
|
| 377 | 377 | |
| 378 | - foreach ( $node->inner_html as $childNode ) { |
|
| 379 | - $string = $this->recursiveRestoreOriginalTags( $childNode, $string ); |
|
| 378 | + foreach ($node->inner_html as $childNode) { |
|
| 379 | + $string = $this->recursiveRestoreOriginalTags($childNode, $string); |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | } else { |
| 383 | 383 | |
| 384 | - $nodeAttributesMap = Map::instance( $node->attributes ); |
|
| 384 | + $nodeAttributesMap = Map::instance($node->attributes); |
|
| 385 | 385 | |
| 386 | - if ( !$nodeAttributesMap->get( 'x-orig' ) ) { |
|
| 386 | + if (!$nodeAttributesMap->get('x-orig')) { |
|
| 387 | 387 | return $string; |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | - $cType = $nodeAttributesMap->get( 'ctype' ); |
|
| 390 | + $cType = $nodeAttributesMap->get('ctype'); |
|
| 391 | 391 | |
| 392 | - if ( CTypeEnum::isLayer2Constant( $cType ) ) { |
|
| 393 | - return preg_replace( '/' . preg_quote( $node->node, '/' ) . '/', base64_decode( $nodeAttributesMap->get( 'x-orig' ) ), $string, 1 ); |
|
| 392 | + if (CTypeEnum::isLayer2Constant($cType)) { |
|
| 393 | + return preg_replace('/'.preg_quote($node->node, '/').'/', base64_decode($nodeAttributesMap->get('x-orig')), $string, 1); |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | } |
@@ -408,24 +408,24 @@ discard block |
||
| 408 | 408 | * |
| 409 | 409 | * @return string |
| 410 | 410 | */ |
| 411 | - private function getNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue = null ) { |
|
| 411 | + private function getNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue = null) { |
|
| 412 | 412 | |
| 413 | - $newTag = [ '<ph' ]; |
|
| 413 | + $newTag = ['<ph']; |
|
| 414 | 414 | |
| 415 | - if ( isset( $id ) ) { |
|
| 416 | - $newTag[] = 'id="' . $id . $upCountIdValue . '"'; |
|
| 415 | + if (isset($id)) { |
|
| 416 | + $newTag[] = 'id="'.$id.$upCountIdValue.'"'; |
|
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - $newTag[] = 'ctype="' . $ctype . '"'; |
|
| 420 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"'; |
|
| 421 | - $newTag[] = 'x-orig="' . base64_encode( $actualNodeString ) . '"'; |
|
| 419 | + $newTag[] = 'ctype="'.$ctype.'"'; |
|
| 420 | + $newTag[] = 'equiv-text="base64:'.base64_encode($dataRefValue).'"'; |
|
| 421 | + $newTag[] = 'x-orig="'.base64_encode($actualNodeString).'"'; |
|
| 422 | 422 | |
| 423 | - return implode( " ", $newTag ) . '/>'; |
|
| 423 | + return implode(" ", $newTag).'/>'; |
|
| 424 | 424 | |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | - private function replaceNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1' ) { |
|
| 428 | - return str_replace( $actualNodeString, $this->getNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue ), $originalString ); |
|
| 427 | + private function replaceNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1') { |
|
| 428 | + return str_replace($actualNodeString, $this->getNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue), $originalString); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | } |
| 432 | 432 | \ No newline at end of file |