@@ -14,10 +14,10 @@ discard block |
||
14 | 14 | |
15 | 15 | class StandardPHToMateCatCustomPH extends AbstractHandler { |
16 | 16 | |
17 | - public function transform( $segment ) { |
|
17 | + public function transform($segment) { |
|
18 | 18 | |
19 | - $segment = $this->filterPhTagContent( $segment ); |
|
20 | - $segment = $this->filterOriginalSelfClosePhTagsWithEquivText( $segment ); |
|
19 | + $segment = $this->filterPhTagContent($segment); |
|
20 | + $segment = $this->filterOriginalSelfClosePhTagsWithEquivText($segment); |
|
21 | 21 | |
22 | 22 | return $segment; |
23 | 23 | |
@@ -28,15 +28,15 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @return string |
30 | 30 | */ |
31 | - private function filterPhTagContent( $segment ) { |
|
31 | + private function filterPhTagContent($segment) { |
|
32 | 32 | |
33 | - if ( preg_match( '|</ph>|s', $segment ) ) { |
|
34 | - preg_match_all( '|<ph id=["\']([^\'"]+?)["\'].*?>(.*?)</ph>|', $segment, $phTags, PREG_SET_ORDER ); |
|
35 | - foreach ( $phTags as $group ) { |
|
33 | + if (preg_match('|</ph>|s', $segment)) { |
|
34 | + preg_match_all('|<ph id=["\']([^\'"]+?)["\'].*?>(.*?)</ph>|', $segment, $phTags, PREG_SET_ORDER); |
|
35 | + foreach ($phTags as $group) { |
|
36 | 36 | $segment = preg_replace( |
37 | - '/' . preg_quote( $group[ 0 ], '/' ) . '/', |
|
38 | - '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ORIGINAL_PH_CONTENT . '" x-orig="' . base64_encode( $group[ 0 ] ) . '" equiv-text="base64:' . |
|
39 | - base64_encode( htmlentities( $group[ 2 ], ENT_NOQUOTES | 16 /* ENT_XML1 */, 'UTF-8' ) ) . |
|
37 | + '/'.preg_quote($group[0], '/').'/', |
|
38 | + '<ph id="'.$this->getPipeline()->getNextId().'" ctype="'.CTypeEnum::ORIGINAL_PH_CONTENT.'" x-orig="'.base64_encode($group[0]).'" equiv-text="base64:'. |
|
39 | + base64_encode(htmlentities($group[2], ENT_NOQUOTES | 16 /* ENT_XML1 */, 'UTF-8')). |
|
40 | 40 | '"/>', |
41 | 41 | $segment, |
42 | 42 | 1 |
@@ -55,16 +55,16 @@ discard block |
||
55 | 55 | * |
56 | 56 | * @return string |
57 | 57 | */ |
58 | - private function filterOriginalSelfClosePhTagsWithEquivText( $segment ) { |
|
58 | + private function filterOriginalSelfClosePhTagsWithEquivText($segment) { |
|
59 | 59 | |
60 | - preg_match_all( '|<ph[^>]+?equiv-text\s*?=\s*?(["\'])(?!base64:)(.*?)?\1[^>]*?/>|', $segment, $html, PREG_SET_ORDER ); |
|
61 | - foreach ( $html as $tag_attribute ) { |
|
60 | + preg_match_all('|<ph[^>]+?equiv-text\s*?=\s*?(["\'])(?!base64:)(.*?)?\1[^>]*?/>|', $segment, $html, PREG_SET_ORDER); |
|
61 | + foreach ($html as $tag_attribute) { |
|
62 | 62 | |
63 | 63 | //replace subsequent elements excluding already encoded |
64 | 64 | $segment = preg_replace( |
65 | - '/' . preg_quote( $tag_attribute[ 0 ], '/' ) . '/', |
|
66 | - '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ORIGINAL_SELF_CLOSE_PH_WITH_EQUIV_TEXT . '" x-orig="' . base64_encode( $tag_attribute[ 0 ] ) . '" equiv-text="base64:' . |
|
67 | - base64_encode( $tag_attribute[ 2 ] != '' ? $tag_attribute[ 2 ] : 'NULL' ) . |
|
65 | + '/'.preg_quote($tag_attribute[0], '/').'/', |
|
66 | + '<ph id="'.$this->getPipeline()->getNextId().'" ctype="'.CTypeEnum::ORIGINAL_SELF_CLOSE_PH_WITH_EQUIV_TEXT.'" x-orig="'.base64_encode($tag_attribute[0]).'" equiv-text="base64:'. |
|
67 | + base64_encode($tag_attribute[2] != '' ? $tag_attribute[2] : 'NULL'). |
|
68 | 68 | '"/>', |
69 | 69 | $segment, |
70 | 70 | 1 |
@@ -23,23 +23,23 @@ discard block |
||
23 | 23 | /** |
24 | 24 | * @inheritDoc |
25 | 25 | */ |
26 | - public function transform( $segment ) { |
|
26 | + public function transform($segment) { |
|
27 | 27 | |
28 | - if ( empty( $this->dataRefMap ) ) { |
|
28 | + if (empty($this->dataRefMap)) { |
|
29 | 29 | $this->dataRefMap = $this->pipeline->getDataRefMap(); |
30 | 30 | } |
31 | 31 | |
32 | - if ( empty( $this->dataRefMap ) ) { |
|
33 | - $segment = $this->restoreXliffPhTagsFromMatecatPhTags( $segment ); |
|
32 | + if (empty($this->dataRefMap)) { |
|
33 | + $segment = $this->restoreXliffPhTagsFromMatecatPhTags($segment); |
|
34 | 34 | |
35 | - return $this->restoreXliffPcTagsFromMatecatPhTags( $segment ); |
|
35 | + return $this->restoreXliffPcTagsFromMatecatPhTags($segment); |
|
36 | 36 | } |
37 | 37 | |
38 | - $dataRefReplacer = new DataRefReplacer( $this->dataRefMap ); |
|
39 | - $segment = $dataRefReplacer->restore( $segment ); |
|
40 | - $segment = $this->restoreXliffPhTagsFromMatecatPhTags( $segment ); |
|
38 | + $dataRefReplacer = new DataRefReplacer($this->dataRefMap); |
|
39 | + $segment = $dataRefReplacer->restore($segment); |
|
40 | + $segment = $this->restoreXliffPhTagsFromMatecatPhTags($segment); |
|
41 | 41 | |
42 | - return $this->restoreXliffPcTagsFromMatecatPhTags( $segment ); |
|
42 | + return $this->restoreXliffPcTagsFromMatecatPhTags($segment); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -58,15 +58,15 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return string |
60 | 60 | */ |
61 | - private function restoreXliffPhTagsFromMatecatPhTags( $segment ) { |
|
62 | - preg_match_all( '|<ph id="mtc_[0-9]+" ctype="' . CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $matches ); |
|
61 | + private function restoreXliffPhTagsFromMatecatPhTags($segment) { |
|
62 | + preg_match_all('|<ph id="mtc_[0-9]+" ctype="'.CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF.'" equiv-text="base64:(.*?)"/>|iu', $segment, $matches); |
|
63 | 63 | |
64 | - if ( empty( $matches[ 0 ] ) ) { |
|
64 | + if (empty($matches[0])) { |
|
65 | 65 | return $segment; |
66 | 66 | } |
67 | 67 | |
68 | - foreach ( $matches[ 0 ] as $index => $match ) { |
|
69 | - $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment ); |
|
68 | + foreach ($matches[0] as $index => $match) { |
|
69 | + $segment = str_replace($match, base64_decode($matches[1][$index]), $segment); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | return $segment; |
@@ -88,16 +88,16 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @return string |
90 | 90 | */ |
91 | - private function restoreXliffPcTagsFromMatecatPhTags( $segment ) { |
|
92 | - preg_match_all( '|<ph.+? ctype="' . CTypeEnum::ORIGINAL_PC_OPEN . '" equiv-text="base64:(.*?)"/>|iu', $segment, $matches ); |
|
93 | - preg_match_all( '|<ph.+? ctype="' . CTypeEnum::ORIGINAL_PC_CLOSE . '" equiv-text="base64:(.*?)"/>|iu', $segment, $matches ); |
|
91 | + private function restoreXliffPcTagsFromMatecatPhTags($segment) { |
|
92 | + preg_match_all('|<ph.+? ctype="'.CTypeEnum::ORIGINAL_PC_OPEN.'" equiv-text="base64:(.*?)"/>|iu', $segment, $matches); |
|
93 | + preg_match_all('|<ph.+? ctype="'.CTypeEnum::ORIGINAL_PC_CLOSE.'" equiv-text="base64:(.*?)"/>|iu', $segment, $matches); |
|
94 | 94 | |
95 | - if ( empty( $matches[ 0 ] ) ) { |
|
95 | + if (empty($matches[0])) { |
|
96 | 96 | return $segment; |
97 | 97 | } |
98 | 98 | |
99 | - foreach ( $matches[ 0 ] as $index => $match ) { |
|
100 | - $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment ); |
|
99 | + foreach ($matches[0] as $index => $match) { |
|
100 | + $segment = str_replace($match, base64_decode($matches[1][$index]), $segment); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | return $segment; |
@@ -28,24 +28,24 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * @inheritDoc |
30 | 30 | */ |
31 | - public function transform( $segment ) { |
|
31 | + public function transform($segment) { |
|
32 | 32 | |
33 | - if ( empty( $this->dataRefMap ) ) { |
|
33 | + if (empty($this->dataRefMap)) { |
|
34 | 34 | $this->dataRefMap = $this->pipeline->getDataRefMap(); |
35 | 35 | } |
36 | 36 | |
37 | 37 | // dataRefMap is present only in xliff 2.0 files |
38 | - if ( empty( $this->dataRefMap ) ) { |
|
39 | - $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
38 | + if (empty($this->dataRefMap)) { |
|
39 | + $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
40 | 40 | |
41 | - return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
41 | + return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
42 | 42 | } |
43 | 43 | |
44 | - $dataRefReplacer = new DataRefReplacer( $this->dataRefMap ); |
|
45 | - $segment = $dataRefReplacer->replace( $segment ); |
|
46 | - $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
44 | + $dataRefReplacer = new DataRefReplacer($this->dataRefMap); |
|
45 | + $segment = $dataRefReplacer->replace($segment); |
|
46 | + $segment = $this->replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
47 | 47 | |
48 | - return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ); |
|
48 | + return $this->replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
@@ -64,23 +64,23 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @return string|string[] |
66 | 66 | */ |
67 | - private function replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ) { |
|
67 | + private function replace_Ph_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment) { |
|
68 | 68 | |
69 | - preg_match_all( '/<(ph .*?)>/iu', $segment, $phTags ); |
|
69 | + preg_match_all('/<(ph .*?)>/iu', $segment, $phTags); |
|
70 | 70 | |
71 | - if ( count( $phTags[ 0 ] ) === 0 ) { |
|
71 | + if (count($phTags[0]) === 0) { |
|
72 | 72 | return $segment; |
73 | 73 | } |
74 | 74 | |
75 | - foreach ( $phTags[ 0 ] as $phTag ) { |
|
75 | + foreach ($phTags[0] as $phTag) { |
|
76 | 76 | |
77 | 77 | // check if phTag has not any correspondence on dataRef map |
78 | - if ( $this->isAValidPhTag( $phTag ) ) { |
|
78 | + if ($this->isAValidPhTag($phTag)) { |
|
79 | 79 | $segment = preg_replace( |
80 | - '/' . preg_quote( $phTag, '/' ) . '/', |
|
81 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
82 | - '" ctype="' . CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF . |
|
83 | - '" equiv-text="base64:' . base64_encode( $phTag ) . |
|
80 | + '/'.preg_quote($phTag, '/').'/', |
|
81 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
82 | + '" ctype="'.CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF. |
|
83 | + '" equiv-text="base64:'.base64_encode($phTag). |
|
84 | 84 | '"/>', |
85 | 85 | $segment, |
86 | 86 | 1 // replace ONLY ONE occurrence |
@@ -100,32 +100,32 @@ discard block |
||
100 | 100 | * |
101 | 101 | * @return bool |
102 | 102 | */ |
103 | - private function isAValidPhTag( $phTag ) { |
|
103 | + private function isAValidPhTag($phTag) { |
|
104 | 104 | |
105 | 105 | // try not to throw exception for wrong segments with opening tags and no closing |
106 | 106 | try { |
107 | - $parsed = XmlParser::parse( $phTag, true ); |
|
108 | - } catch ( Exception $e ){ |
|
107 | + $parsed = XmlParser::parse($phTag, true); |
|
108 | + } catch (Exception $e) { |
|
109 | 109 | return false; |
110 | 110 | } |
111 | 111 | |
112 | - if ( $parsed->count() == 0 ) { |
|
112 | + if ($parsed->count() == 0) { |
|
113 | 113 | return false; |
114 | 114 | } |
115 | 115 | |
116 | 116 | // check for matecat ctype |
117 | - $cType = isset( $parsed[ 0 ]->attributes[ 'ctype' ] ) ? $parsed[ 0 ]->attributes[ 'ctype' ] : null; |
|
118 | - if ( CTypeEnum::isMatecatCType( $cType ) ) { |
|
117 | + $cType = isset($parsed[0]->attributes['ctype']) ? $parsed[0]->attributes['ctype'] : null; |
|
118 | + if (CTypeEnum::isMatecatCType($cType)) { |
|
119 | 119 | return false; |
120 | 120 | } |
121 | 121 | |
122 | 122 | // if has equiv-text don't touch |
123 | - if ( isset( $parsed[ 0 ]->attributes[ 'equiv-text' ] ) ) { |
|
123 | + if (isset($parsed[0]->attributes['equiv-text'])) { |
|
124 | 124 | return false; |
125 | 125 | } |
126 | 126 | |
127 | - if ( isset( $parsed[ 0 ]->attributes[ 'dataRef' ] ) ) { |
|
128 | - return !array_key_exists( $parsed[ 0 ]->attributes[ 'dataRef' ], $this->dataRefMap ); |
|
127 | + if (isset($parsed[0]->attributes['dataRef'])) { |
|
128 | + return !array_key_exists($parsed[0]->attributes['dataRef'], $this->dataRefMap); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | return true; |
@@ -148,33 +148,33 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return string|string[] |
150 | 150 | */ |
151 | - private function replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags( $segment ) { |
|
151 | + private function replace_Pc_TagsWithoutDataRefCorrespondenceToMatecatPhTags($segment) { |
|
152 | 152 | |
153 | - preg_match_all( '/<(pc .*?)>/iu', $segment, $openingPcTags ); |
|
154 | - preg_match_all( '|<(/pc)>|iu', $segment, $closingPcTags ); |
|
153 | + preg_match_all('/<(pc .*?)>/iu', $segment, $openingPcTags); |
|
154 | + preg_match_all('|<(/pc)>|iu', $segment, $closingPcTags); |
|
155 | 155 | |
156 | - if ( count( $openingPcTags[ 0 ] ) === 0 ) { |
|
156 | + if (count($openingPcTags[0]) === 0) { |
|
157 | 157 | return $segment; |
158 | 158 | } |
159 | 159 | |
160 | - foreach ( $openingPcTags[ 0 ] as $openingPcTag ) { |
|
160 | + foreach ($openingPcTags[0] as $openingPcTag) { |
|
161 | 161 | $segment = preg_replace( |
162 | - '/' . preg_quote( $openingPcTag, '/' ) . '/', |
|
163 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
164 | - '" ctype="' . CTypeEnum::ORIGINAL_PC_OPEN . |
|
165 | - '" equiv-text="base64:' . base64_encode( $openingPcTag ) . |
|
162 | + '/'.preg_quote($openingPcTag, '/').'/', |
|
163 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
164 | + '" ctype="'.CTypeEnum::ORIGINAL_PC_OPEN. |
|
165 | + '" equiv-text="base64:'.base64_encode($openingPcTag). |
|
166 | 166 | '"/>', |
167 | 167 | $segment, |
168 | 168 | 1 |
169 | 169 | ); |
170 | 170 | } |
171 | 171 | |
172 | - foreach ( $closingPcTags[ 0 ] as $closingPcTag ) { |
|
172 | + foreach ($closingPcTags[0] as $closingPcTag) { |
|
173 | 173 | $segment = preg_replace( |
174 | - '/' . preg_quote( $closingPcTag, '/' ) . '/', |
|
175 | - '<ph id="' . $this->getPipeline()->getNextId() . |
|
176 | - '" ctype="' . CTypeEnum::ORIGINAL_PC_CLOSE . |
|
177 | - '" equiv-text="base64:' . base64_encode( $closingPcTag ) . |
|
174 | + '/'.preg_quote($closingPcTag, '/').'/', |
|
175 | + '<ph id="'.$this->getPipeline()->getNextId(). |
|
176 | + '" ctype="'.CTypeEnum::ORIGINAL_PC_CLOSE. |
|
177 | + '" equiv-text="base64:'.base64_encode($closingPcTag). |
|
178 | 178 | '"/>', |
179 | 179 | $segment, |
180 | 180 | 1 |
@@ -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( null, $string ); |
|
78 | - $string = $this->replaceClosingPcTags( $string, $dataRefEndMap ); |
|
77 | + $string = $this->replaceOpeningPcTags(null, $string); |
|
78 | + $string = $this->replaceClosingPcTags($string, $dataRefEndMap); |
|
79 | 79 | |
80 | - } catch ( Exception $ignore ) { |
|
80 | + } catch (Exception $ignore) { |
|
81 | 81 | } finally { |
82 | 82 | return $string; |
83 | 83 | } |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * |
90 | 90 | * @return bool |
91 | 91 | */ |
92 | - private function hasAnyDataRefAttribute( $string ) { |
|
93 | - return (bool)preg_match( '/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string ); |
|
92 | + private function hasAnyDataRefAttribute($string) { |
|
93 | + return (bool) preg_match('/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -106,17 +106,17 @@ discard block |
||
106 | 106 | * |
107 | 107 | * @return string |
108 | 108 | */ |
109 | - private function recursiveTransformDataRefToPhTag( $node, $string ) { |
|
109 | + private function recursiveTransformDataRefToPhTag($node, $string) { |
|
110 | 110 | |
111 | - if ( $node->has_children ) { |
|
111 | + if ($node->has_children) { |
|
112 | 112 | |
113 | - foreach ( $node->inner_html as $childNode ) { |
|
114 | - $string = $this->recursiveTransformDataRefToPhTag( $childNode, $string ); |
|
113 | + foreach ($node->inner_html as $childNode) { |
|
114 | + $string = $this->recursiveTransformDataRefToPhTag($childNode, $string); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | } else { |
118 | 118 | |
119 | - switch ( $node->tagName ) { |
|
119 | + switch ($node->tagName) { |
|
120 | 120 | case 'ph': |
121 | 121 | case 'sc': |
122 | 122 | case 'ec': |
@@ -125,48 +125,48 @@ discard block |
||
125 | 125 | return $string; |
126 | 126 | } |
127 | 127 | |
128 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
128 | + if (!isset($node->attributes['dataRef'])) { |
|
129 | 129 | return $string; |
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( $node->attributes[ 'dataRef' ], array_keys( $this->map ) ) ) { |
|
134 | + if (!in_array($node->attributes['dataRef'], array_keys($this->map))) { |
|
135 | 135 | return $string; |
136 | 136 | } |
137 | 137 | |
138 | - $dataRefName = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
139 | - $dataRefValue = $this->map[ $dataRefName ]; // map identifier. Eg: source1 |
|
138 | + $dataRefName = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
139 | + $dataRefValue = $this->map[$dataRefName]; // map identifier. Eg: source1 |
|
140 | 140 | |
141 | 141 | // check if is null or an empty string, in this case, convert it to NULL string |
142 | - if ( is_null( $dataRefValue ) || $dataRefValue === '' ) { |
|
143 | - $this->map[ $dataRefName ] = 'NULL'; |
|
142 | + if (is_null($dataRefValue) || $dataRefValue === '') { |
|
143 | + $this->map[$dataRefName] = 'NULL'; |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | |
147 | - $newTag = [ '<ph' ]; |
|
147 | + $newTag = ['<ph']; |
|
148 | 148 | |
149 | 149 | // if there is no id copy it from dataRef |
150 | - if ( !isset( $node->attributes[ 'id' ] ) ) { |
|
151 | - $newTag[] = 'id="' . $dataRefName . '"'; |
|
150 | + if (!isset($node->attributes['id'])) { |
|
151 | + $newTag[] = 'id="'.$dataRefName.'"'; |
|
152 | 152 | $newTag[] = 'x-removeId="true"'; |
153 | 153 | } else { |
154 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '"'; |
|
154 | + $newTag[] = 'id="'.$node->attributes['id'].'"'; |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | // introduce dataType for <ec>/<sc> tag handling |
158 | - if ( $node->tagName === 'ec' ) { |
|
159 | - $newTag[] = 'ctype="' . CTypeEnum::EC_DATA_REF . '"'; |
|
160 | - } elseif ( $node->tagName === 'sc' ) { |
|
161 | - $newTag[] = 'ctype="' . CTypeEnum::SC_DATA_REF . '"'; |
|
158 | + if ($node->tagName === 'ec') { |
|
159 | + $newTag[] = 'ctype="'.CTypeEnum::EC_DATA_REF.'"'; |
|
160 | + } elseif ($node->tagName === 'sc') { |
|
161 | + $newTag[] = 'ctype="'.CTypeEnum::SC_DATA_REF.'"'; |
|
162 | 162 | } else { |
163 | - $newTag[] = 'ctype="' . CTypeEnum::PH_DATA_REF . '"'; |
|
163 | + $newTag[] = 'ctype="'.CTypeEnum::PH_DATA_REF.'"'; |
|
164 | 164 | } |
165 | 165 | |
166 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"'; |
|
167 | - $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
|
166 | + $newTag[] = 'equiv-text="base64:'.base64_encode($dataRefValue).'"'; |
|
167 | + $newTag[] = 'x-orig="'.base64_encode($node->node).'"'; |
|
168 | 168 | |
169 | - return str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
|
169 | + return str_replace($node->node, implode(" ", $newTag).'/>', $string); |
|
170 | 170 | |
171 | 171 | } |
172 | 172 | |
@@ -182,29 +182,29 @@ 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 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
|
195 | + if (isset($node->attributes['dataRefStart']) && array_key_exists($node->attributes['dataRefStart'], $this->map)) { |
|
196 | 196 | |
197 | - $newTag = [ '<ph' ]; |
|
197 | + $newTag = ['<ph']; |
|
198 | 198 | |
199 | - if ( isset( $node->attributes[ 'id' ] ) ) { |
|
200 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '_1"'; |
|
199 | + if (isset($node->attributes['id'])) { |
|
200 | + $newTag[] = 'id="'.$node->attributes['id'].'_1"'; |
|
201 | 201 | } |
202 | 202 | |
203 | - $newTag[] = 'ctype="' . CTypeEnum::PC_SELF_CLOSE_DATA_REF . '"'; |
|
204 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"'; |
|
205 | - $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
|
203 | + $newTag[] = 'ctype="'.CTypeEnum::PC_SELF_CLOSE_DATA_REF.'"'; |
|
204 | + $newTag[] = 'equiv-text="base64:'.base64_encode($this->map[$node->attributes['dataRefStart']]).'"'; |
|
205 | + $newTag[] = 'x-orig="'.base64_encode($node->node).'"'; |
|
206 | 206 | |
207 | - $string = str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
|
207 | + $string = str_replace($node->node, implode(" ", $newTag).'/>', $string); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | } |
@@ -219,27 +219,27 @@ discard block |
||
219 | 219 | * @param object $node |
220 | 220 | * @param $dataRefEndMap |
221 | 221 | */ |
222 | - private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
|
222 | + private function extractDataRefMapRecursively($node, &$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, $dataRefEndMap ); |
|
225 | + if ($node->has_children) { |
|
226 | + foreach ($node->inner_html as $nestedNode) { |
|
227 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | 231 | // EXCLUDE self closed <pc/> |
232 | - if ( $node->tagName === 'pc' && $node->self_closed === false ) { |
|
233 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
234 | - $dataRefEnd = $node->attributes[ 'dataRefEnd' ]; |
|
235 | - } elseif ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
236 | - $dataRefEnd = $node->attributes[ 'dataRefStart' ]; |
|
232 | + if ($node->tagName === 'pc' && $node->self_closed === false) { |
|
233 | + if (isset($node->attributes['dataRefEnd'])) { |
|
234 | + $dataRefEnd = $node->attributes['dataRefEnd']; |
|
235 | + } elseif (isset($node->attributes['dataRefStart'])) { |
|
236 | + $dataRefEnd = $node->attributes['dataRefStart']; |
|
237 | 237 | } else { |
238 | 238 | $dataRefEnd = null; |
239 | 239 | } |
240 | 240 | |
241 | 241 | $dataRefEndMap[] = [ |
242 | - 'id' => isset( $node->attributes[ 'id' ] ) ? $node->attributes[ 'id' ] : null, |
|
242 | + 'id' => isset($node->attributes['id']) ? $node->attributes['id'] : null, |
|
243 | 243 | 'dataRefEnd' => $dataRefEnd, |
244 | 244 | ]; |
245 | 245 | |
@@ -257,40 +257,40 @@ discard block |
||
257 | 257 | * @throws InvalidXmlException |
258 | 258 | * @throws XmlParsingException |
259 | 259 | */ |
260 | - private function replaceOpeningPcTags( $node, $string ) { |
|
260 | + private function replaceOpeningPcTags($node, $string) { |
|
261 | 261 | |
262 | - preg_match_all( '|<pc ([^>/]+?)>|iu', $string, $openingPcMatches ); |
|
262 | + preg_match_all('|<pc ([^>/]+?)>|iu', $string, $openingPcMatches); |
|
263 | 263 | |
264 | - foreach ( $openingPcMatches[ 0 ] as $match ) { |
|
264 | + foreach ($openingPcMatches[0] as $match) { |
|
265 | 265 | |
266 | - $node = XmlParser::parse( $match . '</pc>', true )[ 0 ]; // add a closing tag to not break xml integrity |
|
266 | + $node = XmlParser::parse($match.'</pc>', true)[0]; // add a closing tag to not break xml integrity |
|
267 | 267 | |
268 | 268 | // CASE 1 - Missing `dataRefStart` |
269 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) && !isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
270 | - $node->attributes[ 'dataRefStart' ] = $node->attributes[ 'dataRefEnd' ]; |
|
269 | + if (isset($node->attributes['dataRefEnd']) && !isset($node->attributes['dataRefStart'])) { |
|
270 | + $node->attributes['dataRefStart'] = $node->attributes['dataRefEnd']; |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | // CASE 2 - Missing `dataRefEnd` |
274 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) && !isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
275 | - $node->attributes[ 'dataRefEnd' ] = $node->attributes[ 'dataRefStart' ]; |
|
274 | + if (isset($node->attributes['dataRefStart']) && !isset($node->attributes['dataRefEnd'])) { |
|
275 | + $node->attributes['dataRefEnd'] = $node->attributes['dataRefStart']; |
|
276 | 276 | } |
277 | 277 | |
278 | - if ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
278 | + if (isset($node->attributes['dataRefStart'])) { |
|
279 | 279 | |
280 | - $startValue = $this->map[ $node->attributes[ 'dataRefStart' ] ] ?: 'NULL'; //handling null values in original data map |
|
280 | + $startValue = $this->map[$node->attributes['dataRefStart']] ?: 'NULL'; //handling null values in original data map |
|
281 | 281 | |
282 | - $newTag = [ '<ph' ]; |
|
282 | + $newTag = ['<ph']; |
|
283 | 283 | |
284 | - if ( isset( $node->attributes[ 'id' ] ) ) { |
|
285 | - $newTag[] = 'id="' . $node->attributes[ 'id' ] . '_1"'; |
|
284 | + if (isset($node->attributes['id'])) { |
|
285 | + $newTag[] = 'id="'.$node->attributes['id'].'_1"'; |
|
286 | 286 | } |
287 | 287 | |
288 | - $newTag[] = 'ctype="' . CTypeEnum::PC_OPEN_DATA_REF . '"'; |
|
289 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $startValue ) . '"'; |
|
290 | - $newTag[] = 'x-orig="' . base64_encode( $match ) . '"'; |
|
288 | + $newTag[] = 'ctype="'.CTypeEnum::PC_OPEN_DATA_REF.'"'; |
|
289 | + $newTag[] = 'equiv-text="base64:'.base64_encode($startValue).'"'; |
|
290 | + $newTag[] = 'x-orig="'.base64_encode($match).'"'; |
|
291 | 291 | |
292 | 292 | // conversion for opening <pc> tag |
293 | - $string = str_replace( $match, implode( " ", $newTag ) . '/>', $string ); |
|
293 | + $string = str_replace($match, implode(" ", $newTag).'/>', $string); |
|
294 | 294 | |
295 | 295 | } |
296 | 296 | } |
@@ -307,37 +307,37 @@ discard block |
||
307 | 307 | * |
308 | 308 | * @return string |
309 | 309 | */ |
310 | - private function replaceClosingPcTags( $string, $dataRefEndMap = [] ) { |
|
310 | + private function replaceClosingPcTags($string, $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 = isset( $dataRefEndMap[ $index ] ) ? $dataRefEndMap[ $index ] : null; |
|
320 | + $attr = isset($dataRefEndMap[$index]) ? $dataRefEndMap[$index] : null; |
|
321 | 321 | |
322 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
322 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
323 | 323 | |
324 | - $endValue = !empty( $this->map[ $attr[ 'dataRefEnd' ] ] ) ? $this->map[ $attr[ 'dataRefEnd' ] ] : 'NULL'; |
|
324 | + $endValue = !empty($this->map[$attr['dataRefEnd']]) ? $this->map[$attr['dataRefEnd']] : 'NULL'; |
|
325 | 325 | |
326 | - $newTag = [ '<ph' ]; |
|
326 | + $newTag = ['<ph']; |
|
327 | 327 | |
328 | - if ( isset( $attr[ 'id' ] ) ) { |
|
329 | - $newTag[] = 'id="' . $attr[ 'id' ] . '_2"'; |
|
328 | + if (isset($attr['id'])) { |
|
329 | + $newTag[] = 'id="'.$attr['id'].'_2"'; |
|
330 | 330 | } |
331 | 331 | |
332 | - $newTag[] = 'ctype="' . CTypeEnum::PC_CLOSE_DATA_REF . '"'; |
|
333 | - $newTag[] = 'equiv-text="base64:' . base64_encode( $endValue ) . '"'; |
|
334 | - $newTag[] = 'x-orig="' . base64_encode( '</pc>' ) . '"'; |
|
332 | + $newTag[] = 'ctype="'.CTypeEnum::PC_CLOSE_DATA_REF.'"'; |
|
333 | + $newTag[] = 'equiv-text="base64:'.base64_encode($endValue).'"'; |
|
334 | + $newTag[] = 'x-orig="'.base64_encode('</pc>').'"'; |
|
335 | 335 | |
336 | 336 | // conversion for opening <pc> tag |
337 | - $completeTag = implode( " ", $newTag ) . '/>'; |
|
338 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
339 | - $string = substr_replace( $string, $completeTag, $realOffset, $length ); |
|
340 | - $delta = $delta + strlen( $completeTag ) - $length; |
|
337 | + $completeTag = implode(" ", $newTag).'/>'; |
|
338 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
339 | + $string = substr_replace($string, $completeTag, $realOffset, $length); |
|
340 | + $delta = $delta + strlen($completeTag) - $length; |
|
341 | 341 | |
342 | 342 | } |
343 | 343 | |
@@ -355,19 +355,19 @@ discard block |
||
355 | 355 | * @throws InvalidXmlException |
356 | 356 | * @throws XmlParsingException |
357 | 357 | */ |
358 | - public function restore( $string ) { |
|
358 | + public function restore($string) { |
|
359 | 359 | |
360 | 360 | // if map is empty return string as is |
361 | - if ( empty( $this->map ) ) { |
|
361 | + if (empty($this->map)) { |
|
362 | 362 | return $string; |
363 | 363 | } |
364 | 364 | |
365 | 365 | // replace eventual empty equiv-text="" |
366 | 366 | // $string = str_replace( ' equiv-text=""', '', $string ); |
367 | - $html = XmlParser::parse( $string, true ); |
|
367 | + $html = XmlParser::parse($string, true); |
|
368 | 368 | |
369 | - foreach ( $html as $node ) { |
|
370 | - $string = $this->recursiveRestoreOriginalTags( $node, $string ); |
|
369 | + foreach ($html as $node) { |
|
370 | + $string = $this->recursiveRestoreOriginalTags($node, $string); |
|
371 | 371 | } |
372 | 372 | |
373 | 373 | return $string; |
@@ -379,21 +379,21 @@ discard block |
||
379 | 379 | * |
380 | 380 | * @return string |
381 | 381 | */ |
382 | - private function recursiveRestoreOriginalTags( $node, $string ) { |
|
382 | + private function recursiveRestoreOriginalTags($node, $string) { |
|
383 | 383 | |
384 | - if ( $node->has_children ) { |
|
384 | + if ($node->has_children) { |
|
385 | 385 | |
386 | - foreach ( $node->inner_html as $childNode ) { |
|
387 | - $string = $this->recursiveRestoreOriginalTags( $childNode, $string ); |
|
386 | + foreach ($node->inner_html as $childNode) { |
|
387 | + $string = $this->recursiveRestoreOriginalTags($childNode, $string); |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | } else { |
391 | 391 | |
392 | - $cType = isset( $node->attributes[ 'ctype' ] ) ? $node->attributes[ 'ctype' ] : null; |
|
392 | + $cType = isset($node->attributes['ctype']) ? $node->attributes['ctype'] : null; |
|
393 | 393 | |
394 | - if ( $cType ) { |
|
394 | + if ($cType) { |
|
395 | 395 | |
396 | - switch ( $node->attributes[ 'ctype' ] ) { |
|
396 | + switch ($node->attributes['ctype']) { |
|
397 | 397 | case CTypeEnum::ORIGINAL_PC_OPEN: |
398 | 398 | case CTypeEnum::ORIGINAL_PC_CLOSE: |
399 | 399 | case CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF: |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | case CTypeEnum::PC_SELF_CLOSE_DATA_REF: |
404 | 404 | case CTypeEnum::SC_DATA_REF: |
405 | 405 | case CTypeEnum::EC_DATA_REF: |
406 | - return preg_replace( '/' . preg_quote( $node->node, '/' ) . '/', base64_decode( $node->attributes[ 'x-orig' ] ), $string, 1 ); |
|
406 | + return preg_replace('/'.preg_quote($node->node, '/').'/', base64_decode($node->attributes['x-orig']), $string, 1); |
|
407 | 407 | } |
408 | 408 | |
409 | 409 | } |
@@ -39,16 +39,16 @@ |
||
39 | 39 | protected static $constantsValues = []; |
40 | 40 | |
41 | 41 | protected static function getConstantsMap() { |
42 | - if ( empty( static::$constantsValues ) ) { |
|
43 | - $reflectedProperty = ( new ReflectionClass( static::class ) )->getConstants(); |
|
44 | - static::$constantsValues = array_flip( $reflectedProperty ); |
|
42 | + if (empty(static::$constantsValues)) { |
|
43 | + $reflectedProperty = (new ReflectionClass(static::class))->getConstants(); |
|
44 | + static::$constantsValues = array_flip($reflectedProperty); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | return static::$constantsValues; |
48 | 48 | } |
49 | 49 | |
50 | - public static function isMatecatCType( $ctype ) { |
|
51 | - return array_key_exists( $ctype, static::getConstantsMap() ); |
|
50 | + public static function isMatecatCType($ctype) { |
|
51 | + return array_key_exists($ctype, static::getConstantsMap()); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | } |
55 | 55 | \ No newline at end of file |