@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @param array $map |
| 29 | 29 | */ |
| 30 | - public function __construct( array $map = null ) { |
|
| 30 | + public function __construct(array $map = null) { |
|
| 31 | 31 | $this->map = $map; |
| 32 | 32 | } |
| 33 | 33 | |
@@ -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 map is empty |
| 48 | 48 | // or the string has not a dataRef attribute |
| 49 | 49 | // return string as is |
| 50 | - if ( empty( $this->map ) || !$this->hasAnyDataRefAttribute( $string ) ) { |
|
| 50 | + if (empty($this->map) || !$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 = []; |
| 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 | } finally { |
| 83 | 83 | return $string; |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return bool |
| 92 | 92 | */ |
| 93 | - private function hasAnyDataRefAttribute( $string ) { |
|
| 94 | - return (bool)preg_match( '/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string ); |
|
| 93 | + private function hasAnyDataRefAttribute($string) { |
|
| 94 | + return (bool) preg_match('/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -107,17 +107,17 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return string |
| 109 | 109 | */ |
| 110 | - private function recursiveTransformDataRefToPhTag( $node, $string ) { |
|
| 110 | + private function recursiveTransformDataRefToPhTag($node, $string) { |
|
| 111 | 111 | |
| 112 | - if ( $node->has_children ) { |
|
| 112 | + if ($node->has_children) { |
|
| 113 | 113 | |
| 114 | - foreach ( $node->inner_html as $childNode ) { |
|
| 115 | - $string = $this->recursiveTransformDataRefToPhTag( $childNode, $string ); |
|
| 114 | + foreach ($node->inner_html as $childNode) { |
|
| 115 | + $string = $this->recursiveTransformDataRefToPhTag($childNode, $string); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | } else { |
| 119 | 119 | |
| 120 | - switch ( $node->tagName ) { |
|
| 120 | + switch ($node->tagName) { |
|
| 121 | 121 | case 'ph': |
| 122 | 122 | case 'sc': |
| 123 | 123 | case 'ec': |
@@ -126,48 +126,48 @@ discard block |
||
| 126 | 126 | return $string; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
| 129 | + if (!isset($node->attributes['dataRef'])) { |
|
| 130 | 130 | return $string; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | // if isset a value in the map calculate base64 encoded value |
| 134 | 134 | // otherwise skip |
| 135 | - if ( !in_array( $node->attributes[ 'dataRef' ], array_keys( $this->map ) ) ) { |
|
| 135 | + if (!in_array($node->attributes['dataRef'], array_keys($this->map))) { |
|
| 136 | 136 | return $string; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - $dataRefName = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
| 140 | - $dataRefValue = $this->map[ $dataRefName ]; // map identifier. Eg: source1 |
|
| 139 | + $dataRefName = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
| 140 | + $dataRefValue = $this->map[$dataRefName]; // map identifier. Eg: source1 |
|
| 141 | 141 | |
| 142 | 142 | // check if is null or an empty string, in this case, convert it to NULL string |
| 143 | - if ( is_null( $dataRefValue ) || $dataRefValue === '' ) { |
|
| 144 | - $this->map[ $dataRefName ] = 'NULL'; |
|
| 143 | + if (is_null($dataRefValue) || $dataRefValue === '') { |
|
| 144 | + $this->map[$dataRefName] = 'NULL'; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | |
| 148 | - $newTag = [ '<ph' ]; |
|
| 148 | + $newTag = ['<ph']; |
|
| 149 | 149 | |
| 150 | 150 | // if there is no id copy it from dataRef |
| 151 | - if ( !isset( $node->attributes[ 'id' ] ) ) { |
|
| 152 | - $newTag[] = 'id="' . $dataRefName . '"'; |
|
| 151 | + if (!isset($node->attributes['id'])) { |
|
| 152 | + $newTag[] = 'id="'.$dataRefName.'"'; |
|
| 153 | 153 | $newTag[] = 'x-removeId="true"'; |
| 154 | 154 | } else { |
| 155 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '"'; |
|
| 155 | + $newTag[] = 'id="'.$node->attributes['id'].'"'; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // introduce dataType for <ec>/<sc> tag handling |
| 159 | - if ( $node->tagName === 'ec' ) { |
|
| 160 | - $newTag[] = 'ctype="' . CTypeEnum::EC_DATA_REF . '"'; |
|
| 161 | - } elseif ( $node->tagName === 'sc' ) { |
|
| 162 | - $newTag[] = 'ctype="' . CTypeEnum::SC_DATA_REF . '"'; |
|
| 159 | + if ($node->tagName === 'ec') { |
|
| 160 | + $newTag[] = 'ctype="'.CTypeEnum::EC_DATA_REF.'"'; |
|
| 161 | + } elseif ($node->tagName === 'sc') { |
|
| 162 | + $newTag[] = 'ctype="'.CTypeEnum::SC_DATA_REF.'"'; |
|
| 163 | 163 | } else { |
| 164 | - $newTag[] = 'ctype="' . CTypeEnum::PH_DATA_REF . '"'; |
|
| 164 | + $newTag[] = 'ctype="'.CTypeEnum::PH_DATA_REF.'"'; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"'; |
|
| 168 | - $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
|
| 167 | + $newTag[] = 'equiv-text="base64:'.base64_encode($dataRefValue).'"'; |
|
| 168 | + $newTag[] = 'x-orig="'.base64_encode($node->node).'"'; |
|
| 169 | 169 | |
| 170 | - return str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
|
| 170 | + return str_replace($node->node, implode(" ", $newTag).'/>', $string); |
|
| 171 | 171 | |
| 172 | 172 | } |
| 173 | 173 | |
@@ -183,29 +183,29 @@ discard block |
||
| 183 | 183 | * @throws InvalidXmlException |
| 184 | 184 | * @throws XmlParsingException |
| 185 | 185 | */ |
| 186 | - private function recursiveReplaceSelfClosedPcTags( $node, $string ) { |
|
| 186 | + private function recursiveReplaceSelfClosedPcTags($node, $string) { |
|
| 187 | 187 | |
| 188 | - if ( $node->has_children ) { |
|
| 188 | + if ($node->has_children) { |
|
| 189 | 189 | |
| 190 | - foreach ( $node->inner_html as $childNode ) { |
|
| 191 | - $string = $this->recursiveReplaceSelfClosedPcTags( $childNode, $string ); |
|
| 190 | + foreach ($node->inner_html as $childNode) { |
|
| 191 | + $string = $this->recursiveReplaceSelfClosedPcTags($childNode, $string); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - } elseif ( $node->tagName == 'pc' && $node->self_closed === true ) { |
|
| 194 | + } elseif ($node->tagName == 'pc' && $node->self_closed === true) { |
|
| 195 | 195 | |
| 196 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
|
| 196 | + if (isset($node->attributes['dataRefStart']) && array_key_exists($node->attributes['dataRefStart'], $this->map)) { |
|
| 197 | 197 | |
| 198 | - $newTag = [ '<ph' ]; |
|
| 198 | + $newTag = ['<ph']; |
|
| 199 | 199 | |
| 200 | - if ( isset( $node->attributes[ 'id' ] ) ) { |
|
| 201 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '_1"'; |
|
| 200 | + if (isset($node->attributes['id'])) { |
|
| 201 | + $newTag[] = 'id="'.$node->attributes['id'].'_1"'; |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - $newTag[] = 'ctype="' . CTypeEnum::PC_SELF_CLOSE_DATA_REF . '"'; |
|
| 205 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"'; |
|
| 206 | - $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
|
| 204 | + $newTag[] = 'ctype="'.CTypeEnum::PC_SELF_CLOSE_DATA_REF.'"'; |
|
| 205 | + $newTag[] = 'equiv-text="base64:'.base64_encode($this->map[$node->attributes['dataRefStart']]).'"'; |
|
| 206 | + $newTag[] = 'x-orig="'.base64_encode($node->node).'"'; |
|
| 207 | 207 | |
| 208 | - $string = str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
|
| 208 | + $string = str_replace($node->node, implode(" ", $newTag).'/>', $string); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | } |
@@ -220,27 +220,27 @@ discard block |
||
| 220 | 220 | * @param object $node |
| 221 | 221 | * @param $dataRefEndMap |
| 222 | 222 | */ |
| 223 | - private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
|
| 223 | + private function extractDataRefMapRecursively($node, &$dataRefEndMap) { |
|
| 224 | 224 | |
| 225 | 225 | // we have to build the map for the closing pc tag, so get the children first |
| 226 | - if ( $node->has_children ) { |
|
| 227 | - foreach ( $node->inner_html as $nestedNode ) { |
|
| 228 | - $this->extractDataRefMapRecursively( $nestedNode, $dataRefEndMap ); |
|
| 226 | + if ($node->has_children) { |
|
| 227 | + foreach ($node->inner_html as $nestedNode) { |
|
| 228 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // EXCLUDE self closed <pc/> |
| 233 | - if ( $node->tagName === 'pc' && $node->self_closed === false ) { |
|
| 234 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
| 235 | - $dataRefEnd = $node->attributes[ 'dataRefEnd' ]; |
|
| 236 | - } elseif ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 237 | - $dataRefEnd = $node->attributes[ 'dataRefStart' ]; |
|
| 233 | + if ($node->tagName === 'pc' && $node->self_closed === false) { |
|
| 234 | + if (isset($node->attributes['dataRefEnd'])) { |
|
| 235 | + $dataRefEnd = $node->attributes['dataRefEnd']; |
|
| 236 | + } elseif (isset($node->attributes['dataRefStart'])) { |
|
| 237 | + $dataRefEnd = $node->attributes['dataRefStart']; |
|
| 238 | 238 | } else { |
| 239 | 239 | $dataRefEnd = null; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | $dataRefEndMap[] = [ |
| 243 | - 'id' => isset( $node->attributes[ 'id' ] ) ? $node->attributes[ 'id' ] : null, |
|
| 243 | + 'id' => isset($node->attributes['id']) ? $node->attributes['id'] : null, |
|
| 244 | 244 | 'dataRefEnd' => $dataRefEnd, |
| 245 | 245 | ]; |
| 246 | 246 | |
@@ -258,40 +258,40 @@ discard block |
||
| 258 | 258 | * @throws InvalidXmlException |
| 259 | 259 | * @throws XmlParsingException |
| 260 | 260 | */ |
| 261 | - private function replaceOpeningPcTags( $string ) { |
|
| 261 | + private function replaceOpeningPcTags($string) { |
|
| 262 | 262 | |
| 263 | - preg_match_all( '|<pc ([^>/]+?)>|iu', $string, $openingPcMatches ); |
|
| 263 | + preg_match_all('|<pc ([^>/]+?)>|iu', $string, $openingPcMatches); |
|
| 264 | 264 | |
| 265 | - foreach ( $openingPcMatches[ 0 ] as $match ) { |
|
| 265 | + foreach ($openingPcMatches[0] as $match) { |
|
| 266 | 266 | |
| 267 | - $node = XmlParser::parse( $match . '</pc>', true )[ 0 ]; // add a closing tag to not break xml integrity |
|
| 267 | + $node = XmlParser::parse($match.'</pc>', true)[0]; // add a closing tag to not break xml integrity |
|
| 268 | 268 | |
| 269 | 269 | // CASE 1 - Missing `dataRefStart` |
| 270 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) && !isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 271 | - $node->attributes[ 'dataRefStart' ] = $node->attributes[ 'dataRefEnd' ]; |
|
| 270 | + if (isset($node->attributes['dataRefEnd']) && !isset($node->attributes['dataRefStart'])) { |
|
| 271 | + $node->attributes['dataRefStart'] = $node->attributes['dataRefEnd']; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | // CASE 2 - Missing `dataRefEnd` |
| 275 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && !isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
| 276 | - $node->attributes[ 'dataRefEnd' ] = $node->attributes[ 'dataRefStart' ]; |
|
| 275 | + if (isset($node->attributes['dataRefStart']) && !isset($node->attributes['dataRefEnd'])) { |
|
| 276 | + $node->attributes['dataRefEnd'] = $node->attributes['dataRefStart']; |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 279 | + if (isset($node->attributes['dataRefStart'])) { |
|
| 280 | 280 | |
| 281 | - $startValue = $this->map[ $node->attributes[ 'dataRefStart' ] ] ?: 'NULL'; //handling null values in original data map |
|
| 281 | + $startValue = $this->map[$node->attributes['dataRefStart']] ?: 'NULL'; //handling null values in original data map |
|
| 282 | 282 | |
| 283 | - $newTag = [ '<ph' ]; |
|
| 283 | + $newTag = ['<ph']; |
|
| 284 | 284 | |
| 285 | - if ( isset( $node->attributes[ 'id' ] ) ) { |
|
| 286 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '_1"'; |
|
| 285 | + if (isset($node->attributes['id'])) { |
|
| 286 | + $newTag[] = 'id="'.$node->attributes['id'].'_1"'; |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - $newTag[] = 'ctype="' . CTypeEnum::PC_OPEN_DATA_REF . '"'; |
|
| 290 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $startValue ) . '"'; |
|
| 291 | - $newTag[] = 'x-orig="' . base64_encode( $match ) . '"'; |
|
| 289 | + $newTag[] = 'ctype="'.CTypeEnum::PC_OPEN_DATA_REF.'"'; |
|
| 290 | + $newTag[] = 'equiv-text="base64:'.base64_encode($startValue).'"'; |
|
| 291 | + $newTag[] = 'x-orig="'.base64_encode($match).'"'; |
|
| 292 | 292 | |
| 293 | 293 | // conversion for opening <pc> tag |
| 294 | - $string = str_replace( $match, implode( " ", $newTag ) . '/>', $string ); |
|
| 294 | + $string = str_replace($match, implode(" ", $newTag).'/>', $string); |
|
| 295 | 295 | |
| 296 | 296 | } |
| 297 | 297 | } |
@@ -308,37 +308,37 @@ discard block |
||
| 308 | 308 | * |
| 309 | 309 | * @return string |
| 310 | 310 | */ |
| 311 | - private function replaceClosingPcTags( $string, $dataRefEndMap = [] ) { |
|
| 311 | + private function replaceClosingPcTags($string, $dataRefEndMap = []) { |
|
| 312 | 312 | |
| 313 | - preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
| 313 | + preg_match_all('|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
| 314 | 314 | $delta = 0; |
| 315 | 315 | |
| 316 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
| 316 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
| 317 | 317 | |
| 318 | - $offset = $match[ 1 ]; |
|
| 318 | + $offset = $match[1]; |
|
| 319 | 319 | $length = 5; // strlen of '</pc>' |
| 320 | 320 | |
| 321 | - $attr = isset( $dataRefEndMap[ $index ] ) ? $dataRefEndMap[ $index ] : null; |
|
| 321 | + $attr = isset($dataRefEndMap[$index]) ? $dataRefEndMap[$index] : null; |
|
| 322 | 322 | |
| 323 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
| 323 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
| 324 | 324 | |
| 325 | - $endValue = !empty( $this->map[ $attr[ 'dataRefEnd' ] ] ) ? $this->map[ $attr[ 'dataRefEnd' ] ] : 'NULL'; |
|
| 325 | + $endValue = !empty($this->map[$attr['dataRefEnd']]) ? $this->map[$attr['dataRefEnd']] : 'NULL'; |
|
| 326 | 326 | |
| 327 | - $newTag = [ '<ph' ]; |
|
| 327 | + $newTag = ['<ph']; |
|
| 328 | 328 | |
| 329 | - if ( isset( $attr[ 'id' ] ) ) { |
|
| 330 | - $newTag[] = 'id="' . $attr[ 'id' ] . '_2"'; |
|
| 329 | + if (isset($attr['id'])) { |
|
| 330 | + $newTag[] = 'id="'.$attr['id'].'_2"'; |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - $newTag[] = 'ctype="' . CTypeEnum::PC_CLOSE_DATA_REF . '"'; |
|
| 334 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $endValue ) . '"'; |
|
| 335 | - $newTag[] = 'x-orig="' . base64_encode( '</pc>' ) . '"'; |
|
| 333 | + $newTag[] = 'ctype="'.CTypeEnum::PC_CLOSE_DATA_REF.'"'; |
|
| 334 | + $newTag[] = 'equiv-text="base64:'.base64_encode($endValue).'"'; |
|
| 335 | + $newTag[] = 'x-orig="'.base64_encode('</pc>').'"'; |
|
| 336 | 336 | |
| 337 | 337 | // conversion for opening <pc> tag |
| 338 | - $completeTag = implode( " ", $newTag ) . '/>'; |
|
| 339 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
| 340 | - $string = substr_replace( $string, $completeTag, $realOffset, $length ); |
|
| 341 | - $delta = $delta + strlen( $completeTag ) - $length; |
|
| 338 | + $completeTag = implode(" ", $newTag).'/>'; |
|
| 339 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
| 340 | + $string = substr_replace($string, $completeTag, $realOffset, $length); |
|
| 341 | + $delta = $delta + strlen($completeTag) - $length; |
|
| 342 | 342 | |
| 343 | 343 | } |
| 344 | 344 | |
@@ -356,17 +356,17 @@ discard block |
||
| 356 | 356 | * @throws InvalidXmlException |
| 357 | 357 | * @throws XmlParsingException |
| 358 | 358 | */ |
| 359 | - public function restore( $string ) { |
|
| 359 | + public function restore($string) { |
|
| 360 | 360 | |
| 361 | 361 | // if map is empty return string as is |
| 362 | - if ( empty( $this->map ) ) { |
|
| 362 | + if (empty($this->map)) { |
|
| 363 | 363 | return $string; |
| 364 | 364 | } |
| 365 | 365 | |
| 366 | - $html = XmlParser::parse( $string, true ); |
|
| 366 | + $html = XmlParser::parse($string, true); |
|
| 367 | 367 | |
| 368 | - foreach ( $html as $node ) { |
|
| 369 | - $string = $this->recursiveRestoreOriginalTags( $node, $string ); |
|
| 368 | + foreach ($html as $node) { |
|
| 369 | + $string = $this->recursiveRestoreOriginalTags($node, $string); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | return $string; |
@@ -378,21 +378,21 @@ discard block |
||
| 378 | 378 | * |
| 379 | 379 | * @return string |
| 380 | 380 | */ |
| 381 | - private function recursiveRestoreOriginalTags( $node, $string ) { |
|
| 381 | + private function recursiveRestoreOriginalTags($node, $string) { |
|
| 382 | 382 | |
| 383 | - if ( $node->has_children ) { |
|
| 383 | + if ($node->has_children) { |
|
| 384 | 384 | |
| 385 | - foreach ( $node->inner_html as $childNode ) { |
|
| 386 | - $string = $this->recursiveRestoreOriginalTags( $childNode, $string ); |
|
| 385 | + foreach ($node->inner_html as $childNode) { |
|
| 386 | + $string = $this->recursiveRestoreOriginalTags($childNode, $string); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | } else { |
| 390 | 390 | |
| 391 | - $cType = isset( $node->attributes[ 'ctype' ] ) ? $node->attributes[ 'ctype' ] : null; |
|
| 391 | + $cType = isset($node->attributes['ctype']) ? $node->attributes['ctype'] : null; |
|
| 392 | 392 | |
| 393 | - if ( $cType ) { |
|
| 393 | + if ($cType) { |
|
| 394 | 394 | |
| 395 | - switch ( $node->attributes[ 'ctype' ] ) { |
|
| 395 | + switch ($node->attributes['ctype']) { |
|
| 396 | 396 | case CTypeEnum::ORIGINAL_PC_OPEN: |
| 397 | 397 | case CTypeEnum::ORIGINAL_PC_CLOSE: |
| 398 | 398 | case CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF: |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | case CTypeEnum::PC_SELF_CLOSE_DATA_REF: |
| 403 | 403 | case CTypeEnum::SC_DATA_REF: |
| 404 | 404 | case CTypeEnum::EC_DATA_REF: |
| 405 | - return preg_replace( '/' . preg_quote( $node->node, '/' ) . '/', base64_decode( $node->attributes[ 'x-orig' ] ), $string, 1 ); |
|
| 405 | + return preg_replace('/'.preg_quote($node->node, '/').'/', base64_decode($node->attributes['x-orig']), $string, 1); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | } |
@@ -25,24 +25,24 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * @inheritDoc |
| 27 | 27 | */ |
| 28 | - public function transform( $segment ) { |
|
| 28 | + public function transform($segment) { |
|
| 29 | 29 | |
| 30 | - if ( empty( $this->dataRefMap ) ) { |
|
| 30 | + if (empty($this->dataRefMap)) { |
|
| 31 | 31 | $this->dataRefMap = $this->pipeline->getDataRefMap(); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | // dataRefMap is present only in xliff 2.0 files |
| 35 | - if ( empty( $this->dataRefMap ) ) { |
|
| 36 | - $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
| 35 | + if (empty($this->dataRefMap)) { |
|
| 36 | + $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
| 37 | 37 | |
| 38 | - return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
| 38 | + return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - $dataRefReplacer = new DataRefReplacer( $this->dataRefMap ); |
|
| 42 | - $segment = $dataRefReplacer->replace( $segment ); |
|
| 43 | - $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
| 41 | + $dataRefReplacer = new DataRefReplacer($this->dataRefMap); |
|
| 42 | + $segment = $dataRefReplacer->replace($segment); |
|
| 43 | + $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
| 44 | 44 | |
| 45 | - return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
| 45 | + return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
@@ -61,23 +61,23 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return string |
| 63 | 63 | */ |
| 64 | - private function replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ) { |
|
| 64 | + private function replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment) { |
|
| 65 | 65 | |
| 66 | - preg_match_all( '/<(ph .*?)>/iu', $segment, $phTags ); |
|
| 66 | + preg_match_all('/<(ph .*?)>/iu', $segment, $phTags); |
|
| 67 | 67 | |
| 68 | - if ( count( $phTags[ 0 ] ) === 0 ) { |
|
| 68 | + if (count($phTags[0]) === 0) { |
|
| 69 | 69 | return $segment; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - foreach ( $phTags[ 0 ] as $phTag ) { |
|
| 72 | + foreach ($phTags[0] as $phTag) { |
|
| 73 | 73 | |
| 74 | 74 | // check if phTag has not any correspondence on dataRef map |
| 75 | - if ( $this->isAValidPhTag( $phTag ) ) { |
|
| 75 | + if ($this->isAValidPhTag($phTag)) { |
|
| 76 | 76 | $segment = preg_replace( |
| 77 | - '/' . preg_quote( $phTag, '/' ) . '/', |
|
| 78 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
| 79 | - '" ctype="' . CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF . |
|
| 80 | - '" equiv-text="base64:' . base64_encode( $phTag ) . |
|
| 77 | + '/'.preg_quote($phTag, '/').'/', |
|
| 78 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
| 79 | + '" ctype="'.CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF. |
|
| 80 | + '" equiv-text="base64:'.base64_encode($phTag). |
|
| 81 | 81 | '"/>', |
| 82 | 82 | $segment, |
| 83 | 83 | 1 // replace ONLY ONE occurrence |
@@ -97,28 +97,28 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return bool |
| 99 | 99 | */ |
| 100 | - private function isAValidPhTag( $phTag ) { |
|
| 100 | + private function isAValidPhTag($phTag) { |
|
| 101 | 101 | |
| 102 | 102 | // try not to throw exception for wrong segments with opening tags and no closing |
| 103 | 103 | try { |
| 104 | - $parsed = XmlParser::parse( $phTag, true ); |
|
| 105 | - } catch ( Exception $e ){ |
|
| 104 | + $parsed = XmlParser::parse($phTag, true); |
|
| 105 | + } catch (Exception $e) { |
|
| 106 | 106 | return false; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | // check for matecat ctype |
| 110 | - $cType = isset( $parsed[ 0 ]->attributes[ 'ctype' ] ) ? $parsed[ 0 ]->attributes[ 'ctype' ] : null; |
|
| 111 | - if ( CTypeEnum::isMatecatCType( $cType ) ) { |
|
| 110 | + $cType = isset($parsed[0]->attributes['ctype']) ? $parsed[0]->attributes['ctype'] : null; |
|
| 111 | + if (CTypeEnum::isMatecatCType($cType)) { |
|
| 112 | 112 | return false; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | // if has equiv-text don't touch |
| 116 | - if ( isset( $parsed[ 0 ]->attributes[ 'equiv-text' ] ) ) { |
|
| 116 | + if (isset($parsed[0]->attributes['equiv-text'])) { |
|
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if ( isset( $parsed[ 0 ]->attributes[ 'dataRef' ] ) ) { |
|
| 121 | - return !array_key_exists( $parsed[ 0 ]->attributes[ 'dataRef' ], $this->dataRefMap ); |
|
| 120 | + if (isset($parsed[0]->attributes['dataRef'])) { |
|
| 121 | + return !array_key_exists($parsed[0]->attributes['dataRef'], $this->dataRefMap); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | return true; |
@@ -141,33 +141,33 @@ discard block |
||
| 141 | 141 | * |
| 142 | 142 | * @return string |
| 143 | 143 | */ |
| 144 | - private function replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ) { |
|
| 144 | + private function replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment) { |
|
| 145 | 145 | |
| 146 | - preg_match_all( '/<(pc .*?)>/iu', $segment, $openingPcTags ); |
|
| 147 | - preg_match_all( '|<(/pc)>|iu', $segment, $closingPcTags ); |
|
| 146 | + preg_match_all('/<(pc .*?)>/iu', $segment, $openingPcTags); |
|
| 147 | + preg_match_all('|<(/pc)>|iu', $segment, $closingPcTags); |
|
| 148 | 148 | |
| 149 | - if ( count( $openingPcTags[ 0 ] ) === 0 ) { |
|
| 149 | + if (count($openingPcTags[0]) === 0) { |
|
| 150 | 150 | return $segment; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - foreach ( $openingPcTags[ 0 ] as $openingPcTag ) { |
|
| 153 | + foreach ($openingPcTags[0] as $openingPcTag) { |
|
| 154 | 154 | $segment = preg_replace( |
| 155 | - '/' . preg_quote( $openingPcTag, '/' ) . '/', |
|
| 156 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
| 157 | - '" ctype="' . CTypeEnum::ORIGINAL_PC_OPEN . |
|
| 158 | - '" equiv-text="base64:' . base64_encode( $openingPcTag ) . |
|
| 155 | + '/'.preg_quote($openingPcTag, '/').'/', |
|
| 156 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
| 157 | + '" ctype="'.CTypeEnum::ORIGINAL_PC_OPEN. |
|
| 158 | + '" equiv-text="base64:'.base64_encode($openingPcTag). |
|
| 159 | 159 | '"/>', |
| 160 | 160 | $segment, |
| 161 | 161 | 1 |
| 162 | 162 | ); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - foreach ( $closingPcTags[ 0 ] as $closingPcTag ) { |
|
| 165 | + foreach ($closingPcTags[0] as $closingPcTag) { |
|
| 166 | 166 | $segment = preg_replace( |
| 167 | - '/' . preg_quote( $closingPcTag, '/' ) . '/', |
|
| 168 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
| 169 | - '" ctype="' . CTypeEnum::ORIGINAL_PC_CLOSE . |
|
| 170 | - '" equiv-text="base64:' . base64_encode( $closingPcTag ) . |
|
| 167 | + '/'.preg_quote($closingPcTag, '/').'/', |
|
| 168 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
| 169 | + '" ctype="'.CTypeEnum::ORIGINAL_PC_CLOSE. |
|
| 170 | + '" equiv-text="base64:'.base64_encode($closingPcTag). |
|
| 171 | 171 | '"/>', |
| 172 | 172 | $segment, |
| 173 | 173 | 1 |