Passed
Push — master ( e5c479...71c71c )
by Mauro
03:36
created
src/XliffParser/XliffParserV1.php 1 patch
Spacing   +115 added lines, -115 removed lines patch added patch discarded remove patch
@@ -16,41 +16,41 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.