@@ -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,35 +31,35 @@ 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 | - if ( $this->stringContainsPcTags( $string ) ) { |
|
53 | + if ($this->stringContainsPcTags($string)) { |
|
54 | 54 | |
55 | 55 | // replace self-closed <pc /> |
56 | - $string = $this->replaceSelfClosedPcTags( $string ); |
|
56 | + $string = $this->replaceSelfClosedPcTags($string); |
|
57 | 57 | |
58 | 58 | // create a dataRefEnd map |
59 | 59 | // (needed for correct handling of </pc> closing tags) |
60 | - $dataRefEndMap = $this->buildDataRefEndMap( $html ); |
|
61 | - $string = $this->replaceOpeningPcTags( $string ); |
|
62 | - $string = $this->replaceClosingPcTags( $string, $dataRefEndMap ); |
|
60 | + $dataRefEndMap = $this->buildDataRefEndMap($html); |
|
61 | + $string = $this->replaceOpeningPcTags($string); |
|
62 | + $string = $this->replaceClosingPcTags($string, $dataRefEndMap); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | return $string; |
@@ -70,17 +70,17 @@ discard block |
||
70 | 70 | * |
71 | 71 | * @return bool |
72 | 72 | */ |
73 | - private function hasAnyDataRefAttribute( $string ) { |
|
73 | + private function hasAnyDataRefAttribute($string) { |
|
74 | 74 | $dataRefTags = [ |
75 | 75 | 'dataRef', |
76 | 76 | 'dataRefStart', |
77 | 77 | 'dataRefEnd', |
78 | 78 | ]; |
79 | 79 | |
80 | - foreach ( $dataRefTags as $tag ) { |
|
81 | - preg_match( '/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches ); |
|
80 | + foreach ($dataRefTags as $tag) { |
|
81 | + preg_match('/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches); |
|
82 | 82 | |
83 | - if ( count( $matches ) > 0 ) { |
|
83 | + if (count($matches) > 0) { |
|
84 | 84 | return true; |
85 | 85 | } |
86 | 86 | } |
@@ -91,11 +91,11 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return string |
93 | 93 | */ |
94 | - private function cleanFromEquivText( $string ) { |
|
95 | - $html = HtmlParser::parse( $string ); |
|
94 | + private function cleanFromEquivText($string) { |
|
95 | + $html = HtmlParser::parse($string); |
|
96 | 96 | |
97 | - foreach ( $html as $node ) { |
|
98 | - $string = $this->recursiveCleanFromEquivText( $node, $string ); |
|
97 | + foreach ($html as $node) { |
|
98 | + $string = $this->recursiveCleanFromEquivText($node, $string); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | return $string; |
@@ -114,57 +114,57 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @return string |
116 | 116 | */ |
117 | - private function recursiveAddEquivTextToPhTag( $node, $string ) { |
|
118 | - if ( $node->has_children ) { |
|
119 | - foreach ( $node->inner_html as $childNode ) { |
|
120 | - $string = $this->recursiveAddEquivTextToPhTag( $childNode, $string ); |
|
117 | + private function recursiveAddEquivTextToPhTag($node, $string) { |
|
118 | + if ($node->has_children) { |
|
119 | + foreach ($node->inner_html as $childNode) { |
|
120 | + $string = $this->recursiveAddEquivTextToPhTag($childNode, $string); |
|
121 | 121 | } |
122 | 122 | } else { |
123 | - if ( $node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec' ) { |
|
124 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
123 | + if ($node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec') { |
|
124 | + if (!isset($node->attributes['dataRef'])) { |
|
125 | 125 | return $string; |
126 | 126 | } |
127 | 127 | |
128 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
129 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
128 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
129 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
130 | 130 | |
131 | 131 | |
132 | 132 | // if isset a value in the map calculate base64 encoded value |
133 | 133 | // otherwise skip |
134 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
134 | + if (!in_array($b, array_keys($this->map))) { |
|
135 | 135 | return $string; |
136 | 136 | } |
137 | 137 | |
138 | 138 | // check if is null, in this case convert it to NULL string |
139 | - if ( is_null( $this->map[ $b ] ) ) { |
|
140 | - $this->map[ $b ] = 'NULL'; |
|
139 | + if (is_null($this->map[$b])) { |
|
140 | + $this->map[$b] = 'NULL'; |
|
141 | 141 | } |
142 | 142 | |
143 | - $value = $this->map[ $b ]; |
|
144 | - $base64EncodedValue = base64_encode( $value ); |
|
143 | + $value = $this->map[$b]; |
|
144 | + $base64EncodedValue = base64_encode($value); |
|
145 | 145 | |
146 | - if ( empty( $base64EncodedValue ) ) { |
|
146 | + if (empty($base64EncodedValue)) { |
|
147 | 147 | return $string; |
148 | 148 | } |
149 | 149 | |
150 | 150 | // if there is no id copy it from dataRef |
151 | - $id = ( !isset( $node->attributes[ 'id' ] ) ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
151 | + $id = (!isset($node->attributes['id'])) ? ' id="' . $b . '" removeId="true"' : ''; |
|
152 | 152 | |
153 | 153 | // introduce dataType for <ec>/<sc> tag handling |
154 | - $dataType = ( $this->isAEcOrScTag( $node ) ) ? ' dataType="' . $node->tagname . '"' : ''; |
|
154 | + $dataType = ($this->isAEcOrScTag($node)) ? ' dataType="' . $node->tagname . '"' : ''; |
|
155 | 155 | |
156 | 156 | // replacement |
157 | - $d = str_replace( '/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a ); |
|
158 | - $a = str_replace( [ '<', '>', '>', '<' ], '', $a ); |
|
159 | - $d = str_replace( [ '<', '>', '>', '<' ], '', $d ); |
|
157 | + $d = str_replace('/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a); |
|
158 | + $a = str_replace(['<', '>', '>', '<'], '', $a); |
|
159 | + $d = str_replace(['<', '>', '>', '<'], '', $d); |
|
160 | 160 | |
161 | 161 | // convert <ec>/<sc> into <ph> |
162 | - if ( $this->isAEcOrScTag( $node ) ) { |
|
163 | - $d = 'ph' . substr( $d, 2 ); |
|
164 | - $d = trim( $d ); |
|
162 | + if ($this->isAEcOrScTag($node)) { |
|
163 | + $d = 'ph' . substr($d, 2); |
|
164 | + $d = trim($d); |
|
165 | 165 | } |
166 | 166 | |
167 | - return str_replace( $a, $d, $string ); |
|
167 | + return str_replace($a, $d, $string); |
|
168 | 168 | } |
169 | 169 | } |
170 | 170 | |
@@ -176,10 +176,10 @@ discard block |
||
176 | 176 | * |
177 | 177 | * @return bool |
178 | 178 | */ |
179 | - private function stringContainsPcTags( $string ) { |
|
180 | - preg_match_all( '/<pc (.*?)>/iu', $string, $openingPcMatches ); |
|
179 | + private function stringContainsPcTags($string) { |
|
180 | + preg_match_all('/<pc (.*?)>/iu', $string, $openingPcMatches); |
|
181 | 181 | |
182 | - return ( isset( $openingPcMatches[ 0 ] ) && count( $openingPcMatches[ 0 ] ) > 0 ); |
|
182 | + return (isset($openingPcMatches[0]) && count($openingPcMatches[0]) > 0); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | /** |
@@ -187,20 +187,20 @@ discard block |
||
187 | 187 | * |
188 | 188 | * @return mixed |
189 | 189 | */ |
190 | - private function replaceSelfClosedPcTags( $string ) { |
|
190 | + private function replaceSelfClosedPcTags($string) { |
|
191 | 191 | |
192 | 192 | $regex = '/<pc[^>]+?\/>/iu'; |
193 | - preg_match_all( $regex, $string, $selfClosedPcMatches ); |
|
193 | + preg_match_all($regex, $string, $selfClosedPcMatches); |
|
194 | 194 | |
195 | - foreach ( $selfClosedPcMatches[ 0 ] as $match ) { |
|
195 | + foreach ($selfClosedPcMatches[0] as $match) { |
|
196 | 196 | |
197 | - $html = HtmlParser::parse( $match ); |
|
198 | - $node = $html[ 0 ]; |
|
197 | + $html = HtmlParser::parse($match); |
|
198 | + $node = $html[0]; |
|
199 | 199 | $attributes = $node->attributes; |
200 | 200 | |
201 | - if ( isset( $attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
|
202 | - $replacement = '<ph id="' . $attributes[ 'id' ] . '" dataType="pcSelf" originalData="' . base64_encode( $match ) . '" dataRef="' . $attributes[ 'dataRefStart' ] . '" equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"/>'; |
|
203 | - $string = str_replace( $match, $replacement, $string ); |
|
201 | + if (isset($attributes['dataRefStart']) && array_key_exists($node->attributes['dataRefStart'], $this->map)) { |
|
202 | + $replacement = '<ph id="' . $attributes['id'] . '" dataType="pcSelf" originalData="' . base64_encode($match) . '" dataRef="' . $attributes['dataRefStart'] . '" equiv-text="base64:' . base64_encode($this->map[$node->attributes['dataRefStart']]) . '"/>'; |
|
203 | + $string = str_replace($match, $replacement, $string); |
|
204 | 204 | } |
205 | 205 | } |
206 | 206 | |
@@ -215,12 +215,12 @@ discard block |
||
215 | 215 | * |
216 | 216 | * @return array |
217 | 217 | */ |
218 | - private function buildDataRefEndMap( $html ) { |
|
218 | + private function buildDataRefEndMap($html) { |
|
219 | 219 | $dataRefEndMap = []; |
220 | 220 | |
221 | - foreach ( $html as $index => $node ) { |
|
222 | - if ( $node->tagname === 'pc' ) { |
|
223 | - $this->extractDataRefMapRecursively( $node, $dataRefEndMap ); |
|
221 | + foreach ($html as $index => $node) { |
|
222 | + if ($node->tagname === 'pc') { |
|
223 | + $this->extractDataRefMapRecursively($node, $dataRefEndMap); |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | |
@@ -233,25 +233,25 @@ discard block |
||
233 | 233 | * @param object $node |
234 | 234 | * @param $dataRefEndMap |
235 | 235 | */ |
236 | - private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
|
237 | - if ( $this->nodeContainsNestedPcTags( $node ) ) { |
|
238 | - foreach ( $node->inner_html as $nestedNode ) { |
|
239 | - $this->extractDataRefMapRecursively( $nestedNode, $dataRefEndMap ); |
|
236 | + private function extractDataRefMapRecursively($node, &$dataRefEndMap) { |
|
237 | + if ($this->nodeContainsNestedPcTags($node)) { |
|
238 | + foreach ($node->inner_html as $nestedNode) { |
|
239 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
240 | 240 | } |
241 | 241 | } |
242 | 242 | |
243 | 243 | // EXCLUDE self closed <pc/> |
244 | - if ( $node->tagname === 'pc' && $node->self_closed === false ) { |
|
245 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
246 | - $dataRefEnd = $node->attributes[ 'dataRefEnd' ]; |
|
247 | - } elseif ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
248 | - $dataRefEnd = $node->attributes[ 'dataRefStart' ]; |
|
244 | + if ($node->tagname === 'pc' && $node->self_closed === false) { |
|
245 | + if (isset($node->attributes['dataRefEnd'])) { |
|
246 | + $dataRefEnd = $node->attributes['dataRefEnd']; |
|
247 | + } elseif (isset($node->attributes['dataRefStart'])) { |
|
248 | + $dataRefEnd = $node->attributes['dataRefStart']; |
|
249 | 249 | } else { |
250 | 250 | $dataRefEnd = null; |
251 | 251 | } |
252 | 252 | |
253 | 253 | $dataRefEndMap[] = [ |
254 | - 'id' => isset( $node->attributes[ 'id' ] ) ? $node->attributes[ 'id' ] : null, |
|
254 | + 'id' => isset($node->attributes['id']) ? $node->attributes['id'] : null, |
|
255 | 255 | 'dataRefEnd' => $dataRefEnd, |
256 | 256 | ]; |
257 | 257 | } |
@@ -263,15 +263,15 @@ discard block |
||
263 | 263 | * |
264 | 264 | * @return string|string[] |
265 | 265 | */ |
266 | - private function recursiveCleanFromEquivText( $node, $string ) { |
|
267 | - if ( $node->has_children ) { |
|
268 | - foreach ( $node->inner_html as $childNode ) { |
|
269 | - $string = $this->recursiveCleanFromEquivText( $childNode, $string ); |
|
266 | + private function recursiveCleanFromEquivText($node, $string) { |
|
267 | + if ($node->has_children) { |
|
268 | + foreach ($node->inner_html as $childNode) { |
|
269 | + $string = $this->recursiveCleanFromEquivText($childNode, $string); |
|
270 | 270 | } |
271 | 271 | } else { |
272 | - if ( isset( $node->attributes[ 'dataRef' ] ) && array_key_exists( $node->attributes[ 'dataRef' ], $this->map ) ) { |
|
273 | - $cleaned = preg_replace( '/ equiv-text="(.*?)"/', '', $node->node ); |
|
274 | - $string = str_replace( $node->node, $cleaned, $string ); |
|
272 | + if (isset($node->attributes['dataRef']) && array_key_exists($node->attributes['dataRef'], $this->map)) { |
|
273 | + $cleaned = preg_replace('/ equiv-text="(.*?)"/', '', $node->node); |
|
274 | + $string = str_replace($node->node, $cleaned, $string); |
|
275 | 275 | } |
276 | 276 | } |
277 | 277 | |
@@ -285,34 +285,34 @@ discard block |
||
285 | 285 | * |
286 | 286 | * @return string |
287 | 287 | */ |
288 | - private function replaceOpeningPcTags( $string ) { |
|
289 | - preg_match_all( '/<pc (.*?)>/iu', $string, $openingPcMatches ); |
|
288 | + private function replaceOpeningPcTags($string) { |
|
289 | + preg_match_all('/<pc (.*?)>/iu', $string, $openingPcMatches); |
|
290 | 290 | |
291 | - foreach ( $openingPcMatches[ 0 ] as $index => $match ) { |
|
292 | - $attr = HtmlParser::getAttributes( $openingPcMatches[ 1 ][ $index ] ); |
|
291 | + foreach ($openingPcMatches[0] as $index => $match) { |
|
292 | + $attr = HtmlParser::getAttributes($openingPcMatches[1][$index]); |
|
293 | 293 | |
294 | 294 | // CASE 1 - Missing `dataRefStart` |
295 | - if ( isset( $attr[ 'dataRefEnd' ] ) && !isset( $attr[ 'dataRefStart' ] ) ) { |
|
296 | - $attr[ 'dataRefStart' ] = $attr[ 'dataRefEnd' ]; |
|
295 | + if (isset($attr['dataRefEnd']) && !isset($attr['dataRefStart'])) { |
|
296 | + $attr['dataRefStart'] = $attr['dataRefEnd']; |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | // CASE 2 - Missing `dataRefEnd` |
300 | - if ( isset( $attr[ 'dataRefStart' ] ) && !isset( $attr[ 'dataRefEnd' ] ) ) { |
|
301 | - $attr[ 'dataRefEnd' ] = $attr[ 'dataRefStart' ]; |
|
300 | + if (isset($attr['dataRefStart']) && !isset($attr['dataRefEnd'])) { |
|
301 | + $attr['dataRefEnd'] = $attr['dataRefStart']; |
|
302 | 302 | } |
303 | 303 | |
304 | - if ( isset( $attr[ 'dataRefStart' ] ) ) { |
|
304 | + if (isset($attr['dataRefStart'])) { |
|
305 | 305 | $startOriginalData = $match; // opening <pc> |
306 | - $startValue = $this->map[ $attr[ 'dataRefStart' ] ] ?: 'NULL'; //handling null values in original data map |
|
307 | - $base64EncodedStartValue = base64_encode( $startValue ); |
|
308 | - $base64StartOriginalData = base64_encode( $startOriginalData ); |
|
306 | + $startValue = $this->map[$attr['dataRefStart']] ?: 'NULL'; //handling null values in original data map |
|
307 | + $base64EncodedStartValue = base64_encode($startValue); |
|
308 | + $base64StartOriginalData = base64_encode($startOriginalData); |
|
309 | 309 | |
310 | 310 | // conversion for opening <pc> tag |
311 | - $openingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_1"' : '' ) . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
312 | - . $attr[ 'dataRefStart' ] . '" equiv-text="base64:' |
|
311 | + $openingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_1"' : '') . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
312 | + . $attr['dataRefStart'] . '" equiv-text="base64:' |
|
313 | 313 | . $base64EncodedStartValue . '"/>'; |
314 | 314 | |
315 | - $string = str_replace( $startOriginalData, $openingPcConverted, $string ); |
|
315 | + $string = str_replace($startOriginalData, $openingPcConverted, $string); |
|
316 | 316 | } |
317 | 317 | } |
318 | 318 | |
@@ -328,33 +328,33 @@ discard block |
||
328 | 328 | * |
329 | 329 | * @return string |
330 | 330 | */ |
331 | - private function replaceClosingPcTags( $string, $dataRefEndMap = [] ) { |
|
332 | - preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
331 | + private function replaceClosingPcTags($string, $dataRefEndMap = []) { |
|
332 | + preg_match_all('|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
333 | 333 | $delta = 0; |
334 | 334 | |
335 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
336 | - $offset = $match[ 1 ]; |
|
337 | - $length = strlen( $match[ 0 ] ); |
|
338 | - $attr = $dataRefEndMap[ $index ]; |
|
335 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
336 | + $offset = $match[1]; |
|
337 | + $length = strlen($match[0]); |
|
338 | + $attr = $dataRefEndMap[$index]; |
|
339 | 339 | |
340 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
341 | - $endOriginalData = $match[ 0 ]; // </pc> |
|
342 | - $endValue = $this->map[ $attr[ 'dataRefEnd' ] ] ?: 'NULL'; |
|
343 | - $base64EncodedEndValue = base64_encode( $endValue ); |
|
344 | - $base64EndOriginalData = base64_encode( $endOriginalData ); |
|
340 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
341 | + $endOriginalData = $match[0]; // </pc> |
|
342 | + $endValue = $this->map[$attr['dataRefEnd']] ?: 'NULL'; |
|
343 | + $base64EncodedEndValue = base64_encode($endValue); |
|
344 | + $base64EndOriginalData = base64_encode($endOriginalData); |
|
345 | 345 | |
346 | 346 | // conversion for closing <pc> tag |
347 | - $closingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_2"' : '' ) . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
348 | - . $attr[ 'dataRefEnd' ] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
347 | + $closingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_2"' : '') . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
348 | + . $attr['dataRefEnd'] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
349 | 349 | |
350 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
350 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
351 | 351 | |
352 | - $string = substr_replace( $string, $closingPcConverted, $realOffset, $length ); |
|
353 | - $delta = $delta + strlen( $closingPcConverted ) - $length; |
|
352 | + $string = substr_replace($string, $closingPcConverted, $realOffset, $length); |
|
353 | + $delta = $delta + strlen($closingPcConverted) - $length; |
|
354 | 354 | } |
355 | 355 | } |
356 | 356 | |
357 | - return !is_array( $string ) ? $string : implode( $string ); |
|
357 | + return !is_array($string) ? $string : implode($string); |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | /** |
@@ -362,13 +362,13 @@ discard block |
||
362 | 362 | * |
363 | 363 | * @return bool |
364 | 364 | */ |
365 | - private function nodeContainsNestedPcTags( $node ) { |
|
366 | - if ( !$node->has_children ) { |
|
365 | + private function nodeContainsNestedPcTags($node) { |
|
366 | + if (!$node->has_children) { |
|
367 | 367 | return false; |
368 | 368 | } |
369 | 369 | |
370 | - foreach ( $node->inner_html as $nestedNode ) { |
|
371 | - if ( $nestedNode->tagname === 'pc' && ( isset( $node->attributes[ 'dataRefEnd' ] ) || isset( $node->attributes[ 'dataRefStart' ] ) ) ) { |
|
370 | + foreach ($node->inner_html as $nestedNode) { |
|
371 | + if ($nestedNode->tagname === 'pc' && (isset($node->attributes['dataRefEnd']) || isset($node->attributes['dataRefStart']))) { |
|
372 | 372 | return true; |
373 | 373 | } |
374 | 374 | } |
@@ -381,18 +381,18 @@ discard block |
||
381 | 381 | * |
382 | 382 | * @return string |
383 | 383 | */ |
384 | - public function restore( $string ) { |
|
384 | + public function restore($string) { |
|
385 | 385 | // if map is empty return string as is |
386 | - if ( empty( $this->map ) ) { |
|
386 | + if (empty($this->map)) { |
|
387 | 387 | return $string; |
388 | 388 | } |
389 | 389 | |
390 | 390 | // replace eventual empty equiv-text="" |
391 | - $string = str_replace( ' equiv-text=""', '', $string ); |
|
392 | - $html = HtmlParser::parse( $string ); |
|
391 | + $string = str_replace(' equiv-text=""', '', $string); |
|
392 | + $html = HtmlParser::parse($string); |
|
393 | 393 | |
394 | - foreach ( $html as $node ) { |
|
395 | - $string = $this->recursiveRemoveOriginalData( $node, $string ); |
|
394 | + foreach ($html as $node) { |
|
395 | + $string = $this->recursiveRemoveOriginalData($node, $string); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | return $string; |
@@ -404,76 +404,76 @@ discard block |
||
404 | 404 | * |
405 | 405 | * @return string|string[] |
406 | 406 | */ |
407 | - private function recursiveRemoveOriginalData( $node, $string ) { |
|
408 | - if ( $node->has_children ) { |
|
409 | - foreach ( $node->inner_html as $childNode ) { |
|
410 | - $string = $this->recursiveRemoveOriginalData( $childNode, $string ); |
|
407 | + private function recursiveRemoveOriginalData($node, $string) { |
|
408 | + if ($node->has_children) { |
|
409 | + foreach ($node->inner_html as $childNode) { |
|
410 | + $string = $this->recursiveRemoveOriginalData($childNode, $string); |
|
411 | 411 | } |
412 | 412 | } else { |
413 | 413 | |
414 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
414 | + if (!isset($node->attributes['dataRef'])) { |
|
415 | 415 | return $string; |
416 | 416 | } |
417 | 417 | |
418 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
419 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
420 | - $c = $node->terminator; // terminator: Eg: > |
|
418 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
419 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
420 | + $c = $node->terminator; // terminator: Eg: > |
|
421 | 421 | |
422 | 422 | // if isset a value in the map calculate base64 encoded value |
423 | 423 | // or it is an empty string |
424 | 424 | // otherwise skip |
425 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
425 | + if (!in_array($b, array_keys($this->map))) { |
|
426 | 426 | return $string; |
427 | 427 | } |
428 | 428 | |
429 | 429 | // check if is null, in this case convert it to NULL string |
430 | - if ( is_null( $this->map[ $b ] ) ) { |
|
431 | - $this->map[ $b ] = 'NULL'; |
|
430 | + if (is_null($this->map[$b])) { |
|
431 | + $this->map[$b] = 'NULL'; |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | // remove id? |
435 | - $removeId = ( isset( $node->attributes[ 'removeId' ] ) && $node->attributes[ 'removeId' ] === "true" ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
435 | + $removeId = (isset($node->attributes['removeId']) && $node->attributes['removeId'] === "true") ? ' id="' . $b . '" removeId="true"' : ''; |
|
436 | 436 | |
437 | 437 | // grab dataType attribute for <ec>/<sc> tag handling |
438 | - $dataType = ( $this->wasAEcOrScTag( $node ) ) ? ' dataType="' . $node->attributes[ 'dataType' ] . '"' : ''; |
|
438 | + $dataType = ($this->wasAEcOrScTag($node)) ? ' dataType="' . $node->attributes['dataType'] . '"' : ''; |
|
439 | 439 | |
440 | - $d = str_replace( $removeId . $dataType . ' equiv-text="base64:' . base64_encode( $this->map[ $b ] ) . '"/' . $c, '/' . $c, $a ); |
|
440 | + $d = str_replace($removeId . $dataType . ' equiv-text="base64:' . base64_encode($this->map[$b]) . '"/' . $c, '/' . $c, $a); |
|
441 | 441 | |
442 | 442 | // replace original <ec>/<sc> tag |
443 | - if ( $this->wasAEcOrScTag( $node ) ) { |
|
444 | - $d = $node->attributes[ 'dataType' ] . substr( $d, 3 ); |
|
445 | - $d = trim( $d ); |
|
443 | + if ($this->wasAEcOrScTag($node)) { |
|
444 | + $d = $node->attributes['dataType'] . substr($d, 3); |
|
445 | + $d = trim($d); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | // replace only content tag, no matter if the string is encoded or not |
449 | 449 | // in this way we can handle string with mixed tags (encoded and not-encoded) |
450 | 450 | // in the same string |
451 | - $a = $this->purgeTags( $a ); |
|
452 | - $d = $this->purgeTags( $d ); |
|
451 | + $a = $this->purgeTags($a); |
|
452 | + $d = $this->purgeTags($d); |
|
453 | 453 | |
454 | - $string = str_replace( $a, $d, $string ); |
|
454 | + $string = str_replace($a, $d, $string); |
|
455 | 455 | |
456 | 456 | // restoring <pc/> self-closed here |
457 | - if ( Strings::contains( 'dataType="pcSelf"', $d ) ) { |
|
458 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
457 | + if (Strings::contains('dataType="pcSelf"', $d)) { |
|
458 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
459 | 459 | |
460 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
461 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
462 | - $originalData = $this->purgeTags( $originalData ); |
|
463 | - $string = str_replace( $d, $originalData, $string ); |
|
460 | + if (isset($originalDataMatches[1])) { |
|
461 | + $originalData = base64_decode($originalDataMatches[1]); |
|
462 | + $originalData = $this->purgeTags($originalData); |
|
463 | + $string = str_replace($d, $originalData, $string); |
|
464 | 464 | } |
465 | 465 | } |
466 | 466 | |
467 | 467 | // restoring <pc> tags here |
468 | 468 | // if <ph> tag has originalData and originalType is pcStart or pcEnd, |
469 | 469 | // replace with original data |
470 | - if ( Strings::contains( 'dataType="pcStart"', $d ) || Strings::contains( 'dataType="pcEnd"', $d ) ) { |
|
471 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
470 | + if (Strings::contains('dataType="pcStart"', $d) || Strings::contains('dataType="pcEnd"', $d)) { |
|
471 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
472 | 472 | |
473 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
474 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
475 | - $originalData = $this->purgeTags( $originalData ); |
|
476 | - $string = str_replace( $d, $originalData, $string ); |
|
473 | + if (isset($originalDataMatches[1])) { |
|
474 | + $originalData = base64_decode($originalDataMatches[1]); |
|
475 | + $originalData = $this->purgeTags($originalData); |
|
476 | + $string = str_replace($d, $originalData, $string); |
|
477 | 477 | } |
478 | 478 | } |
479 | 479 | } |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | * |
487 | 487 | * @return string |
488 | 488 | */ |
489 | - private function purgeTags( $string ) { |
|
490 | - return str_replace( [ '<', '>', '<', '>' ], '', $string ); |
|
489 | + private function purgeTags($string) { |
|
490 | + return str_replace(['<', '>', '<', '>'], '', $string); |
|
491 | 491 | } |
492 | 492 | |
493 | 493 | /** |
@@ -497,8 +497,8 @@ discard block |
||
497 | 497 | * |
498 | 498 | * @return bool |
499 | 499 | */ |
500 | - private function isAEcOrScTag( $node ) { |
|
501 | - return ( $node->tagname === 'ec' || $node->tagname === 'sc' ); |
|
500 | + private function isAEcOrScTag($node) { |
|
501 | + return ($node->tagname === 'ec' || $node->tagname === 'sc'); |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | /** |
@@ -509,7 +509,7 @@ discard block |
||
509 | 509 | * |
510 | 510 | * @return bool |
511 | 511 | */ |
512 | - private function wasAEcOrScTag( $node ) { |
|
513 | - return ( isset( $node->attributes[ 'dataType' ] ) && ( $node->attributes[ 'dataType' ] === 'ec' || $node->attributes[ 'dataType' ] === 'sc' ) ); |
|
512 | + private function wasAEcOrScTag($node) { |
|
513 | + return (isset($node->attributes['dataType']) && ($node->attributes['dataType'] === 'ec' || $node->attributes['dataType'] === 'sc')); |
|
514 | 514 | } |
515 | 515 | } |