@@ -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 | } |
@@ -17,8 +17,8 @@ discard block |
||
| 17 | 17 | * @return string |
| 18 | 18 | * @throws Exception |
| 19 | 19 | */ |
| 20 | - public static function cleanCDATA( string $testString ): string { |
|
| 21 | - $cleanXMLContent = new SimpleXMLElement( '<rootNoteNode>' . $testString . '</rootNoteNode>', LIBXML_NOCDATA ); |
|
| 20 | + public static function cleanCDATA(string $testString): string { |
|
| 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 $string ): bool { |
|
| 32 | - if ( is_numeric( $string ) ) { |
|
| 31 | + public static function isJSON(string $string): bool { |
|
| 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 $string ): array { |
|
| 65 | - $decodedJSON = json_decode( $string, true ); |
|
| 64 | + public static function jsonToArray(string $string): array { |
|
| 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 | /** |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private static function raiseLastJsonException() { |
| 75 | 75 | |
| 76 | - [ $msg, $error ] = self::getLastJsonError(); |
|
| 76 | + [$msg, $error] = self::getLastJsonError(); |
|
| 77 | 77 | |
| 78 | - if ( $error != JSON_ERROR_NONE ) { |
|
| 79 | - throw new NotValidJSONException( $msg, $error ); |
|
| 78 | + if ($error != JSON_ERROR_NONE) { |
|
| 79 | + throw new NotValidJSONException($msg, $error); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | } |
@@ -86,11 +86,11 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | private static function getLastJsonError(): array { |
| 88 | 88 | |
| 89 | - if ( function_exists( "json_last_error" ) ) { |
|
| 89 | + if (function_exists("json_last_error")) { |
|
| 90 | 90 | |
| 91 | 91 | $error = json_last_error(); |
| 92 | 92 | |
| 93 | - switch ( $error ) { |
|
| 93 | + switch ($error) { |
|
| 94 | 94 | case JSON_ERROR_NONE: |
| 95 | 95 | $msg = null; # - No errors |
| 96 | 96 | break; |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | break; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - return [ $msg, $error ]; |
|
| 117 | + return [$msg, $error]; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - return [ null, JSON_ERROR_NONE ]; |
|
| 120 | + return [null, JSON_ERROR_NONE]; |
|
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | |
@@ -140,11 +140,11 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | - public static function fixNonWellFormedXml( string $content, ?bool $escapeStrings = true ): string { |
|
| 144 | - if ( self::$find_xliff_tags_reg === null ) { |
|
| 143 | + public static function fixNonWellFormedXml(string $content, ?bool $escapeStrings = true): string { |
|
| 144 | + if (self::$find_xliff_tags_reg === null) { |
|
| 145 | 145 | // Convert the list of tags in a regexp list, for example "g|x|bx|ex" |
| 146 | 146 | $xliffTags = XliffTags::$tags; |
| 147 | - $xliff_tags_reg_list = implode( '|', $xliffTags ); |
|
| 147 | + $xliff_tags_reg_list = implode('|', $xliffTags); |
|
| 148 | 148 | // Regexp to find all the XLIFF tags: |
| 149 | 149 | // </? -> matches the tag start, for both opening and |
| 150 | 150 | // closure tags (see the optional slash) |
@@ -165,30 +165,30 @@ discard block |
||
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Find all the XLIFF tags |
| 168 | - preg_match_all( self::$find_xliff_tags_reg, $content, $matches ); |
|
| 169 | - $tags = (array)$matches[ 0 ]; |
|
| 168 | + preg_match_all(self::$find_xliff_tags_reg, $content, $matches); |
|
| 169 | + $tags = (array)$matches[0]; |
|
| 170 | 170 | |
| 171 | 171 | // Prepare placeholders |
| 172 | 172 | $tags_placeholders = []; |
| 173 | - $tagsNum = count( $tags ); |
|
| 174 | - for ( $i = 0; $i < $tagsNum; $i++ ) { |
|
| 175 | - $tag = $tags[ $i ]; |
|
| 176 | - $tags_placeholders[ $tag ] = "#@!XLIFF-TAG-$i!@#"; |
|
| 173 | + $tagsNum = count($tags); |
|
| 174 | + for ($i = 0; $i < $tagsNum; $i++) { |
|
| 175 | + $tag = $tags[$i]; |
|
| 176 | + $tags_placeholders[$tag] = "#@!XLIFF-TAG-$i!@#"; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Replace all XLIFF tags with placeholders that will not be escaped |
| 180 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
| 181 | - $content = str_replace( $tag, $placeholder, $content ); |
|
| 180 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
| 181 | + $content = str_replace($tag, $placeholder, $content); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Escape the string with the remaining non-XLIFF tags |
| 185 | - if ( $escapeStrings ) { |
|
| 186 | - $content = htmlspecialchars( $content, ENT_NOQUOTES, 'UTF-8', false ); |
|
| 185 | + if ($escapeStrings) { |
|
| 186 | + $content = htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8', false); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // Put again in place the original XLIFF tags replacing placeholders |
| 190 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
| 191 | - $content = str_replace( $placeholder, $tag, $content ); |
|
| 190 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
| 191 | + $content = str_replace($placeholder, $tag, $content); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | return $content; |
@@ -199,17 +199,17 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return string |
| 201 | 201 | */ |
| 202 | - public static function removeDangerousChars( $string ): string { |
|
| 202 | + public static function removeDangerousChars($string): string { |
|
| 203 | 203 | // clean invalid xml entities ( characters with ascii < 32 and different from 0A, 0D and 09 |
| 204 | 204 | $regexpEntity = '/&#x(0[0-8BCEF]|1[\dA-F]|7F);/u'; |
| 205 | 205 | |
| 206 | 206 | // remove binary chars in some xliff files |
| 207 | 207 | $regexpAscii = '/[\x{00}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}\x{7F}]/u'; |
| 208 | 208 | |
| 209 | - $string = preg_replace( $regexpAscii, '', $string ?? '' ); |
|
| 210 | - $string = preg_replace( $regexpEntity, '', $string ?? '' ); |
|
| 209 | + $string = preg_replace($regexpAscii, '', $string ?? ''); |
|
| 210 | + $string = preg_replace($regexpEntity, '', $string ?? ''); |
|
| 211 | 211 | |
| 212 | - return !empty( $string ) || strlen( $string ) > 0 ? $string : ""; |
|
| 212 | + return !empty($string) || strlen($string) > 0 ? $string : ""; |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | |
@@ -219,15 +219,15 @@ discard block |
||
| 219 | 219 | * |
| 220 | 220 | * @return string |
| 221 | 221 | */ |
| 222 | - public static function htmlspecialchars_decode( string $string, ?bool $onlyEscapedEntities = false ): string { |
|
| 223 | - if ( false === $onlyEscapedEntities ) { |
|
| 224 | - return htmlspecialchars_decode( $string, ENT_NOQUOTES ); |
|
| 222 | + public static function htmlspecialchars_decode(string $string, ?bool $onlyEscapedEntities = false): string { |
|
| 223 | + if (false === $onlyEscapedEntities) { |
|
| 224 | + return htmlspecialchars_decode($string, ENT_NOQUOTES); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - return preg_replace_callback( self::$htmlEntityRegex, |
|
| 228 | - function ( $match ) { |
|
| 229 | - return self::htmlspecialchars_decode( $match[ 0 ] ); |
|
| 230 | - }, $string ); |
|
| 227 | + return preg_replace_callback(self::$htmlEntityRegex, |
|
| 228 | + function($match) { |
|
| 229 | + return self::htmlspecialchars_decode($match[0]); |
|
| 230 | + }, $string); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -242,8 +242,8 @@ discard block |
||
| 242 | 242 | * |
| 243 | 243 | * @return bool |
| 244 | 244 | */ |
| 245 | - public static function isADoubleEscapedEntity( string $str ): bool { |
|
| 246 | - return preg_match( self::$htmlEntityRegex, $str ) != 0; |
|
| 245 | + public static function isADoubleEscapedEntity(string $str): bool { |
|
| 246 | + return preg_match(self::$htmlEntityRegex, $str) != 0; |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
@@ -251,8 +251,8 @@ discard block |
||
| 251 | 251 | * |
| 252 | 252 | * @return bool |
| 253 | 253 | */ |
| 254 | - public static function isAValidUuid( $uuid ) { |
|
| 255 | - return preg_match( '/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid ) === 1; |
|
| 254 | + public static function isAValidUuid($uuid) { |
|
| 255 | + return preg_match('/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid) === 1; |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | /** |
@@ -261,8 +261,8 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return array|false|string[] |
| 263 | 263 | */ |
| 264 | - public static function preg_split( $pattern, $subject ) { |
|
| 265 | - return preg_split( $pattern, $subject, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 264 | + public static function preg_split($pattern, $subject) { |
|
| 265 | + return preg_split($pattern, $subject, -1, PREG_SPLIT_NO_EMPTY); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -270,8 +270,8 @@ discard block |
||
| 270 | 270 | * |
| 271 | 271 | * @return int |
| 272 | 272 | */ |
| 273 | - public static function getTheNumberOfTrailingSpaces( $segment ): int { |
|
| 274 | - return mb_strlen( $segment ) - mb_strlen( rtrim( $segment, ' ' ) ); |
|
| 273 | + public static function getTheNumberOfTrailingSpaces($segment): int { |
|
| 274 | + return mb_strlen($segment) - mb_strlen(rtrim($segment, ' ')); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | } |