@@ -10,22 +10,22 @@ discard block |
||
10 | 10 | /** |
11 | 11 | * @inheritDoc |
12 | 12 | */ |
13 | - protected function tagOpen( $parser, $name, $attr ) { |
|
13 | + protected function tagOpen($parser, $name, $attr) { |
|
14 | 14 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
15 | - if ( $this->tuTagName === $name ) { |
|
15 | + if ($this->tuTagName === $name) { |
|
16 | 16 | $this->inTU = true; |
17 | 17 | |
18 | 18 | // get id |
19 | 19 | // trim to first 100 characters because this is the limit on Matecat's DB |
20 | - $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
|
20 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
21 | 21 | |
22 | 22 | // current 'translate' attribute of the current trans-unit |
23 | - $this->currentTransUnitTranslate = isset( $attr[ 'translate' ] ) ? $attr[ 'translate' ] : 'yes'; |
|
23 | + $this->currentTransUnitTranslate = isset($attr['translate']) ? $attr['translate'] : 'yes'; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | // check if we are entering into a <target> |
27 | - if ( 'target' == $name ) { |
|
28 | - if ( $this->currentTransUnitTranslate === 'no' ) { |
|
27 | + if ('target' == $name) { |
|
28 | + if ($this->currentTransUnitTranslate === 'no') { |
|
29 | 29 | $this->inTarget = false; |
30 | 30 | } else { |
31 | 31 | $this->inTarget = true; |
@@ -33,13 +33,13 @@ discard block |
||
33 | 33 | } |
34 | 34 | |
35 | 35 | // reset Marker positions |
36 | - if ( 'sdl:seg-defs' == $name ) { |
|
36 | + if ('sdl:seg-defs' == $name) { |
|
37 | 37 | $this->markerPos = 0; |
38 | 38 | } |
39 | 39 | |
40 | 40 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
41 | 41 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
42 | - if ( !$this->inTarget ) { |
|
42 | + if (!$this->inTarget) { |
|
43 | 43 | |
44 | 44 | // costruct tag |
45 | 45 | $tag = "<$name "; |
@@ -49,23 +49,23 @@ discard block |
||
49 | 49 | // so the check on it's name is not enough |
50 | 50 | $_sdlStatus_confWritten = false; |
51 | 51 | |
52 | - foreach ( $attr as $k => $v ) { |
|
52 | + foreach ($attr as $k => $v) { |
|
53 | 53 | |
54 | 54 | // if tag name is file, we must replace the target-language attribute |
55 | - if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) { |
|
55 | + if ($name == 'file' && $k == 'target-language' && !empty($this->targetLang)) { |
|
56 | 56 | //replace Target language with job language provided from constructor |
57 | 57 | $tag .= "$k=\"$this->targetLang\" "; |
58 | 58 | |
59 | - if ( null !== $this->logger ) { |
|
60 | - $this->logger->debug( $k . " => " . $this->targetLang ); |
|
59 | + if (null !== $this->logger) { |
|
60 | + $this->logger->debug($k . " => " . $this->targetLang); |
|
61 | 61 | } |
62 | - } elseif ( 'sdl:seg' == $name ) { |
|
62 | + } elseif ('sdl:seg' == $name) { |
|
63 | 63 | |
64 | 64 | // write the confidence level for this segment ( Translated, Draft, etc. ) |
65 | - if ( isset( $this->segments[ 'matecat|' . $this->currentTransUnitId ] ) && $_sdlStatus_confWritten === false ) { |
|
65 | + if (isset($this->segments['matecat|' . $this->currentTransUnitId]) && $_sdlStatus_confWritten === false) { |
|
66 | 66 | |
67 | 67 | // append definition attribute |
68 | - $tag .= $this->prepareTargetStatuses( $this->lastTransUnit[ $this->markerPos ] ); |
|
68 | + $tag .= $this->prepareTargetStatuses($this->lastTransUnit[$this->markerPos]); |
|
69 | 69 | |
70 | 70 | //prepare for an eventual next cycle |
71 | 71 | $this->markerPos++; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | } |
74 | 74 | |
75 | 75 | // Warning, this is NOT an elseif |
76 | - if ( $k != 'conf' ) { |
|
76 | + if ($k != 'conf') { |
|
77 | 77 | //put also the current attribute in it if it is not a "conf" attribute |
78 | 78 | $tag .= "$k=\"$v\" "; |
79 | 79 | } |
@@ -86,29 +86,29 @@ discard block |
||
86 | 86 | // this logic helps detecting empty tags |
87 | 87 | // get current position of SAX pointer in all the stream of data is has read so far: |
88 | 88 | // it points at the end of current tag |
89 | - $idx = xml_get_current_byte_index( $parser ); |
|
89 | + $idx = xml_get_current_byte_index($parser); |
|
90 | 90 | |
91 | 91 | // check whether the bounds of current tag are entirely in current buffer || the end of the current tag |
92 | 92 | // is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
93 | 93 | // this check is necessary because we may have truncated a tag in half with current read, |
94 | 94 | // and the other half may be encountered in the next buffer it will be passed |
95 | - if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) { |
|
95 | + if (isset($this->currentBuffer[$idx - $this->offset])) { |
|
96 | 96 | // if this tag entire lenght fitted in the buffer, the last char must be the last |
97 | 97 | // symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
98 | 98 | $tmp_offset = $idx - $this->offset; |
99 | - $lastChar = $this->currentBuffer[ $tmp_offset ]; |
|
99 | + $lastChar = $this->currentBuffer[$tmp_offset]; |
|
100 | 100 | } else { |
101 | 101 | //if it's out, simple use the last character of the chunk |
102 | 102 | $tmp_offset = $this->len - 1; |
103 | - $lastChar = $this->currentBuffer[ $tmp_offset ]; |
|
103 | + $lastChar = $this->currentBuffer[$tmp_offset]; |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | // trim last space |
107 | - $tag = rtrim( $tag ); |
|
107 | + $tag = rtrim($tag); |
|
108 | 108 | |
109 | 109 | // detect empty tag |
110 | - $this->isEmpty = ( $lastChar == '/' || $name == 'x' ); |
|
111 | - if ( $this->isEmpty ) { |
|
110 | + $this->isEmpty = ($lastChar == '/' || $name == 'x'); |
|
111 | + if ($this->isEmpty) { |
|
112 | 112 | $tag .= '/'; |
113 | 113 | } |
114 | 114 | |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $tag .= ">"; |
117 | 117 | |
118 | 118 | // set a a Buffer for the segSource Source tag |
119 | - if ( 'source' == $name |
|
119 | + if ('source' == $name |
|
120 | 120 | || 'seg-source' === $name |
121 | 121 | || $this->bufferIsActive |
122 | 122 | || 'value' === $name |
@@ -125,19 +125,19 @@ discard block |
||
125 | 125 | || 'ph' === $name |
126 | 126 | || 'st' === $name |
127 | 127 | || 'note' === $name |
128 | - || 'context' === $name ) { // we are opening a critical CDATA section |
|
128 | + || 'context' === $name) { // we are opening a critical CDATA section |
|
129 | 129 | |
130 | 130 | // WARNING BECAUSE SOURCE AND SEG-SOURCE TAGS CAN BE EMPTY IN SOME CASES!!!!! |
131 | 131 | // so check for isEmpty also in conjunction with name |
132 | - if ( $this->isEmpty && ( 'source' == $name || 'seg-source' == $name ) ) { |
|
133 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
132 | + if ($this->isEmpty && ('source' == $name || 'seg-source' == $name)) { |
|
133 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
134 | 134 | } else { |
135 | 135 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
136 | 136 | $this->bufferIsActive = true; |
137 | - $this->CDATABuffer .= $tag; |
|
137 | + $this->CDATABuffer .= $tag; |
|
138 | 138 | } |
139 | 139 | } else { |
140 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
140 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
141 | 141 | } |
142 | 142 | } |
143 | 143 | } |
@@ -150,19 +150,19 @@ discard block |
||
150 | 150 | * |
151 | 151 | * @return string |
152 | 152 | */ |
153 | - protected function prepareTranslation( $seg, $transUnitTranslation = "" ) { |
|
153 | + protected function prepareTranslation($seg, $transUnitTranslation = "") { |
|
154 | 154 | $endTags = ""; |
155 | 155 | |
156 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
157 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
158 | - $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) && $seg[ 'data_ref_map' ] !== null ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
|
156 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
157 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
158 | + $dataRefMap = (isset($seg['data_ref_map']) && $seg['data_ref_map'] !== null) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
|
159 | 159 | |
160 | - if ( is_null( $seg [ 'translation' ] ) || $seg [ 'translation' ] == '' ) { |
|
160 | + if (is_null($seg ['translation']) || $seg ['translation'] == '') { |
|
161 | 161 | $translation = $segment; |
162 | 162 | } else { |
163 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
163 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
164 | 164 | $error = (isset($seg['error'])) ? $seg['error'] : null; |
165 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
|
165 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) { |
|
166 | 166 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
167 | 167 | } |
168 | 168 | } |
@@ -171,20 +171,20 @@ discard block |
||
171 | 171 | // for Trados the trailing spaces after </mrk> are meaningful |
172 | 172 | // so we trim the translation from Matecat DB and add them after </mrk> |
173 | 173 | $trailingSpaces = ''; |
174 | - for ( $s = 0; $s < Strings::getTheNumberOfTrailingSpaces( $translation ); $s++ ) { |
|
174 | + for ($s = 0; $s < Strings::getTheNumberOfTrailingSpaces($translation); $s++) { |
|
175 | 175 | $trailingSpaces .= ' '; |
176 | 176 | } |
177 | 177 | |
178 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
179 | - if ( $this->targetLang === 'ja-JP' ) { |
|
180 | - $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] ); |
|
178 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
179 | + if ($this->targetLang === 'ja-JP') { |
|
180 | + $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']); |
|
181 | 181 | } |
182 | 182 | |
183 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces; |
|
183 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . rtrim($translation) . $seg['mrk_succ_tags'] . "</mrk>" . $trailingSpaces; |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // we need to trim succ_tags here because we already added the trailing spaces after </mrk> |
187 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . ltrim( $seg[ 'succ_tags' ] ); |
|
187 | + $transUnitTranslation .= $seg['prev_tags'] . $translation . $endTags . ltrim($seg['succ_tags']); |
|
188 | 188 | |
189 | 189 | return $transUnitTranslation; |
190 | 190 | } |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | * |
195 | 195 | * @return string |
196 | 196 | */ |
197 | - protected function prepareTargetStatuses( $segment ) { |
|
197 | + protected function prepareTargetStatuses($segment) { |
|
198 | 198 | $statusMap = [ |
199 | 199 | 'NEW' => '', |
200 | 200 | 'DRAFT' => 'Draft', |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | 'REJECTED' => 'RejectedTranslation', |
204 | 204 | ]; |
205 | 205 | |
206 | - return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" "; |
|
206 | + return "conf=\"{$statusMap[$segment['status']]}\" "; |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | /** |
@@ -213,8 +213,8 @@ discard block |
||
213 | 213 | * |
214 | 214 | * @return array |
215 | 215 | */ |
216 | - protected function setTransUnitState( $seg, $state_prop, $lastMrkState ) { |
|
217 | - return [ null, null ]; |
|
216 | + protected function setTransUnitState($seg, $state_prop, $lastMrkState) { |
|
217 | + return [null, null]; |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | * |
224 | 224 | * @return string |
225 | 225 | */ |
226 | - protected function getWordCountGroup( $raw_word_count, $eq_word_count ) { |
|
226 | + protected function getWordCountGroup($raw_word_count, $eq_word_count) { |
|
227 | 227 | return ''; |
228 | 228 | } |
229 | 229 | } |
@@ -16,30 +16,30 @@ discard block |
||
16 | 16 | * @inheritDoc |
17 | 17 | * @throws \Exception |
18 | 18 | */ |
19 | - public function parse( DOMDocument $dom, $output = [] ) { |
|
19 | + public function parse(DOMDocument $dom, $output = []) { |
|
20 | 20 | $i = 1; |
21 | 21 | /** @var DOMElement $file */ |
22 | - foreach ( $dom->getElementsByTagName( 'file' ) as $file ) { |
|
22 | + foreach ($dom->getElementsByTagName('file') as $file) { |
|
23 | 23 | |
24 | 24 | // metadata |
25 | - $output[ 'files' ][ $i ][ 'attr' ] = $this->extractMetadata( $dom ); |
|
25 | + $output['files'][$i]['attr'] = $this->extractMetadata($dom); |
|
26 | 26 | |
27 | 27 | // notes |
28 | - $output[ 'files' ][ $i ][ 'notes' ] = $this->extractNotes( $file ); |
|
28 | + $output['files'][$i]['notes'] = $this->extractNotes($file); |
|
29 | 29 | |
30 | 30 | // trans-units |
31 | 31 | $transUnitIdArrayForUniquenessCheck = []; |
32 | 32 | $j = 1; |
33 | 33 | /** @var DOMElement $transUnit */ |
34 | - foreach ( $file->childNodes as $childNode ) { |
|
35 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
34 | + foreach ($file->childNodes as $childNode) { |
|
35 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // trans-unit re-count check |
39 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
40 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
41 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
42 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
39 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
40 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
41 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
42 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | $i++; |
@@ -53,23 +53,23 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return array |
55 | 55 | */ |
56 | - private function extractMetadata( DOMDocument $dom ) { |
|
56 | + private function extractMetadata(DOMDocument $dom) { |
|
57 | 57 | $metadata = []; |
58 | 58 | |
59 | - $xliffNode = $dom->getElementsByTagName( 'xliff' )->item( 0 ); |
|
60 | - $fileNode = $dom->getElementsByTagName( 'file' )->item( 0 ); |
|
59 | + $xliffNode = $dom->getElementsByTagName('xliff')->item(0); |
|
60 | + $fileNode = $dom->getElementsByTagName('file')->item(0); |
|
61 | 61 | |
62 | 62 | // original |
63 | - $metadata[ 'original' ] = ( null !== $fileNode->attributes->getNamedItem( 'original' ) ) ? $fileNode->attributes->getNamedItem( 'original' )->nodeValue : 'no-name'; |
|
63 | + $metadata['original'] = (null !== $fileNode->attributes->getNamedItem('original')) ? $fileNode->attributes->getNamedItem('original')->nodeValue : 'no-name'; |
|
64 | 64 | |
65 | 65 | // source-language |
66 | - $metadata[ 'source-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'srcLang' ) ) ? $xliffNode->attributes->getNamedItem( 'srcLang' )->nodeValue : 'en-US'; |
|
66 | + $metadata['source-language'] = (null !== $xliffNode->attributes->getNamedItem('srcLang')) ? $xliffNode->attributes->getNamedItem('srcLang')->nodeValue : 'en-US'; |
|
67 | 67 | |
68 | 68 | // datatype |
69 | 69 | // @TODO to be implemented |
70 | 70 | |
71 | 71 | // target-language |
72 | - $metadata[ 'target-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'trgLang' ) ) ? $xliffNode->attributes->getNamedItem( 'trgLang' )->nodeValue : 'en-US'; |
|
72 | + $metadata['target-language'] = (null !== $xliffNode->attributes->getNamedItem('trgLang')) ? $xliffNode->attributes->getNamedItem('trgLang')->nodeValue : 'en-US'; |
|
73 | 73 | |
74 | 74 | // custom MateCat x-attribute |
75 | 75 | // @TODO to be implemented |
@@ -83,16 +83,16 @@ discard block |
||
83 | 83 | * @return array |
84 | 84 | * @throws \Exception |
85 | 85 | */ |
86 | - private function extractNotes( DOMElement $file ) { |
|
86 | + private function extractNotes(DOMElement $file) { |
|
87 | 87 | $notes = []; |
88 | 88 | |
89 | 89 | // loop <notes> to get nested <note> tag |
90 | - foreach ( $file->childNodes as $childNode ) { |
|
91 | - if ( $childNode->nodeName === 'notes' ) { |
|
92 | - foreach ( $childNode->childNodes as $note ) { |
|
93 | - $noteValue = trim( $note->nodeValue ); |
|
94 | - if ( '' !== $noteValue ) { |
|
95 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
90 | + foreach ($file->childNodes as $childNode) { |
|
91 | + if ($childNode->nodeName === 'notes') { |
|
92 | + foreach ($childNode->childNodes as $note) { |
|
93 | + $noteValue = trim($note->nodeValue); |
|
94 | + if ('' !== $noteValue) { |
|
95 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | } |
@@ -113,34 +113,34 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @throws \Exception |
115 | 115 | */ |
116 | - protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ) { |
|
116 | + protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j) { |
|
117 | 117 | // metadata |
118 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
118 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
119 | 119 | |
120 | 120 | // notes |
121 | 121 | // merge <notes> with key and key-note contained in metadata <mda:metaGroup> |
122 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $transUnit ); |
|
122 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($transUnit); |
|
123 | 123 | |
124 | 124 | // uuid |
125 | - foreach ( $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] as $note ) { |
|
126 | - if ( isset( $note[ 'raw-content' ] ) && Strings::isAValidUuid( $note[ 'raw-content' ] ) ) { |
|
127 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ][ 'uuid' ] = $note[ 'raw-content' ]; |
|
125 | + foreach ($output['files'][$i]['trans-units'][$j]['notes'] as $note) { |
|
126 | + if (isset($note['raw-content']) && Strings::isAValidUuid($note['raw-content'])) { |
|
127 | + $output['files'][$i]['trans-units'][$j]['attr']['uuid'] = $note['raw-content']; |
|
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | 131 | // original-data (exclusive for V2) |
132 | 132 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html#originaldata |
133 | - $originalData = $this->extractTransUnitOriginalData( $transUnit ); |
|
133 | + $originalData = $this->extractTransUnitOriginalData($transUnit); |
|
134 | 134 | $dataRefMap = null; |
135 | - if ( !empty( $originalData ) ) { |
|
136 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'original-data' ] = $originalData; |
|
137 | - $dataRefMap = $this->getDataRefMap( $originalData ); |
|
135 | + if (!empty($originalData)) { |
|
136 | + $output['files'][$i]['trans-units'][$j]['original-data'] = $originalData; |
|
137 | + $dataRefMap = $this->getDataRefMap($originalData); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | // additionalTagData (exclusive for V2) |
141 | - $additionalTagData = $this->extractTransUnitAdditionalTagData( $transUnit ); |
|
142 | - if ( !empty( $additionalTagData ) ) { |
|
143 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'additional-tag-data' ] = $additionalTagData; |
|
141 | + $additionalTagData = $this->extractTransUnitAdditionalTagData($transUnit); |
|
142 | + if (!empty($additionalTagData)) { |
|
143 | + $output['files'][$i]['trans-units'][$j]['additional-tag-data'] = $additionalTagData; |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | // content |
@@ -160,64 +160,64 @@ discard block |
||
160 | 160 | |
161 | 161 | /** @var DOMElement $segment */ |
162 | 162 | $c = 0; |
163 | - foreach ( $transUnit->childNodes as $segment ) { |
|
164 | - if ( $segment->nodeName === 'segment' ) { |
|
163 | + foreach ($transUnit->childNodes as $segment) { |
|
164 | + if ($segment->nodeName === 'segment') { |
|
165 | 165 | |
166 | 166 | // check segment id consistency |
167 | - $attr = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ]; |
|
168 | - $this->checkSegmentIdConsistency( $segment, $attr ); |
|
167 | + $attr = $output['files'][$i]['trans-units'][$j]['attr']; |
|
168 | + $this->checkSegmentIdConsistency($segment, $attr); |
|
169 | 169 | |
170 | 170 | // loop <segment> to get nested <source> and <target> tag |
171 | - foreach ( $segment->childNodes as $childNode ) { |
|
172 | - if ( $childNode->nodeName === 'source' ) { |
|
173 | - $extractedSource = $this->extractContent( $dom, $childNode ); |
|
174 | - $source[ 'raw-content' ][ $c ] = $extractedSource[ 'raw-content' ]; |
|
171 | + foreach ($segment->childNodes as $childNode) { |
|
172 | + if ($childNode->nodeName === 'source') { |
|
173 | + $extractedSource = $this->extractContent($dom, $childNode); |
|
174 | + $source['raw-content'][$c] = $extractedSource['raw-content']; |
|
175 | 175 | |
176 | - if ( !empty( $originalData ) ) { |
|
177 | - $source[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $source[ 'raw-content' ][ $c ] ); |
|
176 | + if (!empty($originalData)) { |
|
177 | + $source['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($source['raw-content'][$c]); |
|
178 | 178 | } |
179 | 179 | |
180 | - if ( !empty( $extractedSource[ 'attr' ] ) ) { |
|
181 | - $source[ 'attr' ][ $c ] = $extractedSource[ 'attr' ]; |
|
180 | + if (!empty($extractedSource['attr'])) { |
|
181 | + $source['attr'][$c] = $extractedSource['attr']; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | // append value to 'seg-source' |
185 | - if ( $this->stringContainsMarks( $extractedSource[ 'raw-content' ] ) ) { |
|
186 | - $segSource = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedSource[ 'raw-content' ], $originalData ); |
|
185 | + if ($this->stringContainsMarks($extractedSource['raw-content'])) { |
|
186 | + $segSource = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedSource['raw-content'], $originalData); |
|
187 | 187 | } else { |
188 | 188 | $segSource[] = [ |
189 | - 'attr' => $this->extractTagAttributes( $segment), |
|
190 | - 'mid' => count( $segSource ) > 0 ? count( $segSource ) : 0, |
|
189 | + 'attr' => $this->extractTagAttributes($segment), |
|
190 | + 'mid' => count($segSource) > 0 ? count($segSource) : 0, |
|
191 | 191 | 'ext-prec-tags' => '', |
192 | - 'raw-content' => $extractedSource[ 'raw-content' ], |
|
193 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedSource[ 'raw-content' ] ) : null, |
|
192 | + 'raw-content' => $extractedSource['raw-content'], |
|
193 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedSource['raw-content']) : null, |
|
194 | 194 | 'ext-succ-tags' => '', |
195 | 195 | ]; |
196 | 196 | } |
197 | 197 | } |
198 | 198 | |
199 | - if ( $childNode->nodeName === 'target' ) { |
|
200 | - $extractedTarget = $this->extractContent( $dom, $childNode ); |
|
201 | - $target[ 'raw-content' ][ $c ] = $extractedTarget[ 'raw-content' ]; |
|
199 | + if ($childNode->nodeName === 'target') { |
|
200 | + $extractedTarget = $this->extractContent($dom, $childNode); |
|
201 | + $target['raw-content'][$c] = $extractedTarget['raw-content']; |
|
202 | 202 | |
203 | - if ( !empty( $originalData ) ) { |
|
204 | - $target[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $target[ 'raw-content' ][ $c ] ); |
|
203 | + if (!empty($originalData)) { |
|
204 | + $target['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($target['raw-content'][$c]); |
|
205 | 205 | } |
206 | 206 | |
207 | - if ( !empty( $extractedTarget[ 'attr' ] ) ) { |
|
208 | - $target[ 'attr' ][ $c ] = $extractedTarget[ 'attr' ]; |
|
207 | + if (!empty($extractedTarget['attr'])) { |
|
208 | + $target['attr'][$c] = $extractedTarget['attr']; |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | // append value to 'seg-target' |
212 | - if ( $this->stringContainsMarks( $extractedTarget[ 'raw-content' ] ) ) { |
|
213 | - $segTarget = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedTarget[ 'raw-content' ], $originalData ); |
|
212 | + if ($this->stringContainsMarks($extractedTarget['raw-content'])) { |
|
213 | + $segTarget = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedTarget['raw-content'], $originalData); |
|
214 | 214 | } else { |
215 | 215 | $segTarget[] = [ |
216 | - 'attr' => $this->extractTagAttributes( $segment), |
|
217 | - 'mid' => count( $segTarget ) > 0 ? count( $segTarget ) : 0, |
|
216 | + 'attr' => $this->extractTagAttributes($segment), |
|
217 | + 'mid' => count($segTarget) > 0 ? count($segTarget) : 0, |
|
218 | 218 | 'ext-prec-tags' => '', |
219 | - 'raw-content' => $extractedTarget[ 'raw-content' ], |
|
220 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedTarget[ 'raw-content' ] ) : null, |
|
219 | + 'raw-content' => $extractedTarget['raw-content'], |
|
220 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedTarget['raw-content']) : null, |
|
221 | 221 | 'ext-succ-tags' => '', |
222 | 222 | ]; |
223 | 223 | } |
@@ -228,10 +228,10 @@ discard block |
||
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $source; |
|
232 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $target; |
|
233 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $segSource; |
|
234 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $segTarget; |
|
231 | + $output['files'][$i]['trans-units'][$j]['source'] = $source; |
|
232 | + $output['files'][$i]['trans-units'][$j]['target'] = $target; |
|
233 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $segSource; |
|
234 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $segTarget; |
|
235 | 235 | |
236 | 236 | $j++; |
237 | 237 | } |
@@ -242,41 +242,41 @@ discard block |
||
242 | 242 | * |
243 | 243 | * @return array |
244 | 244 | */ |
245 | - private function extractTransUnitMetadata( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck ) { |
|
245 | + private function extractTransUnitMetadata(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck) { |
|
246 | 246 | $metadata = []; |
247 | 247 | |
248 | 248 | // id |
249 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
250 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
249 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
250 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
251 | 251 | } |
252 | 252 | |
253 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
253 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
254 | 254 | |
255 | - if ( strlen( $id ) > 100 ) { |
|
256 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
255 | + if (strlen($id) > 100) { |
|
256 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | $transUnitIdArrayForUniquenessCheck[] = $id; |
260 | - $metadata[ 'id' ] = $id; |
|
260 | + $metadata['id'] = $id; |
|
261 | 261 | |
262 | 262 | // translate |
263 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
264 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
263 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
264 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | // tGroupBegin |
268 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupBegin' ) ) { |
|
269 | - $metadata[ 'tGroupBegin' ] = $transUnit->attributes->getNamedItem( 'tGroupBegin' )->nodeValue; |
|
268 | + if (null !== $transUnit->attributes->getNamedItem('tGroupBegin')) { |
|
269 | + $metadata['tGroupBegin'] = $transUnit->attributes->getNamedItem('tGroupBegin')->nodeValue; |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | // tGroupEnd |
273 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupEnd' ) ) { |
|
274 | - $metadata[ 'tGroupEnd' ] = $transUnit->attributes->getNamedItem( 'tGroupEnd' )->nodeValue; |
|
273 | + if (null !== $transUnit->attributes->getNamedItem('tGroupEnd')) { |
|
274 | + $metadata['tGroupEnd'] = $transUnit->attributes->getNamedItem('tGroupEnd')->nodeValue; |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | // sizeRestriction |
278 | - if ( null !== $transUnit->attributes->getNamedItem( 'sizeRestriction' ) && '' !== $transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue ) { |
|
279 | - $metadata[ 'sizeRestriction' ] = (int)$transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue; |
|
278 | + if (null !== $transUnit->attributes->getNamedItem('sizeRestriction') && '' !== $transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue) { |
|
279 | + $metadata['sizeRestriction'] = (int)$transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue; |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | return $metadata; |
@@ -288,31 +288,31 @@ discard block |
||
288 | 288 | * @return array |
289 | 289 | * @throws \Exception |
290 | 290 | */ |
291 | - private function extractTransUnitOriginalData( DOMElement $transUnit ) { |
|
291 | + private function extractTransUnitOriginalData(DOMElement $transUnit) { |
|
292 | 292 | $originalData = []; |
293 | 293 | |
294 | 294 | // loop <originalData> to get nested content |
295 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
296 | - if ( $childNode->nodeName === 'originalData' ) { |
|
297 | - foreach ( $childNode->childNodes as $data ) { |
|
298 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
299 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
295 | + foreach ($transUnit->childNodes as $childNode) { |
|
296 | + if ($childNode->nodeName === 'originalData') { |
|
297 | + foreach ($childNode->childNodes as $data) { |
|
298 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
299 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
300 | 300 | |
301 | - $dataValue = str_replace( Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue ); |
|
302 | - $dataValue = str_replace( Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue ); |
|
303 | - $dataValue = str_replace( Placeholder::TAB_PLACEHOLDER, '\t', $dataValue ); |
|
301 | + $dataValue = str_replace(Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue); |
|
302 | + $dataValue = str_replace(Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue); |
|
303 | + $dataValue = str_replace(Placeholder::TAB_PLACEHOLDER, '\t', $dataValue); |
|
304 | 304 | |
305 | - if ( '' !== $dataValue ) { |
|
305 | + if ('' !== $dataValue) { |
|
306 | 306 | |
307 | - $jsonOrRawContentArray = $this->JSONOrRawContentArray( $dataValue, false ); |
|
307 | + $jsonOrRawContentArray = $this->JSONOrRawContentArray($dataValue, false); |
|
308 | 308 | |
309 | 309 | // restore xliff tags |
310 | - if ( isset( $jsonOrRawContentArray[ 'json' ] ) ) { |
|
311 | - $jsonOrRawContentArray[ 'json' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'json' ] ); |
|
310 | + if (isset($jsonOrRawContentArray['json'])) { |
|
311 | + $jsonOrRawContentArray['json'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['json']); |
|
312 | 312 | } |
313 | 313 | |
314 | - if ( isset( $jsonOrRawContentArray[ 'raw-content' ] ) ) { |
|
315 | - $jsonOrRawContentArray[ 'raw-content' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'raw-content' ] ); |
|
314 | + if (isset($jsonOrRawContentArray['raw-content'])) { |
|
315 | + $jsonOrRawContentArray['raw-content'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['raw-content']); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | $originalData[] = array_merge( |
@@ -337,41 +337,41 @@ discard block |
||
337 | 337 | * |
338 | 338 | * @return array |
339 | 339 | */ |
340 | - private function extractTransUnitAdditionalTagData( DOMElement $transUnit ) { |
|
340 | + private function extractTransUnitAdditionalTagData(DOMElement $transUnit) { |
|
341 | 341 | $additionalTagData = []; |
342 | 342 | |
343 | 343 | // loop <originalData> to get nested content |
344 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
345 | - if ( $childNode->nodeName === 'memsource:additionalTagData' ) { |
|
346 | - foreach ( $childNode->childNodes as $data ) { |
|
344 | + foreach ($transUnit->childNodes as $childNode) { |
|
345 | + if ($childNode->nodeName === 'memsource:additionalTagData') { |
|
346 | + foreach ($childNode->childNodes as $data) { |
|
347 | 347 | $dataArray = []; |
348 | 348 | |
349 | 349 | // id |
350 | - if ( $data->nodeName === 'memsource:tag' ) { |
|
351 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
352 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
353 | - $dataArray[ 'attr' ][ 'id' ] = $dataId; |
|
350 | + if ($data->nodeName === 'memsource:tag') { |
|
351 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
352 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
353 | + $dataArray['attr']['id'] = $dataId; |
|
354 | 354 | } |
355 | 355 | } |
356 | 356 | |
357 | 357 | // in PHP 7.4 $data->childNodes is an empty DomNodeList, it is iterable with size 0 |
358 | 358 | // PHP 5.6 check: in php 5.6 $data->childNodes can be null |
359 | - if ( $data->childNodes != null ) { |
|
359 | + if ($data->childNodes != null) { |
|
360 | 360 | |
361 | 361 | // content |
362 | - foreach ( $data->childNodes as $datum ) { |
|
363 | - if ( $datum->nodeName === 'memsource:tagId' ) { |
|
364 | - $dataArray[ 'raw-content' ][ 'tagId' ] = $datum->nodeValue; |
|
362 | + foreach ($data->childNodes as $datum) { |
|
363 | + if ($datum->nodeName === 'memsource:tagId') { |
|
364 | + $dataArray['raw-content']['tagId'] = $datum->nodeValue; |
|
365 | 365 | } |
366 | 366 | |
367 | - if ( $datum->nodeName === 'memsource:type' ) { |
|
368 | - $dataArray[ 'raw-content' ][ 'type' ] = $datum->nodeValue; |
|
367 | + if ($datum->nodeName === 'memsource:type') { |
|
368 | + $dataArray['raw-content']['type'] = $datum->nodeValue; |
|
369 | 369 | } |
370 | 370 | } |
371 | 371 | |
372 | 372 | } |
373 | 373 | |
374 | - if ( !empty( $dataArray ) ) { |
|
374 | + if (!empty($dataArray)) { |
|
375 | 375 | $additionalTagData[] = $dataArray; |
376 | 376 | } |
377 | 377 | } |
@@ -387,15 +387,15 @@ discard block |
||
387 | 387 | * @param DOMElement $segment |
388 | 388 | * @param array $attr |
389 | 389 | */ |
390 | - private function checkSegmentIdConsistency( DOMElement $segment, array $attr ) { |
|
391 | - if ( isset( $attr[ 'tGroupBegin' ] ) && isset( $attr[ 'tGroupEnd' ] ) && $segment->attributes->getNamedItem( 'id' ) ) { |
|
392 | - $id = $segment->attributes->getNamedItem( 'id' )->nodeValue; |
|
393 | - $min = (int)$attr[ 'tGroupBegin' ]; |
|
394 | - $max = (int)$attr[ 'tGroupEnd' ]; |
|
395 | - |
|
396 | - if ( false === ( ( $min <= $id ) && ( $id <= $max ) ) ) { |
|
397 | - if ( $this->logger ) { |
|
398 | - $this->logger->warning( 'Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd' ); |
|
390 | + private function checkSegmentIdConsistency(DOMElement $segment, array $attr) { |
|
391 | + if (isset($attr['tGroupBegin']) && isset($attr['tGroupEnd']) && $segment->attributes->getNamedItem('id')) { |
|
392 | + $id = $segment->attributes->getNamedItem('id')->nodeValue; |
|
393 | + $min = (int)$attr['tGroupBegin']; |
|
394 | + $max = (int)$attr['tGroupEnd']; |
|
395 | + |
|
396 | + if (false === (($min <= $id) && ($id <= $max))) { |
|
397 | + if ($this->logger) { |
|
398 | + $this->logger->warning('Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd'); |
|
399 | 399 | } |
400 | 400 | } |
401 | 401 | } |
@@ -407,31 +407,31 @@ discard block |
||
407 | 407 | * @return array |
408 | 408 | * @throws \Exception |
409 | 409 | */ |
410 | - private function extractTransUnitNotes( DOMElement $transUnit ) { |
|
410 | + private function extractTransUnitNotes(DOMElement $transUnit) { |
|
411 | 411 | $notes = []; |
412 | 412 | |
413 | 413 | // loop <notes> to get nested <note> tag |
414 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
415 | - if ( $childNode->nodeName == 'notes' ) { |
|
416 | - foreach ( $childNode->childNodes as $note ) { |
|
417 | - $noteValue = trim( $note->nodeValue ); |
|
418 | - if ( '' !== $noteValue ) { |
|
419 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
414 | + foreach ($transUnit->childNodes as $childNode) { |
|
415 | + if ($childNode->nodeName == 'notes') { |
|
416 | + foreach ($childNode->childNodes as $note) { |
|
417 | + $noteValue = trim($note->nodeValue); |
|
418 | + if ('' !== $noteValue) { |
|
419 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
420 | 420 | } |
421 | 421 | } |
422 | 422 | } |
423 | 423 | |
424 | - if ( $childNode->nodeName === 'mda:metadata' ) { |
|
425 | - foreach ( $childNode->childNodes as $metadata ) { |
|
426 | - if ( $metadata->nodeName === 'mda:metaGroup' ) { |
|
427 | - foreach ( $metadata->childNodes as $meta ) { |
|
428 | - if ( null !== $meta->attributes && null !== $meta->attributes->getNamedItem( 'type' ) ) { |
|
429 | - $type = $meta->attributes->getNamedItem( 'type' )->nodeValue; |
|
430 | - $metaValue = trim( $meta->nodeValue ); |
|
424 | + if ($childNode->nodeName === 'mda:metadata') { |
|
425 | + foreach ($childNode->childNodes as $metadata) { |
|
426 | + if ($metadata->nodeName === 'mda:metaGroup') { |
|
427 | + foreach ($metadata->childNodes as $meta) { |
|
428 | + if (null !== $meta->attributes && null !== $meta->attributes->getNamedItem('type')) { |
|
429 | + $type = $meta->attributes->getNamedItem('type')->nodeValue; |
|
430 | + $metaValue = trim($meta->nodeValue); |
|
431 | 431 | |
432 | - if ( '' !== $metaValue ) { |
|
432 | + if ('' !== $metaValue) { |
|
433 | 433 | $notes[] = array_merge( |
434 | - $this->JSONOrRawContentArray( $metaValue ), |
|
434 | + $this->JSONOrRawContentArray($metaValue), |
|
435 | 435 | [ |
436 | 436 | 'attr' => [ |
437 | 437 | 'type' => $type |
@@ -17,8 +17,8 @@ discard block |
||
17 | 17 | * @return string |
18 | 18 | * @throws Exception |
19 | 19 | */ |
20 | - public static function cleanCDATA( $testString ) { |
|
21 | - $cleanXMLContent = new SimpleXMLElement( '<rootNoteNode>' . $testString . '</rootNoteNode>', LIBXML_NOCDATA ); |
|
20 | + public static function cleanCDATA($testString) { |
|
21 | + $cleanXMLContent = new SimpleXMLElement('<rootNoteNode>' . $testString . '</rootNoteNode>', LIBXML_NOCDATA); |
|
22 | 22 | |
23 | 23 | return $cleanXMLContent->__toString(); |
24 | 24 | } |
@@ -28,28 +28,28 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @return bool |
30 | 30 | */ |
31 | - public static function isJSON( $string ) { |
|
32 | - if ( is_numeric( $string ) ) { |
|
31 | + public static function isJSON($string) { |
|
32 | + if (is_numeric($string)) { |
|
33 | 33 | return false; |
34 | 34 | } |
35 | 35 | |
36 | 36 | try { |
37 | - $string = Strings::cleanCDATA( $string ); |
|
38 | - } catch ( Exception $e ) { |
|
37 | + $string = Strings::cleanCDATA($string); |
|
38 | + } catch (Exception $e) { |
|
39 | 39 | return false; |
40 | 40 | } |
41 | 41 | |
42 | - $string = trim( $string ); |
|
43 | - if ( empty( $string ) ) { |
|
42 | + $string = trim($string); |
|
43 | + if (empty($string)) { |
|
44 | 44 | return false; |
45 | 45 | } |
46 | 46 | |
47 | 47 | // String representation in json is "quoted", but we want to accept only object or arrays. |
48 | 48 | // exclude strings and numbers and other primitive types |
49 | - if ( in_array( $string [ 0 ], [ "{", "[" ] ) ) { |
|
50 | - json_decode( $string ); |
|
49 | + if (in_array($string [0], ["{", "["])) { |
|
50 | + json_decode($string); |
|
51 | 51 | |
52 | - return empty( self::getLastJsonError()[ 0 ] ); |
|
52 | + return empty(self::getLastJsonError()[0]); |
|
53 | 53 | } else { |
54 | 54 | return false; // Not accepted: string or primitive types. |
55 | 55 | } |
@@ -61,10 +61,10 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return array |
63 | 63 | */ |
64 | - public static function jsonToArray( $string ) { |
|
65 | - $decodedJSON = json_decode( $string, true ); |
|
64 | + public static function jsonToArray($string) { |
|
65 | + $decodedJSON = json_decode($string, true); |
|
66 | 66 | |
67 | - return ( is_array( $decodedJSON ) ) ? $decodedJSON : []; |
|
67 | + return (is_array($decodedJSON)) ? $decodedJSON : []; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -75,10 +75,10 @@ discard block |
||
75 | 75 | */ |
76 | 76 | private static function raiseLastJsonException() { |
77 | 77 | |
78 | - list( $msg, $error ) = self::getLastJsonError(); |
|
78 | + list($msg, $error) = self::getLastJsonError(); |
|
79 | 79 | |
80 | - if ( $error != JSON_ERROR_NONE ) { |
|
81 | - throw new NotValidJSONException( $msg, $error ); |
|
80 | + if ($error != JSON_ERROR_NONE) { |
|
81 | + throw new NotValidJSONException($msg, $error); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | } |
@@ -88,11 +88,11 @@ discard block |
||
88 | 88 | */ |
89 | 89 | private static function getLastJsonError() { |
90 | 90 | |
91 | - if ( function_exists( "json_last_error" ) ) { |
|
91 | + if (function_exists("json_last_error")) { |
|
92 | 92 | |
93 | 93 | $error = json_last_error(); |
94 | 94 | |
95 | - switch ( $error ) { |
|
95 | + switch ($error) { |
|
96 | 96 | case JSON_ERROR_NONE: |
97 | 97 | $msg = null; # - No errors |
98 | 98 | break; |
@@ -116,10 +116,10 @@ discard block |
||
116 | 116 | break; |
117 | 117 | } |
118 | 118 | |
119 | - return [ $msg, $error ]; |
|
119 | + return [$msg, $error]; |
|
120 | 120 | } |
121 | 121 | |
122 | - return [ null, JSON_ERROR_NONE ]; |
|
122 | + return [null, JSON_ERROR_NONE]; |
|
123 | 123 | |
124 | 124 | } |
125 | 125 | |
@@ -142,11 +142,11 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @return string |
144 | 144 | */ |
145 | - public static function fixNonWellFormedXml( $content, $escapeStrings = true ) { |
|
146 | - if ( self::$find_xliff_tags_reg === null ) { |
|
145 | + public static function fixNonWellFormedXml($content, $escapeStrings = true) { |
|
146 | + if (self::$find_xliff_tags_reg === null) { |
|
147 | 147 | // Convert the list of tags in a regexp list, for example "g|x|bx|ex" |
148 | 148 | $xliffTags = XliffTags::$tags; |
149 | - $xliff_tags_reg_list = implode( '|', $xliffTags ); |
|
149 | + $xliff_tags_reg_list = implode('|', $xliffTags); |
|
150 | 150 | // Regexp to find all the XLIFF tags: |
151 | 151 | // </? -> matches the tag start, for both opening and |
152 | 152 | // closure tags (see the optional slash) |
@@ -167,30 +167,30 @@ discard block |
||
167 | 167 | } |
168 | 168 | |
169 | 169 | // Find all the XLIFF tags |
170 | - preg_match_all( self::$find_xliff_tags_reg, $content, $matches ); |
|
171 | - $tags = (array)$matches[ 0 ]; |
|
170 | + preg_match_all(self::$find_xliff_tags_reg, $content, $matches); |
|
171 | + $tags = (array)$matches[0]; |
|
172 | 172 | |
173 | 173 | // Prepare placeholders |
174 | 174 | $tags_placeholders = []; |
175 | - $tagsNum = count( $tags ); |
|
176 | - for ( $i = 0; $i < $tagsNum; $i++ ) { |
|
177 | - $tag = $tags[ $i ]; |
|
178 | - $tags_placeholders[ $tag ] = "#@!XLIFF-TAG-$i!@#"; |
|
175 | + $tagsNum = count($tags); |
|
176 | + for ($i = 0; $i < $tagsNum; $i++) { |
|
177 | + $tag = $tags[$i]; |
|
178 | + $tags_placeholders[$tag] = "#@!XLIFF-TAG-$i!@#"; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | // Replace all XLIFF tags with placeholders that will not be escaped |
182 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
183 | - $content = str_replace( $tag, $placeholder, $content ); |
|
182 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
183 | + $content = str_replace($tag, $placeholder, $content); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Escape the string with the remaining non-XLIFF tags |
187 | - if ( $escapeStrings ) { |
|
188 | - $content = htmlspecialchars( $content, ENT_NOQUOTES, 'UTF-8', false ); |
|
187 | + if ($escapeStrings) { |
|
188 | + $content = htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8', false); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | // Put again in place the original XLIFF tags replacing placeholders |
192 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
193 | - $content = str_replace( $placeholder, $tag, $content ); |
|
192 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
193 | + $content = str_replace($placeholder, $tag, $content); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | return $content; |
@@ -201,17 +201,17 @@ discard block |
||
201 | 201 | * |
202 | 202 | * @return string |
203 | 203 | */ |
204 | - public static function removeDangerousChars( $string ) { |
|
204 | + public static function removeDangerousChars($string) { |
|
205 | 205 | // clean invalid xml entities ( characters with ascii < 32 and different from 0A, 0D and 09 |
206 | 206 | $regexpEntity = '/&#x(0[0-8BCEF]|1[\dA-F]|7F);/u'; |
207 | 207 | |
208 | 208 | // remove binary chars in some xliff files |
209 | 209 | $regexpAscii = '/[\x{00}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}\x{7F}]/u'; |
210 | 210 | |
211 | - $string = preg_replace( $regexpAscii, '', $string ); |
|
212 | - $string = preg_replace( $regexpEntity, '', $string ); |
|
211 | + $string = preg_replace($regexpAscii, '', $string); |
|
212 | + $string = preg_replace($regexpEntity, '', $string); |
|
213 | 213 | |
214 | - return !empty( $string ) || strlen( $string ) > 0 ? $string : ""; |
|
214 | + return !empty($string) || strlen($string) > 0 ? $string : ""; |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | /** |
@@ -220,8 +220,8 @@ discard block |
||
220 | 220 | * |
221 | 221 | * @return bool |
222 | 222 | */ |
223 | - public static function contains( $needle, $haystack ) { |
|
224 | - return mb_strpos( $haystack, $needle ) !== false; |
|
223 | + public static function contains($needle, $haystack) { |
|
224 | + return mb_strpos($haystack, $needle) !== false; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | /** |
@@ -229,8 +229,8 @@ discard block |
||
229 | 229 | * |
230 | 230 | * @return string |
231 | 231 | */ |
232 | - public static function htmlentities( $string ) { |
|
233 | - return htmlentities( $string, ENT_NOQUOTES ); |
|
232 | + public static function htmlentities($string) { |
|
233 | + return htmlentities($string, ENT_NOQUOTES); |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -239,15 +239,15 @@ discard block |
||
239 | 239 | * |
240 | 240 | * @return string |
241 | 241 | */ |
242 | - public static function htmlspecialchars_decode( $string, $onlyEscapedEntities = false ) { |
|
243 | - if ( false === $onlyEscapedEntities ) { |
|
244 | - return htmlspecialchars_decode( $string, ENT_NOQUOTES ); |
|
242 | + public static function htmlspecialchars_decode($string, $onlyEscapedEntities = false) { |
|
243 | + if (false === $onlyEscapedEntities) { |
|
244 | + return htmlspecialchars_decode($string, ENT_NOQUOTES); |
|
245 | 245 | } |
246 | 246 | |
247 | - return preg_replace_callback( self::$htmlEntityRegex, |
|
248 | - function ( $match ) { |
|
249 | - return self::htmlspecialchars_decode( $match[ 0 ] ); |
|
250 | - }, $string ); |
|
247 | + return preg_replace_callback(self::$htmlEntityRegex, |
|
248 | + function($match) { |
|
249 | + return self::htmlspecialchars_decode($match[0]); |
|
250 | + }, $string); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | /** |
@@ -262,8 +262,8 @@ discard block |
||
262 | 262 | * |
263 | 263 | * @return bool |
264 | 264 | */ |
265 | - public static function isADoubleEscapedEntity( $str ) { |
|
266 | - return preg_match( self::$htmlEntityRegex, $str ) != 0; |
|
265 | + public static function isADoubleEscapedEntity($str) { |
|
266 | + return preg_match(self::$htmlEntityRegex, $str) != 0; |
|
267 | 267 | } |
268 | 268 | |
269 | 269 | /** |
@@ -271,8 +271,8 @@ discard block |
||
271 | 271 | * |
272 | 272 | * @return bool |
273 | 273 | */ |
274 | - public static function isAnEscapedHTML( $str ) { |
|
275 | - return preg_match( '#/[a-z]*>#i', $str ) != 0; |
|
274 | + public static function isAnEscapedHTML($str) { |
|
275 | + return preg_match('#/[a-z]*>#i', $str) != 0; |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | /** |
@@ -280,8 +280,8 @@ discard block |
||
280 | 280 | * |
281 | 281 | * @return bool |
282 | 282 | */ |
283 | - public static function isAValidUuid( $uuid ) { |
|
284 | - return preg_match( '/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid ) === 1; |
|
283 | + public static function isAValidUuid($uuid) { |
|
284 | + return preg_match('/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid) === 1; |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | /** |
@@ -290,8 +290,8 @@ discard block |
||
290 | 290 | * |
291 | 291 | * @return array|false|string[] |
292 | 292 | */ |
293 | - public static function preg_split( $pattern, $subject ) { |
|
294 | - return preg_split( $pattern, $subject, -1, PREG_SPLIT_NO_EMPTY ); |
|
293 | + public static function preg_split($pattern, $subject) { |
|
294 | + return preg_split($pattern, $subject, -1, PREG_SPLIT_NO_EMPTY); |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | /** |
@@ -309,8 +309,8 @@ discard block |
||
309 | 309 | * |
310 | 310 | * @return string |
311 | 311 | */ |
312 | - public static function escapeOnlyHTMLTags( $string ) { |
|
313 | - return preg_replace( '/<(.*?)>/iu', '<$1>', $string ); |
|
312 | + public static function escapeOnlyHTMLTags($string) { |
|
313 | + return preg_replace('/<(.*?)>/iu', '<$1>', $string); |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | /** |
@@ -320,8 +320,8 @@ discard block |
||
320 | 320 | * |
321 | 321 | * @return string |
322 | 322 | */ |
323 | - public static function lastChar( $string ) { |
|
324 | - return mb_substr( $string, -1 ); |
|
323 | + public static function lastChar($string) { |
|
324 | + return mb_substr($string, -1); |
|
325 | 325 | } |
326 | 326 | |
327 | 327 | /** |
@@ -329,8 +329,8 @@ discard block |
||
329 | 329 | * |
330 | 330 | * @return int |
331 | 331 | */ |
332 | - public static function getTheNumberOfTrailingSpaces( $segment ) { |
|
333 | - return mb_strlen( $segment ) - mb_strlen( rtrim( $segment, ' ' ) ); |
|
332 | + public static function getTheNumberOfTrailingSpaces($segment) { |
|
333 | + return mb_strlen($segment) - mb_strlen(rtrim($segment, ' ')); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | /** |
@@ -340,15 +340,15 @@ discard block |
||
340 | 340 | * |
341 | 341 | * @return bool |
342 | 342 | */ |
343 | - public static function isHtmlString( $string ) { |
|
344 | - $string = stripslashes( $string ); |
|
343 | + public static function isHtmlString($string) { |
|
344 | + $string = stripslashes($string); |
|
345 | 345 | |
346 | - if ( $string === '<>' ) { |
|
346 | + if ($string === '<>') { |
|
347 | 347 | return false; |
348 | 348 | } |
349 | 349 | |
350 | - preg_match( "#</?[a-zA-Z1-6-]+((\s+[a-zA-Z1-6-]+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/?>#", $string, $matches ); |
|
350 | + preg_match("#</?[a-zA-Z1-6-]+((\s+[a-zA-Z1-6-]+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/?>#", $string, $matches); |
|
351 | 351 | |
352 | - return count( $matches ) !== 0; |
|
352 | + return count($matches) !== 0; |
|
353 | 353 | } |
354 | 354 | } |
@@ -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 | } |
@@ -16,33 +16,33 @@ discard block |
||
16 | 16 | * @inheritDoc |
17 | 17 | * @throws Exception |
18 | 18 | */ |
19 | - public function parse( DOMDocument $dom, $output = [] ) { |
|
19 | + public function parse(DOMDocument $dom, $output = []) { |
|
20 | 20 | $i = 1; |
21 | 21 | /** @var DOMElement $file */ |
22 | - foreach ( $dom->getElementsByTagName( 'file' ) as $file ) { |
|
22 | + foreach ($dom->getElementsByTagName('file') as $file) { |
|
23 | 23 | |
24 | 24 | // metadata |
25 | - $output[ 'files' ][ $i ][ 'attr' ] = $this->extractMetadata( $file ); |
|
25 | + $output['files'][$i]['attr'] = $this->extractMetadata($file); |
|
26 | 26 | |
27 | 27 | // reference |
28 | - if ( !empty( $this->extractReference( $file ) ) ) { |
|
29 | - $output[ 'files' ][ $i ][ 'reference' ] = $this->extractReference( $file ); |
|
28 | + if (!empty($this->extractReference($file))) { |
|
29 | + $output['files'][$i]['reference'] = $this->extractReference($file); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | // trans-units |
33 | 33 | $transUnitIdArrayForUniquenessCheck = []; |
34 | 34 | $j = 1; |
35 | - foreach ( $file->childNodes as $body ) { |
|
36 | - if ( $body->nodeName === 'body' ) { |
|
37 | - foreach ( $body->childNodes as $childNode ) { |
|
38 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
35 | + foreach ($file->childNodes as $body) { |
|
36 | + if ($body->nodeName === 'body') { |
|
37 | + foreach ($body->childNodes as $childNode) { |
|
38 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | // trans-unit re-count check |
42 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
43 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
44 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
45 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
42 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
43 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
44 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
45 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | $i++; |
@@ -58,50 +58,50 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return array |
60 | 60 | */ |
61 | - private function extractMetadata( DOMElement $file ) { |
|
61 | + private function extractMetadata(DOMElement $file) { |
|
62 | 62 | $metadata = []; |
63 | 63 | $customAttr = []; |
64 | 64 | |
65 | 65 | /** @var DOMAttr $attribute */ |
66 | - foreach ( $file->attributes as $attribute ) { |
|
67 | - switch ( $attribute->localName ) { |
|
66 | + foreach ($file->attributes as $attribute) { |
|
67 | + switch ($attribute->localName) { |
|
68 | 68 | // original |
69 | 69 | case 'original': |
70 | - $metadata[ 'original' ] = $attribute->value; |
|
70 | + $metadata['original'] = $attribute->value; |
|
71 | 71 | break; |
72 | 72 | |
73 | 73 | // source-language |
74 | 74 | case 'source-language': |
75 | - $metadata[ 'source-language' ] = $attribute->value; |
|
75 | + $metadata['source-language'] = $attribute->value; |
|
76 | 76 | break; |
77 | 77 | |
78 | 78 | // data-type |
79 | 79 | case 'datatype': |
80 | - $metadata[ 'data-type' ] = $attribute->value; |
|
80 | + $metadata['data-type'] = $attribute->value; |
|
81 | 81 | break; |
82 | 82 | |
83 | 83 | // target-language |
84 | 84 | case 'target-language': |
85 | - $metadata[ 'target-language' ] = $attribute->value; |
|
85 | + $metadata['target-language'] = $attribute->value; |
|
86 | 86 | break; |
87 | 87 | } |
88 | 88 | |
89 | 89 | // Custom MateCat x-Attribute |
90 | - preg_match( '|x-(.*?)|si', $attribute->localName, $temp ); |
|
91 | - if ( isset( $temp[ 1 ] ) ) { |
|
92 | - $customAttr[ $attribute->localName ] = $attribute->value; |
|
90 | + preg_match('|x-(.*?)|si', $attribute->localName, $temp); |
|
91 | + if (isset($temp[1])) { |
|
92 | + $customAttr[$attribute->localName] = $attribute->value; |
|
93 | 93 | } |
94 | - unset( $temp ); |
|
94 | + unset($temp); |
|
95 | 95 | |
96 | 96 | // Custom MateCat namespace Attribute mtc: |
97 | - preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp ); |
|
98 | - if ( isset( $temp[ 1 ] ) ) { |
|
99 | - $customAttr[ $attribute->nodeName ] = $attribute->value; |
|
97 | + preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp); |
|
98 | + if (isset($temp[1])) { |
|
99 | + $customAttr[$attribute->nodeName] = $attribute->value; |
|
100 | 100 | } |
101 | - unset( $temp ); |
|
101 | + unset($temp); |
|
102 | 102 | |
103 | - if ( !empty( $customAttr ) ) { |
|
104 | - $metadata[ 'custom' ] = $customAttr; |
|
103 | + if (!empty($customAttr)) { |
|
104 | + $metadata['custom'] = $customAttr; |
|
105 | 105 | } |
106 | 106 | } |
107 | 107 | |
@@ -113,16 +113,16 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @return array |
115 | 115 | */ |
116 | - private function extractReference( DOMElement $file ) { |
|
116 | + private function extractReference(DOMElement $file) { |
|
117 | 117 | $reference = []; |
118 | 118 | |
119 | 119 | $order = 0; |
120 | - foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) { |
|
120 | + foreach ($file->getElementsByTagName('reference') as $ref) { |
|
121 | 121 | /** @var DOMNode $childNode */ |
122 | - foreach ( $ref->childNodes as $childNode ) { |
|
123 | - if ( $childNode->nodeName === 'internal-file' ) { |
|
124 | - $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue; |
|
125 | - $reference[ $order ][ 'base64' ] = trim( $childNode->nodeValue ); |
|
122 | + foreach ($ref->childNodes as $childNode) { |
|
123 | + if ($childNode->nodeName === 'internal-file') { |
|
124 | + $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue; |
|
125 | + $reference[$order]['base64'] = trim($childNode->nodeValue); |
|
126 | 126 | $order++; |
127 | 127 | } |
128 | 128 | } |
@@ -143,54 +143,54 @@ discard block |
||
143 | 143 | * |
144 | 144 | * @throws Exception |
145 | 145 | */ |
146 | - protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ) { |
|
146 | + protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j) { |
|
147 | 147 | // metadata |
148 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
148 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
149 | 149 | |
150 | 150 | // notes |
151 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit ); |
|
151 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit); |
|
152 | 152 | |
153 | 153 | // content |
154 | 154 | /** @var DOMElement $childNode */ |
155 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
155 | + foreach ($transUnit->childNodes as $childNode) { |
|
156 | 156 | // source |
157 | - if ( $childNode->nodeName === 'source' ) { |
|
158 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode ); |
|
157 | + if ($childNode->nodeName === 'source') { |
|
158 | + $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | // seg-source |
162 | - if ( $childNode->nodeName === 'seg-source' ) { |
|
163 | - $rawSegment = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ]; |
|
164 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment ); |
|
162 | + if ($childNode->nodeName === 'seg-source') { |
|
163 | + $rawSegment = $output['files'][$i]['trans-units'][$j]['source']['raw-content']; |
|
164 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | // target |
168 | - if ( $childNode->nodeName === 'target' ) { |
|
169 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode ); |
|
168 | + if ($childNode->nodeName === 'target') { |
|
169 | + $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode); |
|
170 | 170 | |
171 | 171 | // seg-target |
172 | - $targetRawContent = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ]; |
|
173 | - $segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ]; |
|
174 | - if ( isset( $targetRawContent ) && !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) { |
|
175 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $targetRawContent ); |
|
176 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ]['attr'] = $this->extractTagAttributes($childNode); |
|
172 | + $targetRawContent = @$output['files'][$i]['trans-units'][$j]['target']['raw-content']; |
|
173 | + $segSource = @$output['files'][$i]['trans-units'][$j]['seg-source']; |
|
174 | + if (isset($targetRawContent) && !empty($targetRawContent) && isset($segSource) && count($segSource) > 0) { |
|
175 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $targetRawContent); |
|
176 | + $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
180 | 180 | // locked |
181 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
182 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
181 | + if ($childNode->nodeName === 'sdl:seg') { |
|
182 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | 186 | // context-group |
187 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
188 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
187 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
188 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | // alt-trans |
192 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
193 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
192 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
193 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | $j++; |
@@ -203,41 +203,41 @@ discard block |
||
203 | 203 | * @return array |
204 | 204 | * @throws Exception |
205 | 205 | */ |
206 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ) { |
|
206 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck) { |
|
207 | 207 | $metadata = []; |
208 | 208 | |
209 | 209 | // id MUST NOT be null |
210 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
211 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
210 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
211 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
215 | 215 | * @var DOMAttr $element |
216 | 216 | */ |
217 | - foreach ( $transUnit->attributes as $element ) { |
|
217 | + foreach ($transUnit->attributes as $element) { |
|
218 | 218 | |
219 | - if ( $element->nodeName === "id" ) { |
|
219 | + if ($element->nodeName === "id") { |
|
220 | 220 | |
221 | 221 | $id = $element->nodeValue; |
222 | 222 | |
223 | - if ( strlen( $id ) > 100 ) { |
|
224 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
223 | + if (strlen($id) > 100) { |
|
224 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | $transUnitIdArrayForUniquenessCheck[] = $id; |
228 | - $metadata[ 'id' ] = $id; |
|
228 | + $metadata['id'] = $id; |
|
229 | 229 | |
230 | - } elseif ( $element->nodeName === "approved" ) { |
|
230 | + } elseif ($element->nodeName === "approved") { |
|
231 | 231 | // approved as BOOLEAN |
232 | 232 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
233 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
234 | - } elseif ( $element->nodeName === "maxwidth" ) { |
|
233 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
234 | + } elseif ($element->nodeName === "maxwidth") { |
|
235 | 235 | // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character |
236 | 236 | // we duplicate the info to allow Xliff V1 and V2 to work the same |
237 | - $metadata[ 'sizeRestriction' ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
238 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
237 | + $metadata['sizeRestriction'] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
238 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
239 | 239 | } else { |
240 | - $metadata[ $element->nodeName ] = $element->nodeValue; |
|
240 | + $metadata[$element->nodeName] = $element->nodeValue; |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | } |
@@ -251,19 +251,19 @@ discard block |
||
251 | 251 | * @return array |
252 | 252 | * @throws Exception |
253 | 253 | */ |
254 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ) { |
|
254 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit) { |
|
255 | 255 | $notes = []; |
256 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
256 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
257 | 257 | |
258 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
258 | + $noteValue = $this->extractTagContent($dom, $note); |
|
259 | 259 | |
260 | - if ( '' !== $noteValue ) { |
|
260 | + if ('' !== $noteValue) { |
|
261 | 261 | |
262 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
262 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
263 | 263 | |
264 | 264 | // extract all the attributes |
265 | - foreach ( $note->attributes as $attribute ) { |
|
266 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
265 | + foreach ($note->attributes as $attribute) { |
|
266 | + $extractedNote[$attribute->name] = $attribute->value; |
|
267 | 267 | } |
268 | 268 | |
269 | 269 | $notes[] = $extractedNote; |
@@ -278,14 +278,14 @@ discard block |
||
278 | 278 | * |
279 | 279 | * @return array |
280 | 280 | */ |
281 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ) { |
|
281 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup) { |
|
282 | 282 | $cg = []; |
283 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
283 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
284 | 284 | |
285 | 285 | /** @var DOMNode $context */ |
286 | - foreach ( $contextGroup->childNodes as $context ) { |
|
287 | - if ( $context->nodeName === 'context' ) { |
|
288 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
286 | + foreach ($contextGroup->childNodes as $context) { |
|
287 | + if ($context->nodeName === 'context') { |
|
288 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
289 | 289 | } |
290 | 290 | } |
291 | 291 | |
@@ -297,16 +297,16 @@ discard block |
||
297 | 297 | * |
298 | 298 | * @return array |
299 | 299 | */ |
300 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
300 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
301 | 301 | $at = []; |
302 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
302 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
303 | 303 | |
304 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
305 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
304 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
305 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
306 | 306 | } |
307 | 307 | |
308 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
309 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
308 | + if ($altTrans->getElementsByTagName('target')) { |
|
309 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
310 | 310 | } |
311 | 311 | |
312 | 312 | return $at; |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | * |
318 | 318 | * @return bool |
319 | 319 | */ |
320 | - private function extractLocked( DOMElement $locked ) { |
|
321 | - return null !== $locked->getAttribute( 'locked' ); |
|
320 | + private function extractLocked(DOMElement $locked) { |
|
321 | + return null !== $locked->getAttribute('locked'); |
|
322 | 322 | } |
323 | 323 | } |
@@ -31,17 +31,17 @@ discard block |
||
31 | 31 | ]; |
32 | 32 | |
33 | 33 | public function replaceTranslation() { |
34 | - fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' ); |
|
34 | + fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>'); |
|
35 | 35 | |
36 | 36 | //create Sax parser |
37 | 37 | $xmlParser = $this->initSaxParser(); |
38 | 38 | |
39 | - while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) { |
|
39 | + while ($this->currentBuffer = fread($this->originalFP, 4096)) { |
|
40 | 40 | /* |
41 | 41 | preprocess file |
42 | 42 | */ |
43 | 43 | // obfuscate entities because sax automatically does html_entity_decode |
44 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
44 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
45 | 45 | |
46 | 46 | //avoid cutting entities in half: |
47 | 47 | //the last fread could have truncated an entity (say, '<' in '&l'), thus invalidating the escaping |
@@ -53,84 +53,84 @@ discard block |
||
53 | 53 | // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end |
54 | 54 | //it can't be an entity, exit the loop |
55 | 55 | |
56 | - while ( true ) { |
|
57 | - $_ampPos = strpos( $temporary_check_buffer, '&' ); |
|
56 | + while (true) { |
|
57 | + $_ampPos = strpos($temporary_check_buffer, '&'); |
|
58 | 58 | |
59 | 59 | //check for real entity or escape it to safely exit from the loop!!! |
60 | - if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) { |
|
60 | + if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) { |
|
61 | 61 | $escape_AMP = true; |
62 | 62 | break; |
63 | 63 | } |
64 | 64 | |
65 | 65 | //if an entity is still present, fetch some more and repeat the escaping |
66 | - $this->currentBuffer .= fread( $this->originalFP, 9 ); |
|
67 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
66 | + $this->currentBuffer .= fread($this->originalFP, 9); |
|
67 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | //free stuff outside the loop |
71 | - unset( $temporary_check_buffer ); |
|
71 | + unset($temporary_check_buffer); |
|
72 | 72 | |
73 | - $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
74 | - if ( $escape_AMP ) { |
|
75 | - $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
73 | + $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
74 | + if ($escape_AMP) { |
|
75 | + $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | //get length of chunk |
79 | - $this->len = strlen( $this->currentBuffer ); |
|
79 | + $this->len = strlen($this->currentBuffer); |
|
80 | 80 | |
81 | 81 | //parse chunk of text |
82 | - if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) { |
|
82 | + if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) { |
|
83 | 83 | //if unable, raise an exception |
84 | - throw new RuntimeException( sprintf( |
|
84 | + throw new RuntimeException(sprintf( |
|
85 | 85 | "XML error: %s at line %d", |
86 | - xml_error_string( xml_get_error_code( $xmlParser ) ), |
|
87 | - xml_get_current_line_number( $xmlParser ) |
|
88 | - ) ); |
|
86 | + xml_error_string(xml_get_error_code($xmlParser)), |
|
87 | + xml_get_current_line_number($xmlParser) |
|
88 | + )); |
|
89 | 89 | } |
90 | 90 | //get accumulated this->offset in document: as long as SAX pointer advances, we keep track of total bytes it has seen so far; this way, we can translate its global pointer in an address local to the current buffer of text to retrieve last char of tag |
91 | 91 | $this->offset += $this->len; |
92 | 92 | } |
93 | 93 | |
94 | 94 | // close Sax parser |
95 | - $this->closeSaxParser( $xmlParser ); |
|
95 | + $this->closeSaxParser($xmlParser); |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * @inheritDoc |
101 | 101 | */ |
102 | - protected function tagOpen( $parser, $name, $attr ) { |
|
102 | + protected function tagOpen($parser, $name, $attr) { |
|
103 | 103 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
104 | - if ( $this->tuTagName === $name ) { |
|
104 | + if ($this->tuTagName === $name) { |
|
105 | 105 | $this->inTU = true; |
106 | 106 | |
107 | 107 | // get id |
108 | 108 | // trim to first 100 characters because this is the limit on Matecat's DB |
109 | - $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
|
109 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
110 | 110 | |
111 | 111 | // `translate` attribute can be only yes or no |
112 | - if ( isset( $attr[ 'translate' ] ) && $attr[ 'translate' ] === 'no' ) { |
|
113 | - $attr[ 'translate' ] = 'no'; |
|
112 | + if (isset($attr['translate']) && $attr['translate'] === 'no') { |
|
113 | + $attr['translate'] = 'no'; |
|
114 | 114 | } else { |
115 | - $attr[ 'translate' ] = 'yes'; |
|
115 | + $attr['translate'] = 'yes'; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | // current 'translate' attribute of the current trans-unit |
119 | - $this->currentTransUnitTranslate = $attr[ 'translate' ]; |
|
119 | + $this->currentTransUnitTranslate = $attr['translate']; |
|
120 | 120 | } |
121 | 121 | |
122 | - if ( 'source' === $name ) { |
|
122 | + if ('source' === $name) { |
|
123 | 123 | $this->sourceAttributes = $attr; |
124 | 124 | } |
125 | 125 | |
126 | - if ( 'mda:metadata' === $name ) { |
|
126 | + if ('mda:metadata' === $name) { |
|
127 | 127 | $this->unitContainsMda = true; |
128 | 128 | } |
129 | 129 | |
130 | 130 | // check if we are entering into a <target> |
131 | - if ( 'target' === $name ) { |
|
131 | + if ('target' === $name) { |
|
132 | 132 | |
133 | - if ( $this->currentTransUnitTranslate === 'no' ) { |
|
133 | + if ($this->currentTransUnitTranslate === 'no') { |
|
134 | 134 | $this->inTarget = false; |
135 | 135 | } else { |
136 | 136 | $this->inTarget = true; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
141 | 141 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
142 | - if ( !$this->inTarget ) { |
|
142 | + if (!$this->inTarget) { |
|
143 | 143 | |
144 | 144 | $tag = ''; |
145 | 145 | |
@@ -161,8 +161,8 @@ discard block |
||
161 | 161 | // |
162 | 162 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
163 | 163 | // |
164 | - if ( $this->xliffVersion === 2 && ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) && $this->unitContainsMda === false ) { |
|
165 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) && !empty( $this->transUnits[ $this->currentTransUnitId ] ) && !$this->hasWrittenCounts ) { |
|
164 | + if ($this->xliffVersion === 2 && ($name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable') && $this->unitContainsMda === false) { |
|
165 | + if (isset($this->transUnits[$this->currentTransUnitId]) && !empty($this->transUnits[$this->currentTransUnitId]) && !$this->hasWrittenCounts) { |
|
166 | 166 | |
167 | 167 | // we need to update counts here |
168 | 168 | $this->updateCounts(); |
@@ -179,90 +179,90 @@ discard block |
||
179 | 179 | $lastMrkState = null; |
180 | 180 | $stateProp = ''; |
181 | 181 | |
182 | - foreach ( $attr as $k => $v ) { |
|
182 | + foreach ($attr as $k => $v) { |
|
183 | 183 | |
184 | 184 | //if tag name is file, we must replace the target-language attribute |
185 | - if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
|
185 | + if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) { |
|
186 | 186 | //replace Target language with job language provided from constructor |
187 | 187 | $tag .= "$k=\"$this->targetLang\" "; |
188 | 188 | } else { |
189 | 189 | $pos = 0; |
190 | - if ( $this->currentTransUnitId and isset($this->transUnits[ $this->currentTransUnitId ])) { |
|
191 | - $pos = current( $this->transUnits[ $this->currentTransUnitId ] ); |
|
190 | + if ($this->currentTransUnitId and isset($this->transUnits[$this->currentTransUnitId])) { |
|
191 | + $pos = current($this->transUnits[$this->currentTransUnitId]); |
|
192 | 192 | } |
193 | 193 | |
194 | - if ( $name === $this->tuTagName and isset($this->segments[ $pos ]) and isset($this->segments[ $pos ][ 'sid' ]) ) { |
|
194 | + if ($name === $this->tuTagName and isset($this->segments[$pos]) and isset($this->segments[$pos]['sid'])) { |
|
195 | 195 | |
196 | - $sid = $this->segments[ $pos ][ 'sid' ]; |
|
196 | + $sid = $this->segments[$pos]['sid']; |
|
197 | 197 | |
198 | 198 | // add `help-id` to xliff v.1* |
199 | 199 | // add `mtc:segment-id` to xliff v.2* |
200 | - if ( $this->xliffVersion === 1 && strpos( $tag, 'help-id' ) === false ) { |
|
201 | - if ( !empty( $sid ) ) { |
|
200 | + if ($this->xliffVersion === 1 && strpos($tag, 'help-id') === false) { |
|
201 | + if (!empty($sid)) { |
|
202 | 202 | $tag .= "help-id=\"$sid\" "; |
203 | 203 | } |
204 | - } elseif ( $this->xliffVersion === 2 && strpos( $tag, 'mtc:segment-id' ) === false ) { |
|
205 | - if ( !empty( $sid ) ) { |
|
204 | + } elseif ($this->xliffVersion === 2 && strpos($tag, 'mtc:segment-id') === false) { |
|
205 | + if (!empty($sid)) { |
|
206 | 206 | $tag .= "mtc:segment-id=\"$sid\" "; |
207 | 207 | } |
208 | 208 | } |
209 | 209 | |
210 | - } elseif ( 'segment' === $name && $this->xliffVersion === 2 ) { // add state to segment in Xliff v2 |
|
211 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $this->segments[ $pos ], $stateProp, $lastMrkState ); |
|
212 | - } elseif ( 'target' === $name && $this->xliffVersion === 1 ) { // add state to target in Xliff v1 |
|
213 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $this->segments[ $pos ], $stateProp, $lastMrkState ); |
|
210 | + } elseif ('segment' === $name && $this->xliffVersion === 2) { // add state to segment in Xliff v2 |
|
211 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($this->segments[$pos], $stateProp, $lastMrkState); |
|
212 | + } elseif ('target' === $name && $this->xliffVersion === 1) { // add state to target in Xliff v1 |
|
213 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($this->segments[$pos], $stateProp, $lastMrkState); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | //normal tag flux, put attributes in it |
217 | 217 | $tag .= "$k=\"$v\" "; |
218 | 218 | |
219 | 219 | // replace state for xliff v2 |
220 | - if ( $stateProp ) { |
|
220 | + if ($stateProp) { |
|
221 | 221 | $pattern = '/state=\"(.*)\"/i'; |
222 | - $tag = preg_replace( $pattern, $stateProp, $tag ); |
|
222 | + $tag = preg_replace($pattern, $stateProp, $tag); |
|
223 | 223 | } |
224 | 224 | } |
225 | 225 | } |
226 | 226 | |
227 | 227 | // add oasis xliff 20 namespace |
228 | - if ( $this->xliffVersion === 2 && $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
|
228 | + if ($this->xliffVersion === 2 && $name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) { |
|
229 | 229 | $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
230 | 230 | } |
231 | 231 | |
232 | 232 | // add MateCat specific namespace, we want maybe add non-XLIFF attributes |
233 | - if ( $name === 'xliff' && !array_key_exists( 'xmlns:mtc', $attr ) ) { |
|
233 | + if ($name === 'xliff' && !array_key_exists('xmlns:mtc', $attr)) { |
|
234 | 234 | $tag .= ' xmlns:mtc="https://www.matecat.com" '; |
235 | 235 | } |
236 | 236 | |
237 | 237 | // trgLang |
238 | - if ( $name === 'xliff' ) { |
|
239 | - $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag ); |
|
238 | + if ($name === 'xliff') { |
|
239 | + $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | //this logic helps detecting empty tags |
243 | 243 | //get current position of SAX pointer in all the stream of data is has read so far: |
244 | 244 | //it points at the end of current tag |
245 | - $idx = xml_get_current_byte_index( $parser ); |
|
245 | + $idx = xml_get_current_byte_index($parser); |
|
246 | 246 | |
247 | 247 | //check whether the bounds of current tag are entirely in current buffer or the end of the current tag |
248 | 248 | //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
249 | 249 | //this check is necessary because we may have truncated a tag in half with current read, |
250 | 250 | //and the other half may be encountered in the next buffer it will be passed |
251 | - if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) { |
|
251 | + if (isset($this->currentBuffer[$idx - $this->offset])) { |
|
252 | 252 | //if this tag entire lenght fitted in the buffer, the last char must be the last |
253 | 253 | //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
254 | - $lastChar = $this->currentBuffer[ $idx - $this->offset ]; |
|
254 | + $lastChar = $this->currentBuffer[$idx - $this->offset]; |
|
255 | 255 | } else { |
256 | 256 | //if it's out, simple use the last character of the chunk |
257 | - $lastChar = $this->currentBuffer[ $this->len - 1 ]; |
|
257 | + $lastChar = $this->currentBuffer[$this->len - 1]; |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | //trim last space |
261 | - $tag = rtrim( $tag ); |
|
261 | + $tag = rtrim($tag); |
|
262 | 262 | |
263 | 263 | //detect empty tag |
264 | - $this->isEmpty = ( $lastChar == '/' || $name == 'x' ); |
|
265 | - if ( $this->isEmpty ) { |
|
264 | + $this->isEmpty = ($lastChar == '/' || $name == 'x'); |
|
265 | + if ($this->isEmpty) { |
|
266 | 266 | $tag .= '/'; |
267 | 267 | } |
268 | 268 | |
@@ -270,30 +270,30 @@ discard block |
||
270 | 270 | $tag .= ">"; |
271 | 271 | |
272 | 272 | //set a a Buffer for the segSource Source tag |
273 | - if ( $this->bufferIsActive || in_array( $name, $this->nodesToCopy ) ) { // we are opening a critical CDATA section |
|
273 | + if ($this->bufferIsActive || in_array($name, $this->nodesToCopy)) { // we are opening a critical CDATA section |
|
274 | 274 | |
275 | 275 | //WARNING BECAUSE SOURCE AND SEG-SOURCE TAGS CAN BE EMPTY IN SOME CASES!!!!! |
276 | 276 | //so check for isEmpty also in conjunction with name |
277 | - if ( $this->isEmpty && ( 'source' === $name || 'seg-source' === $name ) ) { |
|
278 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
277 | + if ($this->isEmpty && ('source' === $name || 'seg-source' === $name)) { |
|
278 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
279 | 279 | } else { |
280 | 280 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
281 | 281 | $this->bufferIsActive = true; |
282 | - $this->CDATABuffer .= $tag; |
|
282 | + $this->CDATABuffer .= $tag; |
|
283 | 283 | } |
284 | 284 | } else { |
285 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
285 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
286 | 286 | } |
287 | 287 | } |
288 | 288 | |
289 | 289 | // update segmentPositionInTu |
290 | 290 | |
291 | - if ( $this->xliffVersion === 1 && $this->inTU && $name === 'source' ) { |
|
291 | + if ($this->xliffVersion === 1 && $this->inTU && $name === 'source') { |
|
292 | 292 | $asdasdsa = $attr; |
293 | 293 | $this->segmentPositionInTu++; |
294 | 294 | } |
295 | 295 | |
296 | - if ( $this->xliffVersion === 2 && $this->inTU && $name === 'segment' ) { |
|
296 | + if ($this->xliffVersion === 2 && $this->inTU && $name === 'segment') { |
|
297 | 297 | $asdasdsa = $attr; |
298 | 298 | $this->segmentPositionInTu++; |
299 | 299 | } |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | /** |
303 | 303 | * @inheritDoc |
304 | 304 | */ |
305 | - protected function tagClose( $parser, $name ) { |
|
305 | + protected function tagClose($parser, $name) { |
|
306 | 306 | $tag = ''; |
307 | 307 | |
308 | 308 | /** |
@@ -311,26 +311,26 @@ discard block |
||
311 | 311 | * |
312 | 312 | * self::tagOpen method |
313 | 313 | */ |
314 | - if ( !$this->isEmpty && !( $this->inTarget && $name !== 'target' ) ) { |
|
314 | + if (!$this->isEmpty && !($this->inTarget && $name !== 'target')) { |
|
315 | 315 | |
316 | - if ( !$this->inTarget ) { |
|
316 | + if (!$this->inTarget) { |
|
317 | 317 | $tag = "</$name>"; |
318 | 318 | } |
319 | 319 | |
320 | - if ( 'target' == $name ) { |
|
320 | + if ('target' == $name) { |
|
321 | 321 | |
322 | - if ( $this->currentTransUnitTranslate === 'no' ) { |
|
322 | + if ($this->currentTransUnitTranslate === 'no') { |
|
323 | 323 | // do nothing |
324 | - } elseif ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
324 | + } elseif (isset($this->transUnits[$this->currentTransUnitId])) { |
|
325 | 325 | |
326 | 326 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
327 | 327 | // actually there may be more that one segment to that ID if there are two mrk of the same source segment |
328 | 328 | |
329 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
329 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
330 | 330 | |
331 | 331 | // $currentSegmentId |
332 | - if ( !empty( $listOfSegmentsIds ) ) { |
|
333 | - $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
|
332 | + if (!empty($listOfSegmentsIds)) { |
|
333 | + $this->setCurrentSegmentArray($listOfSegmentsIds); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | /* |
@@ -347,12 +347,12 @@ discard block |
||
347 | 347 | $this->lastTransUnit = []; |
348 | 348 | |
349 | 349 | $last_value = null; |
350 | - $segmentsCount = count( $listOfSegmentsIds ); |
|
351 | - for ( $i = 0; $i < $segmentsCount; $i++ ) { |
|
352 | - $id = $listOfSegmentsIds[ $i ]; |
|
353 | - if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
|
354 | - $last_value = $listOfSegmentsIds[ $i ]; |
|
355 | - $this->lastTransUnit[] = $this->segments[ $id ]; |
|
350 | + $segmentsCount = count($listOfSegmentsIds); |
|
351 | + for ($i = 0; $i < $segmentsCount; $i++) { |
|
352 | + $id = $listOfSegmentsIds[$i]; |
|
353 | + if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) { |
|
354 | + $last_value = $listOfSegmentsIds[$i]; |
|
355 | + $this->lastTransUnit[] = $this->segments[$id]; |
|
356 | 356 | } |
357 | 357 | } |
358 | 358 | |
@@ -364,27 +364,27 @@ discard block |
||
364 | 364 | // we must reset the lastMrkId found because this is a new segment. |
365 | 365 | $lastMrkId = -1; |
366 | 366 | |
367 | - if ( $this->xliffVersion === 2 ) { |
|
368 | - $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
|
367 | + if ($this->xliffVersion === 2) { |
|
368 | + $seg = $this->segments[$this->currentSegmentArray['sid']]; |
|
369 | 369 | |
370 | 370 | // update counts |
371 | - if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
|
372 | - $this->updateSegmentCounts( $seg ); |
|
371 | + if (!$this->hasWrittenCounts && !empty($seg)) { |
|
372 | + $this->updateSegmentCounts($seg); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | // delete translations so the prepareSegment |
376 | 376 | // will put source content in target tag |
377 | - if ( $this->sourceInTarget ) { |
|
378 | - $seg[ 'translation' ] = ''; |
|
377 | + if ($this->sourceInTarget) { |
|
378 | + $seg['translation'] = ''; |
|
379 | 379 | $this->resetCounts(); |
380 | 380 | } |
381 | 381 | |
382 | 382 | // append $translation |
383 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
383 | + $translation = $this->prepareTranslation($seg, $translation); |
|
384 | 384 | |
385 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
385 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
386 | 386 | } else { |
387 | - foreach ( $listOfSegmentsIds as $pos => $id ) { |
|
387 | + foreach ($listOfSegmentsIds as $pos => $id) { |
|
388 | 388 | |
389 | 389 | /* |
390 | 390 | * This routine works to respect the positional orders of markers. |
@@ -397,8 +397,8 @@ discard block |
||
397 | 397 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
398 | 398 | * If it is null leave it NULL |
399 | 399 | */ |
400 | - if ( (int)$this->segments[ $id ][ "mrk_id" ] < 0 && $this->segments[ $id ][ "mrk_id" ] !== null ) { |
|
401 | - $this->segments[ $id ][ "mrk_id" ] = 0; |
|
400 | + if ((int)$this->segments[$id]["mrk_id"] < 0 && $this->segments[$id]["mrk_id"] !== null) { |
|
401 | + $this->segments[$id]["mrk_id"] = 0; |
|
402 | 402 | } |
403 | 403 | |
404 | 404 | /* |
@@ -407,65 +407,65 @@ discard block |
||
407 | 407 | * ( null <= -1 ) === true |
408 | 408 | * so, cast to int |
409 | 409 | */ |
410 | - if ( (int)$this->segments[ $id ][ "mrk_id" ] <= $lastMrkId ) { |
|
410 | + if ((int)$this->segments[$id]["mrk_id"] <= $lastMrkId) { |
|
411 | 411 | break; |
412 | 412 | } |
413 | 413 | |
414 | 414 | // set $this->currentSegment |
415 | - $seg = $this->segments[ $id ]; |
|
415 | + $seg = $this->segments[$id]; |
|
416 | 416 | |
417 | 417 | // update counts |
418 | - if ( !empty( $seg ) ) { |
|
419 | - $this->updateSegmentCounts( $seg ); |
|
418 | + if (!empty($seg)) { |
|
419 | + $this->updateSegmentCounts($seg); |
|
420 | 420 | } |
421 | 421 | |
422 | 422 | // delete translations so the prepareSegment |
423 | 423 | // will put source content in target tag |
424 | - if ( $this->sourceInTarget ) { |
|
425 | - $seg[ 'translation' ] = ''; |
|
424 | + if ($this->sourceInTarget) { |
|
425 | + $seg['translation'] = ''; |
|
426 | 426 | $this->resetCounts(); |
427 | 427 | } |
428 | 428 | |
429 | 429 | // append $translation |
430 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
430 | + $translation = $this->prepareTranslation($seg, $translation); |
|
431 | 431 | |
432 | 432 | // for xliff 2 we need $this->transUnits[ $this->currentId ] [ $pos ] for populating metadata |
433 | 433 | |
434 | - unset( $this->transUnits[ $this->currentTransUnitId ] [ $pos ] ); |
|
434 | + unset($this->transUnits[$this->currentTransUnitId] [$pos]); |
|
435 | 435 | |
436 | - $lastMrkId = $this->segments[ $id ][ "mrk_id" ]; |
|
436 | + $lastMrkId = $this->segments[$id]["mrk_id"]; |
|
437 | 437 | |
438 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
438 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
439 | 439 | } |
440 | 440 | } |
441 | 441 | |
442 | 442 | //append translation |
443 | 443 | $targetLang = ''; |
444 | - if ( $this->xliffVersion === 1 ) { |
|
444 | + if ($this->xliffVersion === 1) { |
|
445 | 445 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
446 | 446 | } |
447 | 447 | |
448 | - $tag = $this->buildTranslateTag( $targetLang, $stateProp, $translation, $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
|
448 | + $tag = $this->buildTranslateTag($targetLang, $stateProp, $translation, $this->counts['raw_word_count'], $this->counts['eq_word_count']); |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | // signal we are leaving a target |
452 | 452 | $this->targetWasWritten = true; |
453 | 453 | $this->inTarget = false; |
454 | - $this->postProcAndFlush( $this->outputFP, $tag, $treatAsCDATA = true ); |
|
455 | - } elseif ( in_array( $name, $this->nodesToCopy ) ) { // we are closing a critical CDATA section |
|
454 | + $this->postProcAndFlush($this->outputFP, $tag, $treatAsCDATA = true); |
|
455 | + } elseif (in_array($name, $this->nodesToCopy)) { // we are closing a critical CDATA section |
|
456 | 456 | |
457 | 457 | $this->bufferIsActive = false; |
458 | 458 | |
459 | 459 | // only for Xliff 2.* |
460 | 460 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
461 | - if ( 'mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts ) { |
|
461 | + if ('mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts) { |
|
462 | 462 | |
463 | 463 | // we need to update counts here |
464 | 464 | $this->updateCounts(); |
465 | 465 | $this->hasWrittenCounts = true; |
466 | 466 | |
467 | 467 | $tag = $this->CDATABuffer; |
468 | - $tag .= $this->getWordCountGroupForXliffV2( false ); |
|
468 | + $tag .= $this->getWordCountGroupForXliffV2(false); |
|
469 | 469 | $tag .= " </mda:metadata>"; |
470 | 470 | |
471 | 471 | } else { |
@@ -475,59 +475,59 @@ discard block |
||
475 | 475 | $this->CDATABuffer = ""; |
476 | 476 | |
477 | 477 | //flush to pointer |
478 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
479 | - } elseif ( 'segment' === $name ) { |
|
478 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
479 | + } elseif ('segment' === $name) { |
|
480 | 480 | |
481 | 481 | // only for Xliff 2.* |
482 | 482 | // if segment has no <target> add it BEFORE </segment> |
483 | - if ( $this->xliffVersion === 2 && !$this->targetWasWritten ) { |
|
483 | + if ($this->xliffVersion === 2 && !$this->targetWasWritten) { |
|
484 | 484 | |
485 | 485 | $seg = $this->getCurrentSegment(); |
486 | 486 | |
487 | 487 | // copy attr from <source> |
488 | 488 | $tag = '<target'; |
489 | - foreach ( $this->sourceAttributes as $k => $v ) { |
|
489 | + foreach ($this->sourceAttributes as $k => $v) { |
|
490 | 490 | $tag .= " $k=\"$v\""; |
491 | 491 | } |
492 | 492 | |
493 | - $tag .= '>' . $seg[ 'translation' ] . '</target></segment>'; |
|
493 | + $tag .= '>' . $seg['translation'] . '</target></segment>'; |
|
494 | 494 | } |
495 | 495 | |
496 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
496 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
497 | 497 | |
498 | 498 | // we are leaving <segment>, reset $segmentHasTarget |
499 | 499 | $this->targetWasWritten = false; |
500 | 500 | |
501 | - } elseif ( $name === 'trans-unit' ) { |
|
501 | + } elseif ($name === 'trans-unit') { |
|
502 | 502 | |
503 | 503 | // only for Xliff 1.* |
504 | 504 | // handling </trans-unit> closure |
505 | - if ( !$this->targetWasWritten ) { |
|
505 | + if (!$this->targetWasWritten) { |
|
506 | 506 | $seg = $this->getCurrentSegment(); |
507 | 507 | $lastMrkState = null; |
508 | 508 | $stateProp = ''; |
509 | 509 | $tag = ''; |
510 | 510 | |
511 | 511 | // if there is translation available insert <target> BEFORE </trans-unit> |
512 | - if ( isset( $seg[ 'translation' ] ) ) { |
|
513 | - list( $stateProp, $lastMrkState ) = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
|
514 | - $tag .= $this->createTargetTag( $seg[ 'translation' ], $stateProp ); |
|
512 | + if (isset($seg['translation'])) { |
|
513 | + list($stateProp, $lastMrkState) = $this->setTransUnitState($seg, $stateProp, $lastMrkState); |
|
514 | + $tag .= $this->createTargetTag($seg['translation'], $stateProp); |
|
515 | 515 | } |
516 | 516 | |
517 | 517 | $tag .= '</trans-unit>'; |
518 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
518 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
519 | 519 | } else { |
520 | - $this->postProcAndFlush( $this->outputFP, '</trans-unit>' ); |
|
520 | + $this->postProcAndFlush($this->outputFP, '</trans-unit>'); |
|
521 | 521 | } |
522 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
522 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
523 | 523 | $this->CDATABuffer .= "</$name>"; |
524 | 524 | // Do NOT Flush |
525 | 525 | } else { //generic tag closure do Nothing |
526 | 526 | // flush to pointer |
527 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
527 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
528 | 528 | } |
529 | - } elseif ( $this->CDATABuffer === '<note/>' && $this->bufferIsActive === true ) { |
|
530 | - $this->postProcAndFlush( $this->outputFP, '<note/>' ); |
|
529 | + } elseif ($this->CDATABuffer === '<note/>' && $this->bufferIsActive === true) { |
|
530 | + $this->postProcAndFlush($this->outputFP, '<note/>'); |
|
531 | 531 | $this->bufferIsActive = false; |
532 | 532 | $this->CDATABuffer = ''; |
533 | 533 | $this->isEmpty = false; |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | } |
538 | 538 | |
539 | 539 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
540 | - if ( $this->tuTagName === $name ) { |
|
540 | + if ($this->tuTagName === $name) { |
|
541 | 541 | $this->currentTransUnitTranslate = null; |
542 | 542 | $this->inTU = false; |
543 | 543 | $this->segmentPositionInTu = -1; |
@@ -554,21 +554,21 @@ discard block |
||
554 | 554 | * |
555 | 555 | * @param array $listOfSegmentsIds |
556 | 556 | */ |
557 | - private function setCurrentSegmentArray( array $listOfSegmentsIds = [] ) { |
|
557 | + private function setCurrentSegmentArray(array $listOfSegmentsIds = []) { |
|
558 | 558 | // $currentSegmentId |
559 | - if ( empty( $this->currentSegmentArray ) ) { |
|
559 | + if (empty($this->currentSegmentArray)) { |
|
560 | 560 | $this->currentSegmentArray = [ |
561 | - 'sid' => $listOfSegmentsIds[ 0 ], |
|
561 | + 'sid' => $listOfSegmentsIds[0], |
|
562 | 562 | 'tid' => $this->currentTransUnitId, |
563 | 563 | ]; |
564 | 564 | } else { |
565 | - if ( $this->currentSegmentArray[ 'tid' ] === $this->currentTransUnitId ) { |
|
566 | - $key = array_search( $this->currentSegmentArray[ 'sid' ], $listOfSegmentsIds ); |
|
567 | - $this->currentSegmentArray[ 'sid' ] = $listOfSegmentsIds[ $key + 1 ]; |
|
568 | - $this->currentSegmentArray[ 'tid' ] = $this->currentTransUnitId; |
|
565 | + if ($this->currentSegmentArray['tid'] === $this->currentTransUnitId) { |
|
566 | + $key = array_search($this->currentSegmentArray['sid'], $listOfSegmentsIds); |
|
567 | + $this->currentSegmentArray['sid'] = $listOfSegmentsIds[$key + 1]; |
|
568 | + $this->currentSegmentArray['tid'] = $this->currentTransUnitId; |
|
569 | 569 | } else { |
570 | 570 | $this->currentSegmentArray = [ |
571 | - 'sid' => $listOfSegmentsIds[ 0 ], |
|
571 | + 'sid' => $listOfSegmentsIds[0], |
|
572 | 572 | 'tid' => $this->currentTransUnitId, |
573 | 573 | ]; |
574 | 574 | } |
@@ -580,23 +580,23 @@ discard block |
||
580 | 580 | */ |
581 | 581 | private function updateCounts() { |
582 | 582 | // populate counts |
583 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
583 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
584 | 584 | |
585 | 585 | // $currentSegmentId |
586 | - if ( !empty( $listOfSegmentsIds ) ) { |
|
587 | - $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
|
586 | + if (!empty($listOfSegmentsIds)) { |
|
587 | + $this->setCurrentSegmentArray($listOfSegmentsIds); |
|
588 | 588 | } |
589 | 589 | |
590 | - if ( $this->xliffVersion === 2 ) { |
|
591 | - $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
|
592 | - if ( !empty( $seg ) ) { |
|
593 | - $this->updateSegmentCounts( $seg ); |
|
590 | + if ($this->xliffVersion === 2) { |
|
591 | + $seg = $this->segments[$this->currentSegmentArray['sid']]; |
|
592 | + if (!empty($seg)) { |
|
593 | + $this->updateSegmentCounts($seg); |
|
594 | 594 | } |
595 | 595 | } else { |
596 | - foreach ( $listOfSegmentsIds as $pos => $id ) { |
|
597 | - $seg = $this->segments[ $id ]; |
|
598 | - if ( !empty( $seg ) ) { |
|
599 | - $this->updateSegmentCounts( $seg ); |
|
596 | + foreach ($listOfSegmentsIds as $pos => $id) { |
|
597 | + $seg = $this->segments[$id]; |
|
598 | + if (!empty($seg)) { |
|
599 | + $this->updateSegmentCounts($seg); |
|
600 | 600 | } |
601 | 601 | } |
602 | 602 | } |
@@ -607,27 +607,27 @@ discard block |
||
607 | 607 | /** |
608 | 608 | * @param array $seg |
609 | 609 | */ |
610 | - private function updateSegmentCounts( array $seg = [] ) { |
|
610 | + private function updateSegmentCounts(array $seg = []) { |
|
611 | 611 | |
612 | - $raw_word_count = $seg[ 'raw_word_count' ]; |
|
613 | - $eq_word_count = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 ); |
|
612 | + $raw_word_count = $seg['raw_word_count']; |
|
613 | + $eq_word_count = (floor($seg['eq_word_count'] * 100) / 100); |
|
614 | 614 | |
615 | 615 | |
616 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
616 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
617 | 617 | |
618 | - $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [ |
|
618 | + $this->counts['segments_count_array'][$seg['sid']] = [ |
|
619 | 619 | 'raw_word_count' => $raw_word_count, |
620 | 620 | 'eq_word_count' => $eq_word_count, |
621 | 621 | ]; |
622 | 622 | |
623 | - $this->counts[ 'raw_word_count' ] += $raw_word_count; |
|
624 | - $this->counts[ 'eq_word_count' ] += $eq_word_count; |
|
623 | + $this->counts['raw_word_count'] += $raw_word_count; |
|
624 | + $this->counts['eq_word_count'] += $eq_word_count; |
|
625 | 625 | } |
626 | 626 | |
627 | 627 | private function resetCounts() { |
628 | - $this->counts[ 'segments_count_array' ] = []; |
|
629 | - $this->counts[ 'raw_word_count' ] = 0; |
|
630 | - $this->counts[ 'eq_word_count' ] = 0; |
|
628 | + $this->counts['segments_count_array'] = []; |
|
629 | + $this->counts['raw_word_count'] = 0; |
|
630 | + $this->counts['eq_word_count'] = 0; |
|
631 | 631 | } |
632 | 632 | |
633 | 633 | /** |
@@ -638,38 +638,38 @@ discard block |
||
638 | 638 | * |
639 | 639 | * @return string |
640 | 640 | */ |
641 | - protected function prepareTranslation( $seg, $transUnitTranslation = "" ) { |
|
641 | + protected function prepareTranslation($seg, $transUnitTranslation = "") { |
|
642 | 642 | $endTags = ""; |
643 | 643 | |
644 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
645 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
646 | - $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) && $seg[ 'data_ref_map' ] !== null ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
|
644 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
645 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
646 | + $dataRefMap = (isset($seg['data_ref_map']) && $seg['data_ref_map'] !== null) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
|
647 | 647 | |
648 | - if ( is_null( $seg [ 'translation' ] ) || $seg [ 'translation' ] == '' ) { |
|
648 | + if (is_null($seg ['translation']) || $seg ['translation'] == '') { |
|
649 | 649 | $translation = $segment; |
650 | 650 | } else { |
651 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
651 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
652 | 652 | $error = (isset($seg['error'])) ? $seg['error'] : null; |
653 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
|
653 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) { |
|
654 | 654 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
655 | 655 | } |
656 | 656 | } |
657 | 657 | } |
658 | 658 | |
659 | 659 | // for xliff v2 we ignore the marks on purpose |
660 | - if ( $this->xliffVersion === 2 ) { |
|
660 | + if ($this->xliffVersion === 2) { |
|
661 | 661 | return $translation; |
662 | 662 | } |
663 | 663 | |
664 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
665 | - if ( $this->targetLang === 'ja-JP' ) { |
|
666 | - $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] ); |
|
664 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
665 | + if ($this->targetLang === 'ja-JP') { |
|
666 | + $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']); |
|
667 | 667 | } |
668 | 668 | |
669 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
|
669 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . $translation . $seg['mrk_succ_tags'] . "</mrk>"; |
|
670 | 670 | } |
671 | 671 | |
672 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . $seg[ 'succ_tags' ]; |
|
672 | + $transUnitTranslation .= $seg['prev_tags'] . $translation . $endTags . $seg['succ_tags']; |
|
673 | 673 | |
674 | 674 | return $transUnitTranslation; |
675 | 675 | } |
@@ -683,15 +683,15 @@ discard block |
||
683 | 683 | * |
684 | 684 | * @return string |
685 | 685 | */ |
686 | - private function buildTranslateTag( $targetLang, $stateProp, $translation, $rawWordCount, $eqWordCount ) { |
|
687 | - switch ( $this->xliffVersion ) { |
|
686 | + private function buildTranslateTag($targetLang, $stateProp, $translation, $rawWordCount, $eqWordCount) { |
|
687 | + switch ($this->xliffVersion) { |
|
688 | 688 | case 1: |
689 | 689 | default: |
690 | 690 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
691 | 691 | |
692 | 692 | // if it's a Trados file don't append count group |
693 | - if ( get_class( $this ) !== SdlXliffSAXTranslationReplacer::class ) { |
|
694 | - $tag .= $this->getWordCountGroup( $rawWordCount, $eqWordCount ); |
|
693 | + if (get_class($this) !== SdlXliffSAXTranslationReplacer::class) { |
|
694 | + $tag .= $this->getWordCountGroup($rawWordCount, $eqWordCount); |
|
695 | 695 | } |
696 | 696 | |
697 | 697 | return $tag; |
@@ -707,7 +707,7 @@ discard block |
||
707 | 707 | * |
708 | 708 | * @return string |
709 | 709 | */ |
710 | - private function getWordCountGroup( $raw_word_count, $eq_word_count ) { |
|
710 | + private function getWordCountGroup($raw_word_count, $eq_word_count) { |
|
711 | 711 | return "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">$raw_word_count</count><count count-type=\"x-matecat-weighted\">$eq_word_count</count></count-group>"; |
712 | 712 | } |
713 | 713 | |
@@ -715,11 +715,11 @@ discard block |
||
715 | 715 | * @return array |
716 | 716 | */ |
717 | 717 | private function getCurrentSegment() { |
718 | - if ( $this->currentTransUnitTranslate === 'yes' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
719 | - $index = $this->transUnits[ $this->currentTransUnitId ][ $this->segmentPositionInTu ]; |
|
718 | + if ($this->currentTransUnitTranslate === 'yes' && isset($this->transUnits[$this->currentTransUnitId])) { |
|
719 | + $index = $this->transUnits[$this->currentTransUnitId][$this->segmentPositionInTu]; |
|
720 | 720 | |
721 | - if ( isset( $this->segments[ $index ] ) ) { |
|
722 | - return $this->segments[ $index ]; |
|
721 | + if (isset($this->segments[$index])) { |
|
722 | + return $this->segments[$index]; |
|
723 | 723 | } |
724 | 724 | } |
725 | 725 | |
@@ -734,10 +734,10 @@ discard block |
||
734 | 734 | * |
735 | 735 | * @return string |
736 | 736 | */ |
737 | - private function createTargetTag( $translation, $stateProp ) { |
|
737 | + private function createTargetTag($translation, $stateProp) { |
|
738 | 738 | $targetLang = 'xml:lang="' . $this->targetLang . '"'; |
739 | 739 | |
740 | - return $this->buildTranslateTag( $targetLang, $stateProp, $translation, $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
|
740 | + return $this->buildTranslateTag($targetLang, $stateProp, $translation, $this->counts['raw_word_count'], $this->counts['eq_word_count']); |
|
741 | 741 | } |
742 | 742 | |
743 | 743 | /** |
@@ -745,10 +745,10 @@ discard block |
||
745 | 745 | * |
746 | 746 | * @return string |
747 | 747 | */ |
748 | - private function getWordCountGroupForXliffV2( $withMetadataTag = true ) { |
|
748 | + private function getWordCountGroupForXliffV2($withMetadataTag = true) { |
|
749 | 749 | |
750 | 750 | $this->mdaGroupCounter++; |
751 | - $segments_count_array = $this->counts[ 'segments_count_array' ]; |
|
751 | + $segments_count_array = $this->counts['segments_count_array']; |
|
752 | 752 | |
753 | 753 | $id = $this->currentSegmentArray; |
754 | 754 | |
@@ -756,23 +756,23 @@ discard block |
||
756 | 756 | |
757 | 757 | $return = ''; |
758 | 758 | |
759 | - if ( $withMetadataTag === true ) { |
|
759 | + if ($withMetadataTag === true) { |
|
760 | 760 | $return .= '<mda:metadata>'; |
761 | 761 | } |
762 | 762 | |
763 | 763 | $index = 0; |
764 | - foreach ($segments_count_array as $segments_count_item){ |
|
764 | + foreach ($segments_count_array as $segments_count_item) { |
|
765 | 765 | |
766 | - $id = 'word_count_tu['. $this->currentTransUnitId . '][' . $index.']'; |
|
766 | + $id = 'word_count_tu[' . $this->currentTransUnitId . '][' . $index . ']'; |
|
767 | 767 | $index++; |
768 | 768 | |
769 | 769 | $return .= " <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\"> |
770 | - <mda:meta type=\"x-matecat-raw\">". $segments_count_item['raw_word_count']."</mda:meta> |
|
771 | - <mda:meta type=\"x-matecat-weighted\">". $segments_count_item['eq_word_count']."</mda:meta> |
|
770 | + <mda:meta type=\"x-matecat-raw\">". $segments_count_item['raw_word_count'] . "</mda:meta> |
|
771 | + <mda:meta type=\"x-matecat-weighted\">". $segments_count_item['eq_word_count'] . "</mda:meta> |
|
772 | 772 | </mda:metaGroup>"; |
773 | 773 | } |
774 | 774 | |
775 | - if ( $withMetadataTag === true ) { |
|
775 | + if ($withMetadataTag === true) { |
|
776 | 776 | $return .= '</mda:metadata>'; |
777 | 777 | } |
778 | 778 | |
@@ -787,17 +787,17 @@ discard block |
||
787 | 787 | * |
788 | 788 | * @return array |
789 | 789 | */ |
790 | - private function setTransUnitState( $seg, $state_prop, $lastMrkState ) { |
|
791 | - switch ( $seg[ 'status' ] ) { |
|
790 | + private function setTransUnitState($seg, $state_prop, $lastMrkState) { |
|
791 | + switch ($seg['status']) { |
|
792 | 792 | |
793 | 793 | case TranslationStatus::STATUS_FIXED: |
794 | 794 | case TranslationStatus::STATUS_APPROVED: |
795 | - if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
|
795 | + if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED) { |
|
796 | 796 | |
797 | - if( isset($seg[ 'r2' ]) and $seg[ 'r2' ] == 1 ){ |
|
797 | + if (isset($seg['r2']) and $seg['r2'] == 1) { |
|
798 | 798 | $state_prop = "state=\"final\""; |
799 | 799 | } else { |
800 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\""; |
|
800 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"reviewed\"" : "state=\"signed-off\""; |
|
801 | 801 | } |
802 | 802 | |
803 | 803 | $lastMrkState = TranslationStatus::STATUS_APPROVED; |
@@ -805,7 +805,7 @@ discard block |
||
805 | 805 | break; |
806 | 806 | |
807 | 807 | case TranslationStatus::STATUS_TRANSLATED: |
808 | - if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
|
808 | + if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED) { |
|
809 | 809 | $state_prop = "state=\"translated\""; |
810 | 810 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
811 | 811 | } |
@@ -813,22 +813,22 @@ discard block |
||
813 | 813 | |
814 | 814 | case TranslationStatus::STATUS_REJECTED: // if there is a mark REJECTED and there is not a DRAFT, all the trans-unit is REJECTED. In V2 there is no way to mark |
815 | 815 | case TranslationStatus::STATUS_REBUTTED: |
816 | - if ( ( $lastMrkState == null ) || ( $lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT ) ) { |
|
817 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\""; |
|
816 | + if (($lastMrkState == null) || ($lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT)) { |
|
817 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"initial\"" : "state=\"needs-review-translation\""; |
|
818 | 818 | $lastMrkState = TranslationStatus::STATUS_REJECTED; |
819 | 819 | } |
820 | 820 | break; |
821 | 821 | |
822 | 822 | case TranslationStatus::STATUS_NEW: |
823 | - if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_NEW ) { |
|
824 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\""; |
|
823 | + if (($lastMrkState == null) || $lastMrkState != TranslationStatus::STATUS_NEW) { |
|
824 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\""; |
|
825 | 825 | $lastMrkState = TranslationStatus::STATUS_NEW; |
826 | 826 | } |
827 | 827 | break; |
828 | 828 | |
829 | 829 | case TranslationStatus::STATUS_DRAFT: |
830 | - if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) { |
|
831 | - $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\""; |
|
830 | + if (($lastMrkState == null) || $lastMrkState != TranslationStatus::STATUS_DRAFT) { |
|
831 | + $state_prop = ($this->xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\""; |
|
832 | 832 | $lastMrkState = TranslationStatus::STATUS_DRAFT; |
833 | 833 | } |
834 | 834 | break; |
@@ -837,7 +837,7 @@ discard block |
||
837 | 837 | // this is the case when a segment is not showed in cattool, so the row in |
838 | 838 | // segment_translations does not exists and |
839 | 839 | // ---> $seg[ 'status' ] is NULL |
840 | - if ( $lastMrkState == null ) { //this is the first MRK ID |
|
840 | + if ($lastMrkState == null) { //this is the first MRK ID |
|
841 | 841 | $state_prop = "state=\"translated\""; |
842 | 842 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
843 | 843 | } else { |
@@ -846,17 +846,17 @@ discard block |
||
846 | 846 | break; |
847 | 847 | } |
848 | 848 | |
849 | - return [ $state_prop, $lastMrkState ]; |
|
849 | + return [$state_prop, $lastMrkState]; |
|
850 | 850 | } |
851 | 851 | |
852 | 852 | /** |
853 | 853 | * @inheritDoc |
854 | 854 | */ |
855 | - protected function characterData( $parser, $data ) { |
|
855 | + protected function characterData($parser, $data) { |
|
856 | 856 | // don't write <target> data |
857 | - if ( !$this->inTarget && !$this->bufferIsActive ) { |
|
858 | - $this->postProcAndFlush( $this->outputFP, $data ); |
|
859 | - } elseif ( $this->bufferIsActive ) { |
|
857 | + if (!$this->inTarget && !$this->bufferIsActive) { |
|
858 | + $this->postProcAndFlush($this->outputFP, $data); |
|
859 | + } elseif ($this->bufferIsActive) { |
|
860 | 860 | $this->CDATABuffer .= $data; |
861 | 861 | } |
862 | 862 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * |
15 | 15 | * @var array |
16 | 16 | */ |
17 | - private static $chmap = [ |
|
17 | + private static $chmap = [ |
|
18 | 18 | '¦' => '¦', |
19 | 19 | '©' => '©', |
20 | 20 | '®' => '®', |
@@ -7402,7 +7402,7 @@ discard block |
||
7402 | 7402 | ' |