@@ -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 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @param ?LoggerInterface $logger |
| 32 | 32 | */ |
| 33 | - public function __construct( ?LoggerInterface $logger = null ) { |
|
| 33 | + public function __construct(?LoggerInterface $logger = null) { |
|
| 34 | 34 | $this->logger = $logger; |
| 35 | 35 | } |
| 36 | 36 | |
@@ -45,11 +45,11 @@ discard block |
||
| 45 | 45 | * @param bool $setSourceInTarget |
| 46 | 46 | * @param XliffReplacerCallbackInterface|null $callback |
| 47 | 47 | */ |
| 48 | - public function replaceTranslation( string $originalXliffPath, array $data, array $transUnits, string $targetLang, string $outputFile, bool $setSourceInTarget = false, ?XliffReplacerCallbackInterface $callback = null ) { |
|
| 48 | + public function replaceTranslation(string $originalXliffPath, array $data, array $transUnits, string $targetLang, string $outputFile, bool $setSourceInTarget = false, ?XliffReplacerCallbackInterface $callback = null) { |
|
| 49 | 49 | try { |
| 50 | - $parser = XliffReplacerFactory::getInstance( $originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback ); |
|
| 50 | + $parser = XliffReplacerFactory::getInstance($originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback); |
|
| 51 | 51 | $parser->replaceTranslation(); |
| 52 | - } catch ( Exception $exception ) { |
|
| 52 | + } catch (Exception $exception) { |
|
| 53 | 53 | // do nothing |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -67,26 +67,26 @@ discard block |
||
| 67 | 67 | * @throws InvalidXmlException |
| 68 | 68 | * @throws XmlParsingException |
| 69 | 69 | */ |
| 70 | - public function xliffToArray( string $xliffContent, ?bool $collapseEmptyTags = false ): array { |
|
| 70 | + public function xliffToArray(string $xliffContent, ?bool $collapseEmptyTags = false): array { |
|
| 71 | 71 | $xliff = []; |
| 72 | - $xliffContent = self::forceUft8Encoding( $xliffContent, $xliff ); |
|
| 73 | - $xliffVersion = XliffVersionDetector::detect( $xliffContent ); |
|
| 74 | - $info = XliffProprietaryDetect::getInfoFromXliffContent( $xliffContent ); |
|
| 72 | + $xliffContent = self::forceUft8Encoding($xliffContent, $xliff); |
|
| 73 | + $xliffVersion = XliffVersionDetector::detect($xliffContent); |
|
| 74 | + $info = XliffProprietaryDetect::getInfoFromXliffContent($xliffContent); |
|
| 75 | 75 | |
| 76 | - if ( $xliffVersion === 1 ) { |
|
| 77 | - $xliffContent = self::removeInternalFileTagFromContent( $xliffContent, $xliff ); |
|
| 76 | + if ($xliffVersion === 1) { |
|
| 77 | + $xliffContent = self::removeInternalFileTagFromContent($xliffContent, $xliff); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ( $xliffVersion === 2 ) { |
|
| 81 | - $xliffContent = self::escapeDataInOriginalMap( $xliffContent ); |
|
| 80 | + if ($xliffVersion === 2) { |
|
| 81 | + $xliffContent = self::escapeDataInOriginalMap($xliffContent); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if ( $collapseEmptyTags === false ) { |
|
| 85 | - $xliffContent = self::insertPlaceholderInEmptyTags( $xliffContent ); |
|
| 84 | + if ($collapseEmptyTags === false) { |
|
| 85 | + $xliffContent = self::insertPlaceholderInEmptyTags($xliffContent); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - $xliffProprietary = $info[ 'proprietary_short_name' ] ?? null; |
|
| 89 | - $parser = XliffParserFactory::getInstance( $xliffVersion, $xliffProprietary, $this->logger ); |
|
| 88 | + $xliffProprietary = $info['proprietary_short_name'] ?? null; |
|
| 89 | + $parser = XliffParserFactory::getInstance($xliffVersion, $xliffProprietary, $this->logger); |
|
| 90 | 90 | |
| 91 | 91 | $dom = XmlDomLoader::load( |
| 92 | 92 | $xliffContent, |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | ) |
| 98 | 98 | ); |
| 99 | 99 | |
| 100 | - return $parser->parse( $dom, $xliff ); |
|
| 100 | + return $parser->parse($dom, $xliff); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -109,12 +109,12 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return string |
| 111 | 111 | */ |
| 112 | - private static function forceUft8Encoding( $xliffContent, &$xliff ): string { |
|
| 113 | - $enc = mb_detect_encoding( $xliffContent ); |
|
| 112 | + private static function forceUft8Encoding($xliffContent, &$xliff): string { |
|
| 113 | + $enc = mb_detect_encoding($xliffContent); |
|
| 114 | 114 | |
| 115 | - if ( $enc != 'UTF-8' && $enc != "ASCII" ) { |
|
| 116 | - $xliff[ 'parser-warnings' ][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
| 117 | - $s = mb_convert_encoding( $xliffContent, 'UTF-8', $enc ); |
|
| 115 | + if ($enc != 'UTF-8' && $enc != "ASCII") { |
|
| 116 | + $xliff['parser-warnings'][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
| 117 | + $s = mb_convert_encoding($xliffContent, 'UTF-8', $enc); |
|
| 118 | 118 | $xliffContent = $s !== false ? $s : ""; |
| 119 | 119 | } |
| 120 | 120 | |
@@ -131,28 +131,28 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return mixed|string |
| 133 | 133 | */ |
| 134 | - private static function removeInternalFileTagFromContent( $xliffContent, &$xliff ) { |
|
| 134 | + private static function removeInternalFileTagFromContent($xliffContent, &$xliff) { |
|
| 135 | 135 | $index = 1; |
| 136 | - $a = Strings::preg_split( '|<internal-file[\s>]|i', $xliffContent ); |
|
| 137 | - $tagMatches = count( $a ); |
|
| 136 | + $a = Strings::preg_split('|<internal-file[\s>]|i', $xliffContent); |
|
| 137 | + $tagMatches = count($a); |
|
| 138 | 138 | |
| 139 | 139 | // no match, return original string |
| 140 | - if ( $tagMatches === 1 ) { |
|
| 141 | - return $a[ 0 ]; |
|
| 140 | + if ($tagMatches === 1) { |
|
| 141 | + return $a[0]; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - $b = Strings::preg_split( '|</internal-file>|i', $a[ 1 ] ); |
|
| 145 | - $strippedContent = $a[ 0 ] . $b[ 1 ]; |
|
| 146 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $b[ 0 ] ); |
|
| 144 | + $b = Strings::preg_split('|</internal-file>|i', $a[1]); |
|
| 145 | + $strippedContent = $a[0] . $b[1]; |
|
| 146 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
| 147 | 147 | $index++; |
| 148 | 148 | |
| 149 | 149 | // Sometimes, sdlxliff files can contain more than 2 <internal-file> nodes. |
| 150 | 150 | // In this case loop and extract any other extra <internal-file> node |
| 151 | - for($i=2; $i < $tagMatches; $i++){ |
|
| 152 | - if ( isset( $a[ $i ] ) ) { |
|
| 153 | - $c = Strings::preg_split( '|</internal-file[\s>]|i', $a[ $i ] ); |
|
| 154 | - $strippedContent .= $c[ 1 ]; |
|
| 155 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $c[ 0 ] ); |
|
| 151 | + for ($i = 2; $i < $tagMatches; $i++) { |
|
| 152 | + if (isset($a[$i])) { |
|
| 153 | + $c = Strings::preg_split('|</internal-file[\s>]|i', $a[$i]); |
|
| 154 | + $strippedContent .= $c[1]; |
|
| 155 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
@@ -164,10 +164,10 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return array |
| 166 | 166 | */ |
| 167 | - private static function extractBase64( $base64 ): array { |
|
| 167 | + private static function extractBase64($base64): array { |
|
| 168 | 168 | return [ |
| 169 | 169 | 'form-type' => 'base64', |
| 170 | - 'base64' => trim( str_replace( 'form="base64">', '', $base64 ) ), |
|
| 170 | + 'base64' => trim(str_replace('form="base64">', '', $base64)), |
|
| 171 | 171 | ]; |
| 172 | 172 | } |
| 173 | 173 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return string |
| 189 | 189 | */ |
| 190 | - private static function escapeDataInOriginalMap( string $xliffContent ): string { |
|
| 191 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceSpace' ], $xliffContent ); |
|
| 192 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceXliffTags' ], $xliffContent ); |
|
| 190 | + private static function escapeDataInOriginalMap(string $xliffContent): string { |
|
| 191 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
| 192 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
| 193 | 193 | |
| 194 | 194 | return $xliffContent; |
| 195 | 195 | } |
@@ -208,15 +208,15 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @return string |
| 210 | 210 | */ |
| 211 | - private static function insertPlaceholderInEmptyTags( $xliffContent ): string { |
|
| 212 | - preg_match_all( '|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches ); |
|
| 211 | + private static function insertPlaceholderInEmptyTags($xliffContent): string { |
|
| 212 | + preg_match_all('|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches); |
|
| 213 | 213 | |
| 214 | - if ( !empty( $emptyTagMatches[ 0 ] ) ) { |
|
| 215 | - foreach ( $emptyTagMatches[ 0 ] as $index => $emptyTagMatch ) { |
|
| 216 | - $matchedTag = $emptyTagMatches[ 1 ][ $index ]; |
|
| 214 | + if (!empty($emptyTagMatches[0])) { |
|
| 215 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
| 216 | + $matchedTag = $emptyTagMatches[1][$index]; |
|
| 217 | 217 | $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
| 218 | - $replacedTag = str_replace( '</' . $matchedTag . '>', $subst, $emptyTagMatch ); |
|
| 219 | - $xliffContent = str_replace( $emptyTagMatch, $replacedTag, $xliffContent ); |
|
| 218 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
| 219 | + $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
|
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
@@ -230,12 +230,12 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @return string |
| 232 | 232 | */ |
| 233 | - private static function replaceSpace( array $matches ): string { |
|
| 234 | - $content = str_replace( ' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[ 2 ] ); |
|
| 235 | - $content = str_replace( '\n', Placeholder::NEW_LINE_PLACEHOLDER, $content ); |
|
| 236 | - $content = str_replace( '\t', Placeholder::TAB_PLACEHOLDER, $content ); |
|
| 233 | + private static function replaceSpace(array $matches): string { |
|
| 234 | + $content = str_replace(' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[2]); |
|
| 235 | + $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
|
| 236 | + $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
|
| 237 | 237 | |
| 238 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
| 238 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
@@ -243,15 +243,15 @@ discard block |
||
| 243 | 243 | * |
| 244 | 244 | * @return string |
| 245 | 245 | */ |
| 246 | - private static function replaceXliffTags( array $matches ): string { |
|
| 246 | + private static function replaceXliffTags(array $matches): string { |
|
| 247 | 247 | $xliffTags = XliffTags::$tags; |
| 248 | - $content = $matches[ 2 ]; |
|
| 248 | + $content = $matches[2]; |
|
| 249 | 249 | |
| 250 | - foreach ( $xliffTags as $xliffTag ) { |
|
| 251 | - $content = preg_replace( '|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
| 252 | - $content = preg_replace( '|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
| 250 | + foreach ($xliffTags as $xliffTag) { |
|
| 251 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
| 252 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
| 255 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
| 256 | 256 | } |
| 257 | 257 | } |