@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | /** |
| 21 | 21 | * @var bool |
| 22 | 22 | */ |
| 23 | - protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
| 23 | + protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * @var string |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | /** |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - protected string $namespace = "matecat"; // Custom namespace |
|
| 38 | + protected string $namespace = "matecat"; // Custom namespace |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * @var array |
@@ -51,23 +51,23 @@ discard block |
||
| 51 | 51 | /** |
| 52 | 52 | * @inheritDoc |
| 53 | 53 | */ |
| 54 | - protected function tagOpen( $parser, string $name, array $attr ) { |
|
| 54 | + protected function tagOpen($parser, string $name, array $attr) { |
|
| 55 | 55 | |
| 56 | - $this->handleOpenUnit( $name, $attr ); |
|
| 56 | + $this->handleOpenUnit($name, $attr); |
|
| 57 | 57 | |
| 58 | - if ( 'mda:metadata' === $name ) { |
|
| 58 | + if ('mda:metadata' === $name) { |
|
| 59 | 59 | $this->unitContainsMda = true; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - $this->trySetAltTrans( $name );; |
|
| 63 | - $this->checkSetInTarget( $name ); |
|
| 62 | + $this->trySetAltTrans($name); ; |
|
| 63 | + $this->checkSetInTarget($name); |
|
| 64 | 64 | |
| 65 | 65 | // open buffer |
| 66 | - $this->setInBuffer( $name ); |
|
| 66 | + $this->setInBuffer($name); |
|
| 67 | 67 | |
| 68 | 68 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
| 69 | 69 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
| 70 | - if ( !$this->inTarget ) { |
|
| 70 | + if (!$this->inTarget) { |
|
| 71 | 71 | |
| 72 | 72 | $tag = ''; |
| 73 | 73 | |
@@ -89,48 +89,48 @@ discard block |
||
| 89 | 89 | // |
| 90 | 90 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
| 91 | 91 | // |
| 92 | - if ( in_array( $name, [ 'notes', 'originalData', 'segment', 'ignorable' ] ) && |
|
| 92 | + if (in_array($name, ['notes', 'originalData', 'segment', 'ignorable']) && |
|
| 93 | 93 | $this->unitContainsMda === false && |
| 94 | - !empty( $this->transUnits[ $this->currentTransUnitId ] ) && |
|
| 94 | + !empty($this->transUnits[$this->currentTransUnitId]) && |
|
| 95 | 95 | !$this->hasWrittenCounts |
| 96 | 96 | ) { |
| 97 | 97 | // we need to update counts here |
| 98 | 98 | $this->updateCounts(); |
| 99 | 99 | $this->hasWrittenCounts = true; |
| 100 | - $tag .= $this->getWordCountGroupForXliffV2(); |
|
| 100 | + $tag .= $this->getWordCountGroupForXliffV2(); |
|
| 101 | 101 | $this->unitContainsMda = true; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | // construct tag |
| 105 | 105 | $tag .= "<$name "; |
| 106 | 106 | |
| 107 | - foreach ( $attr as $k => $v ) { |
|
| 107 | + foreach ($attr as $k => $v) { |
|
| 108 | 108 | //normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute |
| 109 | - if ( $k != 'state' ) { |
|
| 109 | + if ($k != 'state') { |
|
| 110 | 110 | $tag .= "$k=\"$v\" "; |
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | $seg = $this->getCurrentSegment(); |
| 115 | 115 | |
| 116 | - if ( $name === $this->tuTagName && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
|
| 116 | + if ($name === $this->tuTagName && !empty($seg) && isset($seg['sid'])) { |
|
| 117 | 117 | |
| 118 | 118 | // add `matecat:segment-id` to xliff v.2* |
| 119 | - if ( strpos( $tag, 'matecat:segment-id' ) === false ) { |
|
| 120 | - $tag .= "matecat:segment-id=\"{$seg[ 'sid' ]}\" "; |
|
| 119 | + if (strpos($tag, 'matecat:segment-id') === false) { |
|
| 120 | + $tag .= "matecat:segment-id=\"{$seg['sid']}\" "; |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // replace state for xliff v2 |
| 126 | - if ( 'segment' === $name ) { // add state to segment in Xliff v2 |
|
| 127 | - [ $stateProp, ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ] ); |
|
| 126 | + if ('segment' === $name) { // add state to segment in Xliff v2 |
|
| 127 | + [$stateProp, ] = StatusToStateAttribute::getState($this->xliffVersion, $seg['status']); |
|
| 128 | 128 | $tag .= $stateProp; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
|
| 131 | + $tag = $this->handleOpenXliffTag($name, $attr, $tag); |
|
| 132 | 132 | |
| 133 | - $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|
| 133 | + $this->checkForSelfClosedTagAndFlush($parser, $tag); |
|
| 134 | 134 | |
| 135 | 135 | } |
| 136 | 136 | |
@@ -143,10 +143,10 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { |
|
| 147 | - $tag = parent::handleOpenXliffTag( $name, $attr, $tag ); |
|
| 146 | + protected function handleOpenXliffTag(string $name, array $attr, string $tag): string { |
|
| 147 | + $tag = parent::handleOpenXliffTag($name, $attr, $tag); |
|
| 148 | 148 | // add oasis xliff 20 namespace |
| 149 | - if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
|
| 149 | + if ($name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) { |
|
| 150 | 150 | $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
| 151 | 151 | } |
| 152 | 152 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | /** |
| 157 | 157 | * @inheritDoc |
| 158 | 158 | */ |
| 159 | - protected function tagClose( $parser, string $name ) { |
|
| 159 | + protected function tagClose($parser, string $name) { |
|
| 160 | 160 | $tag = ''; |
| 161 | 161 | |
| 162 | 162 | /** |
@@ -165,39 +165,39 @@ discard block |
||
| 165 | 165 | * |
| 166 | 166 | * self::tagOpen method |
| 167 | 167 | */ |
| 168 | - if ( !$this->isEmpty ) { |
|
| 168 | + if (!$this->isEmpty) { |
|
| 169 | 169 | |
| 170 | 170 | // write closing tag if is not a target |
| 171 | 171 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
| 172 | - if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
|
| 172 | + if (!$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no') { |
|
| 173 | 173 | $tag = "</$name>"; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - if ( 'target' == $name && !$this->inAltTrans ) { |
|
| 176 | + if ('target' == $name && !$this->inAltTrans) { |
|
| 177 | 177 | |
| 178 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 178 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 179 | 179 | |
| 180 | 180 | $seg = $this->getCurrentSegment(); |
| 181 | 181 | |
| 182 | 182 | // update counts |
| 183 | - if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
|
| 184 | - $this->updateSegmentCounts( $seg ); |
|
| 183 | + if (!$this->hasWrittenCounts && !empty($seg)) { |
|
| 184 | + $this->updateSegmentCounts($seg); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | // delete translations so the prepareSegment |
| 188 | 188 | // will put source content in target tag |
| 189 | - if ( $this->sourceInTarget ) { |
|
| 190 | - $seg[ 'translation' ] = ''; |
|
| 189 | + if ($this->sourceInTarget) { |
|
| 190 | + $seg['translation'] = ''; |
|
| 191 | 191 | $this->resetCounts(); |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // append $translation |
| 195 | - $translation = $this->prepareTranslation( $seg ); |
|
| 195 | + $translation = $this->prepareTranslation($seg); |
|
| 196 | 196 | |
| 197 | 197 | //append translation |
| 198 | 198 | $tag = "<target>$translation</target>"; |
| 199 | 199 | |
| 200 | - } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
|
| 200 | + } elseif (!empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no') { |
|
| 201 | 201 | |
| 202 | 202 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
| 203 | 203 | $this->bufferIsActive = false; |
@@ -208,22 +208,22 @@ discard block |
||
| 208 | 208 | // signal we are leaving a target |
| 209 | 209 | $this->targetWasWritten = true; |
| 210 | 210 | $this->inTarget = false; |
| 211 | - $this->postProcAndFlush( $this->outputFP, $tag, true ); |
|
| 211 | + $this->postProcAndFlush($this->outputFP, $tag, true); |
|
| 212 | 212 | |
| 213 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
|
| 213 | + } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section |
|
| 214 | 214 | |
| 215 | 215 | $this->bufferIsActive = false; |
| 216 | 216 | |
| 217 | 217 | // only for Xliff 2.* |
| 218 | 218 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
| 219 | - if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) { |
|
| 219 | + if ('mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts) { |
|
| 220 | 220 | |
| 221 | 221 | // we need to update counts here |
| 222 | 222 | $this->updateCounts(); |
| 223 | 223 | $this->hasWrittenCounts = true; |
| 224 | 224 | |
| 225 | 225 | $tag = $this->CDATABuffer; |
| 226 | - $tag .= $this->getWordCountGroupForXliffV2( false ); |
|
| 226 | + $tag .= $this->getWordCountGroupForXliffV2(false); |
|
| 227 | 227 | $tag .= " </mda:metadata>"; |
| 228 | 228 | |
| 229 | 229 | } else { |
@@ -233,19 +233,19 @@ discard block |
||
| 233 | 233 | $this->CDATABuffer = ""; |
| 234 | 234 | |
| 235 | 235 | //flush to the pointer |
| 236 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 236 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 237 | 237 | |
| 238 | - } elseif ( 'segment' === $name ) { |
|
| 238 | + } elseif ('segment' === $name) { |
|
| 239 | 239 | |
| 240 | 240 | // only for Xliff 2.* |
| 241 | 241 | // if segment has no <target> add it BEFORE </segment> |
| 242 | - if ( !$this->targetWasWritten ) { |
|
| 242 | + if (!$this->targetWasWritten) { |
|
| 243 | 243 | |
| 244 | 244 | $seg = $this->getCurrentSegment(); |
| 245 | 245 | |
| 246 | - if ( isset( $seg[ 'translation' ] ) ) { |
|
| 246 | + if (isset($seg['translation'])) { |
|
| 247 | 247 | |
| 248 | - $translation = $this->prepareTranslation( $seg ); |
|
| 248 | + $translation = $this->prepareTranslation($seg); |
|
| 249 | 249 | // replace the tag |
| 250 | 250 | $tag = "<target>$translation</target>"; |
| 251 | 251 | |
@@ -258,19 +258,19 @@ discard block |
||
| 258 | 258 | // update segmentPositionInTu |
| 259 | 259 | $this->segmentInUnitPosition++; |
| 260 | 260 | |
| 261 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 261 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 262 | 262 | |
| 263 | 263 | // we are leaving <segment>, reset $segmentHasTarget |
| 264 | 264 | $this->targetWasWritten = false; |
| 265 | 265 | |
| 266 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 266 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 267 | 267 | $this->CDATABuffer .= "</$name>"; |
| 268 | 268 | // Do NOT Flush |
| 269 | 269 | } else { //generic tag closure do Nothing |
| 270 | 270 | // flush to pointer |
| 271 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 271 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 272 | 272 | } |
| 273 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
|
| 273 | + } elseif (in_array($name, $this->nodesToBuffer)) { |
|
| 274 | 274 | |
| 275 | 275 | $this->isEmpty = false; |
| 276 | 276 | $this->bufferIsActive = false; |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | $this->CDATABuffer = ""; |
| 279 | 279 | |
| 280 | 280 | //flush to the pointer |
| 281 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 281 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 282 | 282 | |
| 283 | 283 | } else { |
| 284 | 284 | //ok, nothing to be done; reset flag for next coming tag |
@@ -286,10 +286,10 @@ discard block |
||
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // try to signal that we are leaving a target |
| 289 | - $this->tryUnsetAltTrans( $name ); |
|
| 289 | + $this->tryUnsetAltTrans($name); |
|
| 290 | 290 | |
| 291 | 291 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
| 292 | - if ( $this->tuTagName === $name ) { |
|
| 292 | + if ($this->tuTagName === $name) { |
|
| 293 | 293 | $this->currentTransUnitIsTranslatable = null; |
| 294 | 294 | $this->inTU = false; |
| 295 | 295 | $this->unitContainsMda = false; |
@@ -305,8 +305,8 @@ discard block |
||
| 305 | 305 | private function updateCounts() { |
| 306 | 306 | |
| 307 | 307 | $seg = $this->getCurrentSegment(); |
| 308 | - if ( !empty( $seg ) ) { |
|
| 309 | - $this->updateSegmentCounts( $seg ); |
|
| 308 | + if (!empty($seg)) { |
|
| 309 | + $this->updateSegmentCounts($seg); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | } |
@@ -316,30 +316,30 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @return string |
| 318 | 318 | */ |
| 319 | - private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string { |
|
| 319 | + private function getWordCountGroupForXliffV2(bool $withMetadataTag = true): string { |
|
| 320 | 320 | |
| 321 | 321 | $this->mdaGroupCounter++; |
| 322 | - $segments_count_array = $this->counts[ 'segments_count_array' ]; |
|
| 322 | + $segments_count_array = $this->counts['segments_count_array']; |
|
| 323 | 323 | |
| 324 | 324 | $tag = ''; |
| 325 | 325 | |
| 326 | - if ( $withMetadataTag === true ) { |
|
| 326 | + if ($withMetadataTag === true) { |
|
| 327 | 327 | $tag .= '<mda:metadata>'; |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | $index = 0; |
| 331 | - foreach ( $segments_count_array as $segments_count_item ) { |
|
| 331 | + foreach ($segments_count_array as $segments_count_item) { |
|
| 332 | 332 | |
| 333 | 333 | $id = 'word_count_tu.' . $this->currentTransUnitId . '.' . $index; |
| 334 | 334 | $index++; |
| 335 | 335 | |
| 336 | 336 | $tag .= " <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\"> |
| 337 | - <mda:meta type=\"x-matecat-raw\">" . $segments_count_item[ 'raw_word_count' ] . "</mda:meta> |
|
| 338 | - <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item[ 'eq_word_count' ] . "</mda:meta> |
|
| 337 | + <mda:meta type=\"x-matecat-raw\">" . $segments_count_item['raw_word_count'] . "</mda:meta> |
|
| 338 | + <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item['eq_word_count'] . "</mda:meta> |
|
| 339 | 339 | </mda:metaGroup>"; |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - if ( $withMetadataTag === true ) { |
|
| 342 | + if ($withMetadataTag === true) { |
|
| 343 | 343 | $tag .= '</mda:metadata>'; |
| 344 | 344 | } |
| 345 | 345 | |
@@ -354,18 +354,18 @@ discard block |
||
| 354 | 354 | * |
| 355 | 355 | * @return string |
| 356 | 356 | */ |
| 357 | - protected function prepareTranslation( array $seg ): string { |
|
| 357 | + protected function prepareTranslation(array $seg): string { |
|
| 358 | 358 | |
| 359 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
| 360 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
| 361 | - $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
|
| 359 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
| 360 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
| 361 | + $dataRefMap = (isset($seg['data_ref_map'])) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
|
| 362 | 362 | |
| 363 | - if ( $seg [ 'translation' ] == '' ) { |
|
| 363 | + if ($seg ['translation'] == '') { |
|
| 364 | 364 | $translation = $segment; |
| 365 | 365 | } else { |
| 366 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
| 367 | - $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
|
| 368 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
|
| 366 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
| 367 | + $error = (!empty($seg['error'])) ? $seg['error'] : null; |
|
| 368 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) { |
|
| 369 | 369 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
| 370 | 370 | } |
| 371 | 371 | } |
@@ -36,34 +36,34 @@ discard block |
||
| 36 | 36 | /** |
| 37 | 37 | * @var string |
| 38 | 38 | */ |
| 39 | - protected string $namespace = "mtc"; // Custom namespace |
|
| 39 | + protected string $namespace = "mtc"; // Custom namespace |
|
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * @inheritDoc |
| 43 | 43 | */ |
| 44 | - protected function tagOpen( $parser, string $name, array $attr ) { |
|
| 44 | + protected function tagOpen($parser, string $name, array $attr) { |
|
| 45 | 45 | |
| 46 | - $this->handleOpenUnit( $name, $attr ); |
|
| 46 | + $this->handleOpenUnit($name, $attr); |
|
| 47 | 47 | |
| 48 | - $this->trySetAltTrans( $name );; |
|
| 49 | - $this->checkSetInTarget( $name ); |
|
| 48 | + $this->trySetAltTrans($name); ; |
|
| 49 | + $this->checkSetInTarget($name); |
|
| 50 | 50 | |
| 51 | 51 | // open buffer |
| 52 | - $this->setInBuffer( $name ); |
|
| 52 | + $this->setInBuffer($name); |
|
| 53 | 53 | |
| 54 | 54 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
| 55 | 55 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
| 56 | - if ( !$this->inTarget ) { |
|
| 56 | + if (!$this->inTarget) { |
|
| 57 | 57 | |
| 58 | 58 | $tag = ''; |
| 59 | 59 | |
| 60 | 60 | // construct tag |
| 61 | 61 | $tag .= "<$name "; |
| 62 | 62 | |
| 63 | - foreach ( $attr as $k => $v ) { |
|
| 63 | + foreach ($attr as $k => $v) { |
|
| 64 | 64 | |
| 65 | 65 | //if tag name is file, we must replace the target-language attribute |
| 66 | - if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
|
| 66 | + if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) { |
|
| 67 | 67 | //replace Target language with job language provided from constructor |
| 68 | 68 | $tag .= "$k=\"$this->targetLang\" "; |
| 69 | 69 | } else { |
@@ -74,20 +74,20 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | $seg = $this->getCurrentSegment(); |
| 76 | 76 | |
| 77 | - if ( $name === $this->tuTagName && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
|
| 77 | + if ($name === $this->tuTagName && !empty($seg) && isset($seg['sid'])) { |
|
| 78 | 78 | |
| 79 | 79 | // add `help-id` to xliff v.1* |
| 80 | - if ( strpos( $tag, 'help-id' ) === false ) { |
|
| 81 | - if ( !empty( $seg[ 'sid' ] ) ) { |
|
| 82 | - $tag .= "help-id=\"{$seg[ 'sid' ]}\" "; |
|
| 80 | + if (strpos($tag, 'help-id') === false) { |
|
| 81 | + if (!empty($seg['sid'])) { |
|
| 82 | + $tag .= "help-id=\"{$seg['sid']}\" "; |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
|
| 88 | + $tag = $this->handleOpenXliffTag($name, $attr, $tag); |
|
| 89 | 89 | |
| 90 | - $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|
| 90 | + $this->checkForSelfClosedTagAndFlush($parser, $tag); |
|
| 91 | 91 | |
| 92 | 92 | } |
| 93 | 93 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | /** |
| 98 | 98 | * @inheritDoc |
| 99 | 99 | */ |
| 100 | - protected function tagClose( $parser, string $name ) { |
|
| 100 | + protected function tagClose($parser, string $name) { |
|
| 101 | 101 | $tag = ''; |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -106,23 +106,23 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * self::tagOpen method |
| 108 | 108 | */ |
| 109 | - if ( !$this->isEmpty ) { |
|
| 109 | + if (!$this->isEmpty) { |
|
| 110 | 110 | |
| 111 | 111 | // write closing tag if is not a target |
| 112 | 112 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
| 113 | - if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
|
| 113 | + if (!$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no') { |
|
| 114 | 114 | $tag = "</$name>"; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - if ( 'target' == $name && !$this->inAltTrans ) { |
|
| 117 | + if ('target' == $name && !$this->inAltTrans) { |
|
| 118 | 118 | |
| 119 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 119 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 120 | 120 | |
| 121 | 121 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
| 122 | 122 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
| 123 | 123 | $tag = $this->rebuildTarget(); |
| 124 | 124 | |
| 125 | - } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
|
| 125 | + } elseif (!empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no') { |
|
| 126 | 126 | |
| 127 | 127 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
| 128 | 128 | $this->bufferIsActive = false; |
@@ -133,45 +133,45 @@ discard block |
||
| 133 | 133 | $this->targetWasWritten = true; |
| 134 | 134 | // signal we are leaving a target |
| 135 | 135 | $this->inTarget = false; |
| 136 | - $this->postProcAndFlush( $this->outputFP, $tag, true ); |
|
| 136 | + $this->postProcAndFlush($this->outputFP, $tag, true); |
|
| 137 | 137 | |
| 138 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
|
| 138 | + } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section |
|
| 139 | 139 | |
| 140 | 140 | $this->bufferIsActive = false; |
| 141 | 141 | $tag = $this->CDATABuffer . "</$name>"; |
| 142 | 142 | $this->CDATABuffer = ""; |
| 143 | 143 | |
| 144 | 144 | //flush to the pointer |
| 145 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 145 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 146 | 146 | |
| 147 | - } elseif ( $name === $this->tuTagName ) { |
|
| 147 | + } elseif ($name === $this->tuTagName) { |
|
| 148 | 148 | |
| 149 | 149 | $tag = ""; |
| 150 | 150 | |
| 151 | 151 | // handling </trans-unit> closure |
| 152 | - if ( !$this->targetWasWritten ) { |
|
| 152 | + if (!$this->targetWasWritten) { |
|
| 153 | 153 | |
| 154 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 154 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 155 | 155 | $tag = $this->rebuildTarget(); |
| 156 | 156 | } else { |
| 157 | - $tag = $this->createTargetTag( "", "" ); |
|
| 157 | + $tag = $this->createTargetTag("", ""); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - $tag .= "</$this->tuTagName>"; |
|
| 162 | + $tag .= "</$this->tuTagName>"; |
|
| 163 | 163 | $this->targetWasWritten = false; |
| 164 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 164 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 165 | 165 | |
| 166 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 166 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 167 | 167 | $this->CDATABuffer .= "</$name>"; |
| 168 | 168 | // Do NOT Flush |
| 169 | 169 | } else { //generic tag closure do Nothing |
| 170 | 170 | // flush to pointer |
| 171 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 171 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
|
| 174 | + } elseif (in_array($name, $this->nodesToBuffer)) { |
|
| 175 | 175 | |
| 176 | 176 | $this->isEmpty = false; |
| 177 | 177 | $this->bufferIsActive = false; |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | $this->CDATABuffer = ""; |
| 180 | 180 | |
| 181 | 181 | //flush to the pointer |
| 182 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 182 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 183 | 183 | |
| 184 | 184 | } else { |
| 185 | 185 | //ok, nothing to be done; reset flag for next coming tag |
@@ -187,10 +187,10 @@ discard block |
||
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // try to signal that we are leaving a target |
| 190 | - $this->tryUnsetAltTrans( $name ); |
|
| 190 | + $this->tryUnsetAltTrans($name); |
|
| 191 | 191 | |
| 192 | 192 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
| 193 | - if ( $this->tuTagName === $name ) { |
|
| 193 | + if ($this->tuTagName === $name) { |
|
| 194 | 194 | $this->currentTransUnitIsTranslatable = null; |
| 195 | 195 | $this->inTU = false; |
| 196 | 196 | $this->hasWrittenCounts = false; |
@@ -207,31 +207,31 @@ discard block |
||
| 207 | 207 | * |
| 208 | 208 | * @return string |
| 209 | 209 | */ |
| 210 | - protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
|
| 210 | + protected function prepareTranslation(array $seg, string $transUnitTranslation = ""): string { |
|
| 211 | 211 | |
| 212 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
| 213 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
| 212 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
| 213 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
| 214 | 214 | |
| 215 | - if ( $seg [ 'translation' ] == '' ) { |
|
| 215 | + if ($seg ['translation'] == '') { |
|
| 216 | 216 | $translation = $segment; |
| 217 | 217 | } else { |
| 218 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
| 219 | - $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
|
| 220 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
|
| 218 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
| 219 | + $error = (!empty($seg['error'])) ? $seg['error'] : null; |
|
| 220 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, [], $error)) { |
|
| 221 | 221 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
|
| 226 | + $transUnitTranslation .= $seg['prev_tags'] . $this->rebuildMarks($seg, $translation) . ltrim($seg['succ_tags']); |
|
| 227 | 227 | |
| 228 | 228 | return $transUnitTranslation; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - protected function rebuildMarks( array $seg, string $translation ): string { |
|
| 231 | + protected function rebuildMarks(array $seg, string $translation): string { |
|
| 232 | 232 | |
| 233 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
| 234 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
|
| 233 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
| 234 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . $translation . $seg['mrk_succ_tags'] . "</mrk>"; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | return $translation; |
@@ -246,10 +246,10 @@ discard block |
||
| 246 | 246 | * |
| 247 | 247 | * @return string |
| 248 | 248 | */ |
| 249 | - protected function createTargetTag( string $translation, string $stateProp ): string { |
|
| 249 | + protected function createTargetTag(string $translation, string $stateProp): string { |
|
| 250 | 250 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
| 251 | 251 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
| 252 | - $tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts[ 'raw_word_count' ] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts[ 'eq_word_count' ] . '</count></count-group>'; |
|
| 252 | + $tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts['raw_word_count'] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts['eq_word_count'] . '</count></count-group>'; |
|
| 253 | 253 | |
| 254 | 254 | return $tag; |
| 255 | 255 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | // we must reset the lastMrkId found because this is a new segment. |
| 266 | 266 | $lastMrkId = -1; |
| 267 | 267 | |
| 268 | - foreach ( $this->lastTransUnit as $pos => $seg ) { |
|
| 268 | + foreach ($this->lastTransUnit as $pos => $seg) { |
|
| 269 | 269 | |
| 270 | 270 | /* |
| 271 | 271 | * This routine works to respect the positional orders of markers. |
@@ -278,8 +278,8 @@ discard block |
||
| 278 | 278 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
| 279 | 279 | * If it is null leave it NULL |
| 280 | 280 | */ |
| 281 | - if ( (int)$seg[ "mrk_id" ] < 0 && $seg[ "mrk_id" ] !== null ) { |
|
| 282 | - $seg[ "mrk_id" ] = 0; |
|
| 281 | + if ((int)$seg["mrk_id"] < 0 && $seg["mrk_id"] !== null) { |
|
| 282 | + $seg["mrk_id"] = 0; |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /* |
@@ -288,39 +288,39 @@ discard block |
||
| 288 | 288 | * ( null <= -1 ) === true |
| 289 | 289 | * so, cast to int |
| 290 | 290 | */ |
| 291 | - if ( (int)$seg[ "mrk_id" ] <= $lastMrkId ) { |
|
| 291 | + if ((int)$seg["mrk_id"] <= $lastMrkId) { |
|
| 292 | 292 | break; |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | // update counts |
| 296 | - if ( !empty( $seg ) ) { |
|
| 297 | - $this->updateSegmentCounts( $seg ); |
|
| 296 | + if (!empty($seg)) { |
|
| 297 | + $this->updateSegmentCounts($seg); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // delete translations so the prepareSegment |
| 301 | 301 | // will put source content in target tag |
| 302 | - if ( $this->sourceInTarget ) { |
|
| 303 | - $seg[ 'translation' ] = ''; |
|
| 302 | + if ($this->sourceInTarget) { |
|
| 303 | + $seg['translation'] = ''; |
|
| 304 | 304 | $this->resetCounts(); |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // append $translation |
| 308 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
| 308 | + $translation = $this->prepareTranslation($seg, $translation); |
|
| 309 | 309 | |
| 310 | - $lastMrkId = $seg[ "mrk_id" ]; |
|
| 310 | + $lastMrkId = $seg["mrk_id"]; |
|
| 311 | 311 | |
| 312 | - [ $stateProp, $lastMrkState ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ], $lastMrkState ); |
|
| 312 | + [$stateProp, $lastMrkState] = StatusToStateAttribute::getState($this->xliffVersion, $seg['status'], $lastMrkState); |
|
| 313 | 313 | |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | //append translation |
| 317 | - return $this->createTargetTag( $translation, $stateProp ); |
|
| 317 | + return $this->createTargetTag($translation, $stateProp); |
|
| 318 | 318 | |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | protected function getCurrentSegment(): array { |
| 322 | - if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 323 | - return $this->segments[ $this->transUnits[ $this->currentTransUnitId ][ 0 ] ]; // TODO try to understand why here is needed to override the method and set 0 index hardcoded |
|
| 322 | + if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 323 | + return $this->segments[$this->transUnits[$this->currentTransUnitId][0]]; // TODO try to understand why here is needed to override the method and set 0 index hardcoded |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | return []; |
@@ -16,41 +16,41 @@ discard block |
||
| 16 | 16 | * @inheritDoc |
| 17 | 17 | * @throws Exception |
| 18 | 18 | */ |
| 19 | - public function parse( DOMDocument $dom, ?array $output = [] ): array { |
|
| 19 | + public function parse(DOMDocument $dom, ?array $output = []): array { |
|
| 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 ) { |
|
| 35 | + foreach ($file->childNodes as $body) { |
|
| 36 | 36 | |
| 37 | 37 | // external-file |
| 38 | - if ( $body->nodeName === 'header' ) { |
|
| 39 | - foreach ( $body->childNodes as $header ) { |
|
| 40 | - $this->extractExternalFile( $header, $i, $output ); |
|
| 38 | + if ($body->nodeName === 'header') { |
|
| 39 | + foreach ($body->childNodes as $header) { |
|
| 40 | + $this->extractExternalFile($header, $i, $output); |
|
| 41 | 41 | } |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - if ( $body->nodeName === 'body' ) { |
|
| 45 | - foreach ( $body->childNodes as $childNode ) { |
|
| 46 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 44 | + if ($body->nodeName === 'body') { |
|
| 45 | + foreach ($body->childNodes as $childNode) { |
|
| 46 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // trans-unit re-count check |
| 50 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
| 51 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
| 52 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
| 53 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
| 50 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
| 51 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
| 52 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
| 53 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | $i++; |
@@ -66,27 +66,27 @@ discard block |
||
| 66 | 66 | * @param $i |
| 67 | 67 | * @param $output |
| 68 | 68 | */ |
| 69 | - private function extractExternalFile( DOMNode $header, $i, &$output ) { |
|
| 70 | - |
|
| 71 | - if ( $header->nodeName === "skl" ) { |
|
| 72 | - foreach ( $header->childNodes as $referenceNode ) { |
|
| 73 | - if ( $referenceNode->nodeName === "reference" ) { |
|
| 74 | - foreach ( $referenceNode->childNodes as $childNode ) { |
|
| 75 | - if ( $childNode->nodeName === "external-file" ) { |
|
| 76 | - $href = $childNode->getAttribute( "href" ); |
|
| 77 | - $output[ 'files' ][ $i ][ 'attr' ][ 'external-file' ] = $href; |
|
| 69 | + private function extractExternalFile(DOMNode $header, $i, &$output) { |
|
| 70 | + |
|
| 71 | + if ($header->nodeName === "skl") { |
|
| 72 | + foreach ($header->childNodes as $referenceNode) { |
|
| 73 | + if ($referenceNode->nodeName === "reference") { |
|
| 74 | + foreach ($referenceNode->childNodes as $childNode) { |
|
| 75 | + if ($childNode->nodeName === "external-file") { |
|
| 76 | + $href = $childNode->getAttribute("href"); |
|
| 77 | + $output['files'][$i]['attr']['external-file'] = $href; |
|
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | - } elseif ( $referenceNode->nodeName === "external-file" ) { |
|
| 81 | - $href = $referenceNode->getAttribute( "href" ); |
|
| 82 | - $output[ 'files' ][ $i ][ 'attr' ][ 'external-file' ] = $href; |
|
| 80 | + } elseif ($referenceNode->nodeName === "external-file") { |
|
| 81 | + $href = $referenceNode->getAttribute("href"); |
|
| 82 | + $output['files'][$i]['attr']['external-file'] = $href; |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | - } elseif ( $header->nodeName === "reference" ) { |
|
| 86 | - foreach ( $header->childNodes as $referenceNode ) { |
|
| 87 | - if ( $referenceNode->nodeName === "external-file" ) { |
|
| 88 | - $href = $referenceNode->getAttribute( "href" ); |
|
| 89 | - $output[ 'files' ][ $i ][ 'attr' ][ 'external-file' ] = $href; |
|
| 85 | + } elseif ($header->nodeName === "reference") { |
|
| 86 | + foreach ($header->childNodes as $referenceNode) { |
|
| 87 | + if ($referenceNode->nodeName === "external-file") { |
|
| 88 | + $href = $referenceNode->getAttribute("href"); |
|
| 89 | + $output['files'][$i]['attr']['external-file'] = $href; |
|
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -97,50 +97,50 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return array |
| 99 | 99 | */ |
| 100 | - private function extractMetadata( DOMElement $file ): array { |
|
| 100 | + private function extractMetadata(DOMElement $file): array { |
|
| 101 | 101 | $metadata = []; |
| 102 | 102 | $customAttr = []; |
| 103 | 103 | |
| 104 | 104 | /** @var DOMAttr $attribute */ |
| 105 | - foreach ( $file->attributes as $attribute ) { |
|
| 106 | - switch ( $attribute->localName ) { |
|
| 105 | + foreach ($file->attributes as $attribute) { |
|
| 106 | + switch ($attribute->localName) { |
|
| 107 | 107 | // original |
| 108 | 108 | case 'original': |
| 109 | - $metadata[ 'original' ] = $attribute->value; |
|
| 109 | + $metadata['original'] = $attribute->value; |
|
| 110 | 110 | break; |
| 111 | 111 | |
| 112 | 112 | // source-language |
| 113 | 113 | case 'source-language': |
| 114 | - $metadata[ 'source-language' ] = $attribute->value; |
|
| 114 | + $metadata['source-language'] = $attribute->value; |
|
| 115 | 115 | break; |
| 116 | 116 | |
| 117 | 117 | // data-type |
| 118 | 118 | case 'datatype': |
| 119 | - $metadata[ 'data-type' ] = $attribute->value; |
|
| 119 | + $metadata['data-type'] = $attribute->value; |
|
| 120 | 120 | break; |
| 121 | 121 | |
| 122 | 122 | // target-language |
| 123 | 123 | case 'target-language': |
| 124 | - $metadata[ 'target-language' ] = $attribute->value; |
|
| 124 | + $metadata['target-language'] = $attribute->value; |
|
| 125 | 125 | break; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // Custom MateCat x-Attribute |
| 129 | - preg_match( '|x-(.*?)|si', $attribute->localName, $temp ); |
|
| 130 | - if ( isset( $temp[ 1 ] ) ) { |
|
| 131 | - $customAttr[ $attribute->localName ] = $attribute->value; |
|
| 129 | + preg_match('|x-(.*?)|si', $attribute->localName, $temp); |
|
| 130 | + if (isset($temp[1])) { |
|
| 131 | + $customAttr[$attribute->localName] = $attribute->value; |
|
| 132 | 132 | } |
| 133 | - unset( $temp ); |
|
| 133 | + unset($temp); |
|
| 134 | 134 | |
| 135 | 135 | // Custom MateCat namespace Attribute mtc: |
| 136 | - preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp ); |
|
| 137 | - if ( isset( $temp[ 1 ] ) ) { |
|
| 138 | - $customAttr[ $attribute->nodeName ] = $attribute->value; |
|
| 136 | + preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp); |
|
| 137 | + if (isset($temp[1])) { |
|
| 138 | + $customAttr[$attribute->nodeName] = $attribute->value; |
|
| 139 | 139 | } |
| 140 | - unset( $temp ); |
|
| 140 | + unset($temp); |
|
| 141 | 141 | |
| 142 | - if ( !empty( $customAttr ) ) { |
|
| 143 | - $metadata[ 'custom' ] = $customAttr; |
|
| 142 | + if (!empty($customAttr)) { |
|
| 143 | + $metadata['custom'] = $customAttr; |
|
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | 146 | |
@@ -152,16 +152,16 @@ discard block |
||
| 152 | 152 | * |
| 153 | 153 | * @return array |
| 154 | 154 | */ |
| 155 | - private function extractReference( DOMElement $file ): array { |
|
| 155 | + private function extractReference(DOMElement $file): array { |
|
| 156 | 156 | $reference = []; |
| 157 | 157 | |
| 158 | 158 | $order = 0; |
| 159 | - foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) { |
|
| 159 | + foreach ($file->getElementsByTagName('reference') as $ref) { |
|
| 160 | 160 | /** @var DOMNode $childNode */ |
| 161 | - foreach ( $ref->childNodes as $childNode ) { |
|
| 162 | - if ( $childNode->nodeName === 'internal-file' ) { |
|
| 163 | - $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue; |
|
| 164 | - $reference[ $order ][ 'base64' ] = trim( $childNode->nodeValue ); |
|
| 161 | + foreach ($ref->childNodes as $childNode) { |
|
| 162 | + if ($childNode->nodeName === 'internal-file') { |
|
| 163 | + $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue; |
|
| 164 | + $reference[$order]['base64'] = trim($childNode->nodeValue); |
|
| 165 | 165 | $order++; |
| 166 | 166 | } |
| 167 | 167 | } |
@@ -183,61 +183,61 @@ discard block |
||
| 183 | 183 | * |
| 184 | 184 | * @throws Exception |
| 185 | 185 | */ |
| 186 | - protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ) { |
|
| 186 | + protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []) { |
|
| 187 | 187 | // metadata |
| 188 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
| 188 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
| 189 | 189 | |
| 190 | 190 | // notes |
| 191 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit ); |
|
| 191 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit); |
|
| 192 | 192 | |
| 193 | 193 | // content |
| 194 | 194 | /** @var DOMElement $childNode */ |
| 195 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
| 195 | + foreach ($transUnit->childNodes as $childNode) { |
|
| 196 | 196 | // source |
| 197 | - if ( $childNode->nodeName === 'source' ) { |
|
| 198 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode ); |
|
| 197 | + if ($childNode->nodeName === 'source') { |
|
| 198 | + $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // seg-source |
| 202 | - if ( $childNode->nodeName === 'seg-source' ) { |
|
| 203 | - $rawSegment = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ]; |
|
| 204 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment ); |
|
| 202 | + if ($childNode->nodeName === 'seg-source') { |
|
| 203 | + $rawSegment = $output['files'][$i]['trans-units'][$j]['source']['raw-content']; |
|
| 204 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // target |
| 208 | - if ( $childNode->nodeName === 'target' ) { |
|
| 209 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode ); |
|
| 208 | + if ($childNode->nodeName === 'target') { |
|
| 209 | + $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode); |
|
| 210 | 210 | |
| 211 | 211 | // seg-target |
| 212 | - $targetRawContent = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ] ?? null; |
|
| 213 | - $segSource = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] ?? null; |
|
| 212 | + $targetRawContent = $output['files'][$i]['trans-units'][$j]['target']['raw-content'] ?? null; |
|
| 213 | + $segSource = $output['files'][$i]['trans-units'][$j]['seg-source'] ?? null; |
|
| 214 | 214 | |
| 215 | - if ( !empty( $targetRawContent ) and isset( $segSource ) and count( $segSource ) > 0 ) { |
|
| 216 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
| 217 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ][ 'attr' ] = $this->extractTagAttributes( $childNode ); |
|
| 215 | + if (!empty($targetRawContent) and isset($segSource) and count($segSource) > 0) { |
|
| 216 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
| 217 | + $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | // locked |
| 222 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
| 223 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
| 222 | + if ($childNode->nodeName === 'sdl:seg') { |
|
| 223 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | // context-group |
| 228 | - if ( !empty( $contextGroups ) ) { |
|
| 229 | - foreach ( $contextGroups as $contextGroup ) { |
|
| 230 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
| 228 | + if (!empty($contextGroups)) { |
|
| 229 | + foreach ($contextGroups as $contextGroup) { |
|
| 230 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
| 235 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
| 234 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
| 235 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | // alt-trans |
| 239 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
| 240 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
| 239 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
| 240 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | $j++; |
@@ -250,41 +250,41 @@ discard block |
||
| 250 | 250 | * @return array |
| 251 | 251 | * @throws Exception |
| 252 | 252 | */ |
| 253 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ): array { |
|
| 253 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck): array { |
|
| 254 | 254 | $metadata = []; |
| 255 | 255 | |
| 256 | 256 | // id MUST NOT be null |
| 257 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
| 258 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
| 257 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
| 258 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | /** |
| 262 | 262 | * @var DOMAttr $element |
| 263 | 263 | */ |
| 264 | - foreach ( $transUnit->attributes as $element ) { |
|
| 264 | + foreach ($transUnit->attributes as $element) { |
|
| 265 | 265 | |
| 266 | - if ( $element->nodeName === "id" ) { |
|
| 266 | + if ($element->nodeName === "id") { |
|
| 267 | 267 | |
| 268 | 268 | $id = $element->nodeValue; |
| 269 | 269 | |
| 270 | - if ( strlen( $id ) > 100 ) { |
|
| 271 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
| 270 | + if (strlen($id) > 100) { |
|
| 271 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | $transUnitIdArrayForUniquenessCheck[] = $id; |
| 275 | - $metadata[ 'id' ] = $id; |
|
| 275 | + $metadata['id'] = $id; |
|
| 276 | 276 | |
| 277 | - } elseif ( $element->nodeName === "approved" ) { |
|
| 277 | + } elseif ($element->nodeName === "approved") { |
|
| 278 | 278 | // approved as BOOLEAN |
| 279 | 279 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
| 280 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
| 281 | - } elseif ( $element->nodeName === "maxwidth" ) { |
|
| 280 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
| 281 | + } elseif ($element->nodeName === "maxwidth") { |
|
| 282 | 282 | // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character |
| 283 | 283 | // we duplicate the info to allow Xliff V1 and V2 to work the same |
| 284 | - $metadata[ 'sizeRestriction' ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
| 285 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
| 284 | + $metadata['sizeRestriction'] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
| 285 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
| 286 | 286 | } else { |
| 287 | - $metadata[ $element->nodeName ] = $element->nodeValue; |
|
| 287 | + $metadata[$element->nodeName] = $element->nodeValue; |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | } |
@@ -299,19 +299,19 @@ discard block |
||
| 299 | 299 | * @return array |
| 300 | 300 | * @throws Exception |
| 301 | 301 | */ |
| 302 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ): array { |
|
| 302 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit): array { |
|
| 303 | 303 | $notes = []; |
| 304 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
| 304 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
| 305 | 305 | |
| 306 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
| 306 | + $noteValue = $this->extractTagContent($dom, $note); |
|
| 307 | 307 | |
| 308 | - if ( '' !== $noteValue ) { |
|
| 308 | + if ('' !== $noteValue) { |
|
| 309 | 309 | |
| 310 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
| 310 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
| 311 | 311 | |
| 312 | 312 | // extract all the attributes |
| 313 | - foreach ( $note->attributes as $attribute ) { |
|
| 314 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
| 313 | + foreach ($note->attributes as $attribute) { |
|
| 314 | + $extractedNote[$attribute->name] = $attribute->value; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | $notes[] = $extractedNote; |
@@ -327,14 +327,14 @@ discard block |
||
| 327 | 327 | * |
| 328 | 328 | * @return array |
| 329 | 329 | */ |
| 330 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ): array { |
|
| 330 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup): array { |
|
| 331 | 331 | $cg = []; |
| 332 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
| 332 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
| 333 | 333 | |
| 334 | 334 | /** @var DOMNode $context */ |
| 335 | - foreach ( $contextGroup->childNodes as $context ) { |
|
| 336 | - if ( $context->nodeName === 'context' ) { |
|
| 337 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
| 335 | + foreach ($contextGroup->childNodes as $context) { |
|
| 336 | + if ($context->nodeName === 'context') { |
|
| 337 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | * |
| 347 | 347 | * @return array |
| 348 | 348 | */ |
| 349 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
| 349 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
| 350 | 350 | $at = []; |
| 351 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
| 351 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
| 352 | 352 | |
| 353 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
| 354 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
| 353 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
| 354 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
| 358 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
| 357 | + if ($altTrans->getElementsByTagName('target')) { |
|
| 358 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | return $at; |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | * |
| 367 | 367 | * @return bool |
| 368 | 368 | */ |
| 369 | - private function extractLocked( DOMElement $locked ) { |
|
| 370 | - return null !== $locked->getAttribute( 'locked' ); |
|
| 369 | + private function extractLocked(DOMElement $locked) { |
|
| 370 | + return null !== $locked->getAttribute('locked'); |
|
| 371 | 371 | } |
| 372 | 372 | } |