Passed
Push — master ( a562ef...e5c479 )
by Mauro
09:37
created
src/XliffParser/XliffParserV1.php 1 patch
Spacing   +95 added lines, -95 removed lines patch added patch discarded remove patch
@@ -16,33 +16,33 @@  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 ) {
36
-                if ( $body->nodeName === 'body' ) {
37
-                    foreach ( $body->childNodes as $childNode ) {
38
-                        $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j );
35
+            foreach ($file->childNodes as $body) {
36
+                if ($body->nodeName === 'body') {
37
+                    foreach ($body->childNodes as $childNode) {
38
+                        $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j);
39 39
                     }
40 40
 
41 41
                     // trans-unit re-count check
42
-                    $totalTransUnitsId  = count( $transUnitIdArrayForUniquenessCheck );
43
-                    $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) );
44
-                    if ( $totalTransUnitsId != $transUnitsUniqueId ) {
45
-                        throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 );
42
+                    $totalTransUnitsId  = count($transUnitIdArrayForUniquenessCheck);
43
+                    $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck));
44
+                    if ($totalTransUnitsId != $transUnitsUniqueId) {
45
+                        throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400);
46 46
                     }
47 47
 
48 48
                     $i++;
@@ -58,50 +58,50 @@  discard block
 block discarded – undo
58 58
      *
59 59
      * @return array
60 60
      */
61
-    private function extractMetadata( DOMElement $file ): array {
61
+    private function extractMetadata(DOMElement $file): array {
62 62
         $metadata   = [];
63 63
         $customAttr = [];
64 64
 
65 65
         /** @var DOMAttr $attribute */
66
-        foreach ( $file->attributes as $attribute ) {
67
-            switch ( $attribute->localName ) {
66
+        foreach ($file->attributes as $attribute) {
67
+            switch ($attribute->localName) {
68 68
                 // original
69 69
                 case 'original':
70
-                    $metadata[ 'original' ] = $attribute->value;
70
+                    $metadata['original'] = $attribute->value;
71 71
                     break;
72 72
 
73 73
                 // source-language
74 74
                 case 'source-language':
75
-                    $metadata[ 'source-language' ] = $attribute->value;
75
+                    $metadata['source-language'] = $attribute->value;
76 76
                     break;
77 77
 
78 78
                 // data-type
79 79
                 case 'datatype':
80
-                    $metadata[ 'data-type' ] = $attribute->value;
80
+                    $metadata['data-type'] = $attribute->value;
81 81
                     break;
82 82
 
83 83
                 // target-language
84 84
                 case 'target-language':
85
-                    $metadata[ 'target-language' ] = $attribute->value;
85
+                    $metadata['target-language'] = $attribute->value;
86 86
                     break;
87 87
             }
88 88
 
89 89
             // Custom MateCat x-Attribute
90
-            preg_match( '|x-(.*?)|si', $attribute->localName, $temp );
91
-            if ( isset( $temp[ 1 ] ) ) {
92
-                $customAttr[ $attribute->localName ] = $attribute->value;
90
+            preg_match('|x-(.*?)|si', $attribute->localName, $temp);
91
+            if (isset($temp[1])) {
92
+                $customAttr[$attribute->localName] = $attribute->value;
93 93
             }
94
-            unset( $temp );
94
+            unset($temp);
95 95
 
96 96
             // Custom MateCat namespace Attribute mtc:
97
-            preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp );
98
-            if ( isset( $temp[ 1 ] ) ) {
99
-                $customAttr[ $attribute->nodeName ] = $attribute->value;
97
+            preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp);
98
+            if (isset($temp[1])) {
99
+                $customAttr[$attribute->nodeName] = $attribute->value;
100 100
             }
101
-            unset( $temp );
101
+            unset($temp);
102 102
 
103
-            if ( !empty( $customAttr ) ) {
104
-                $metadata[ 'custom' ] = $customAttr;
103
+            if (!empty($customAttr)) {
104
+                $metadata['custom'] = $customAttr;
105 105
             }
106 106
         }
107 107
 
@@ -113,16 +113,16 @@  discard block
 block discarded – undo
113 113
      *
114 114
      * @return array
115 115
      */
116
-    private function extractReference( DOMElement $file ): array {
116
+    private function extractReference(DOMElement $file): array {
117 117
         $reference = [];
118 118
 
119 119
         $order = 0;
120
-        foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) {
120
+        foreach ($file->getElementsByTagName('reference') as $ref) {
121 121
             /** @var DOMNode $childNode */
122
-            foreach ( $ref->childNodes as $childNode ) {
123
-                if ( $childNode->nodeName === 'internal-file' ) {
124
-                    $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue;
125
-                    $reference[ $order ][ 'base64' ]    = trim( $childNode->nodeValue );
122
+            foreach ($ref->childNodes as $childNode) {
123
+                if ($childNode->nodeName === 'internal-file') {
124
+                    $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue;
125
+                    $reference[$order]['base64']    = trim($childNode->nodeValue);
126 126
                     $order++;
127 127
                 }
128 128
             }
@@ -144,61 +144,61 @@  discard block
 block discarded – undo
144 144
      *
145 145
      * @throws Exception
146 146
      */
147
-    protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ) {
147
+    protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []) {
148 148
         // metadata
149
-        $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck );
149
+        $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck);
150 150
 
151 151
         // notes
152
-        $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit );
152
+        $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit);
153 153
 
154 154
         // content
155 155
         /** @var DOMElement $childNode */
156
-        foreach ( $transUnit->childNodes as $childNode ) {
156
+        foreach ($transUnit->childNodes as $childNode) {
157 157
             // source
158
-            if ( $childNode->nodeName === 'source' ) {
159
-                $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode );
158
+            if ($childNode->nodeName === 'source') {
159
+                $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode);
160 160
             }
161 161
 
162 162
             // seg-source
163
-            if ( $childNode->nodeName === 'seg-source' ) {
164
-                $rawSegment                                                     = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ];
165
-                $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment );
163
+            if ($childNode->nodeName === 'seg-source') {
164
+                $rawSegment                                                     = $output['files'][$i]['trans-units'][$j]['source']['raw-content'];
165
+                $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment);
166 166
             }
167 167
 
168 168
             // target
169
-            if ( $childNode->nodeName === 'target' ) {
170
-                $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode );
169
+            if ($childNode->nodeName === 'target') {
170
+                $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode);
171 171
 
172 172
                 // seg-target
173
-                $targetRawContent = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ] ?? null;
174
-                $segSource        = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] ?? null;
173
+                $targetRawContent = $output['files'][$i]['trans-units'][$j]['target']['raw-content'] ?? null;
174
+                $segSource        = $output['files'][$i]['trans-units'][$j]['seg-source'] ?? null;
175 175
 
176
-                if ( !empty( $targetRawContent ) and isset( $segSource ) and count( $segSource ) > 0 ) {
177
-                    $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ]                = $this->extractContentWithMarksAndExtTags( $dom, $childNode );
178
-                    $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ][ 'attr' ] = $this->extractTagAttributes( $childNode );
176
+                if (!empty($targetRawContent) and isset($segSource) and count($segSource) > 0) {
177
+                    $output['files'][$i]['trans-units'][$j]['seg-target']                = $this->extractContentWithMarksAndExtTags($dom, $childNode);
178
+                    $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode);
179 179
                 }
180 180
             }
181 181
 
182 182
             // locked
183
-            if ( $childNode->nodeName === 'sdl:seg' ) {
184
-                $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode );
183
+            if ($childNode->nodeName === 'sdl:seg') {
184
+                $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode);
185 185
             }
186 186
         }
187 187
 
188 188
         // context-group
189
-        if ( !empty( $contextGroups ) ) {
190
-            foreach ( $contextGroups as $contextGroup ) {
191
-                $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup );
189
+        if (!empty($contextGroups)) {
190
+            foreach ($contextGroups as $contextGroup) {
191
+                $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup);
192 192
             }
193 193
         }
194 194
 
195
-        foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) {
196
-            $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup );
195
+        foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) {
196
+            $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup);
197 197
         }
198 198
 
199 199
         // alt-trans
200
-        foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) {
201
-            $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans );
200
+        foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) {
201
+            $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans);
202 202
         }
203 203
 
204 204
         $j++;
@@ -211,41 +211,41 @@  discard block
 block discarded – undo
211 211
      * @return array
212 212
      * @throws Exception
213 213
      */
214
-    private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ): array {
214
+    private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck): array {
215 215
         $metadata = [];
216 216
 
217 217
         // id MUST NOT be null
218
-        if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) {
219
-            throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 );
218
+        if (null === $transUnit->attributes->getNamedItem('id')) {
219
+            throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400);
220 220
         }
221 221
 
222 222
         /**
223 223
          * @var DOMAttr $element
224 224
          */
225
-        foreach ( $transUnit->attributes as $element ) {
225
+        foreach ($transUnit->attributes as $element) {
226 226
 
227
-            if ( $element->nodeName === "id" ) {
227
+            if ($element->nodeName === "id") {
228 228
 
229 229
                 $id = $element->nodeValue;
230 230
 
231
-                if ( strlen( $id ) > 100 ) {
232
-                    throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 );
231
+                if (strlen($id) > 100) {
232
+                    throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400);
233 233
                 }
234 234
 
235 235
                 $transUnitIdArrayForUniquenessCheck[] = $id;
236
-                $metadata[ 'id' ]                     = $id;
236
+                $metadata['id']                     = $id;
237 237
 
238
-            } elseif ( $element->nodeName === "approved" ) {
238
+            } elseif ($element->nodeName === "approved") {
239 239
                 // approved as BOOLEAN
240 240
                 // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved
241
-                $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN );
242
-            } elseif ( $element->nodeName === "maxwidth" ) {
241
+                $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN);
242
+            } elseif ($element->nodeName === "maxwidth") {
243 243
                 // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character
244 244
                 // we duplicate the info to allow Xliff V1 and V2 to work the same
245
-                $metadata[ 'sizeRestriction' ]  = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT );
246
-                $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT );
245
+                $metadata['sizeRestriction']  = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT);
246
+                $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT);
247 247
             } else {
248
-                $metadata[ $element->nodeName ] = $element->nodeValue;
248
+                $metadata[$element->nodeName] = $element->nodeValue;
249 249
             }
250 250
 
251 251
         }
@@ -260,19 +260,19 @@  discard block
 block discarded – undo
260 260
      * @return array
261 261
      * @throws Exception
262 262
      */
263
-    private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ): array {
263
+    private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit): array {
264 264
         $notes = [];
265
-        foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) {
265
+        foreach ($transUnit->getElementsByTagName('note') as $note) {
266 266
 
267
-            $noteValue = $this->extractTagContent( $dom, $note );
267
+            $noteValue = $this->extractTagContent($dom, $note);
268 268
 
269
-            if ( '' !== $noteValue ) {
269
+            if ('' !== $noteValue) {
270 270
 
271
-                $extractedNote = $this->JSONOrRawContentArray( $noteValue );
271
+                $extractedNote = $this->JSONOrRawContentArray($noteValue);
272 272
 
273 273
                 // extract all the attributes
274
-                foreach ( $note->attributes as $attribute ) {
275
-                    $extractedNote[ $attribute->name ] = $attribute->value;
274
+                foreach ($note->attributes as $attribute) {
275
+                    $extractedNote[$attribute->name] = $attribute->value;
276 276
                 }
277 277
 
278 278
                 $notes[] = $extractedNote;
@@ -288,14 +288,14 @@  discard block
 block discarded – undo
288 288
      *
289 289
      * @return array
290 290
      */
291
-    private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ): array {
291
+    private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup): array {
292 292
         $cg           = [];
293
-        $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup );
293
+        $cg['attr'] = $this->extractTagAttributes($contextGroup);
294 294
 
295 295
         /** @var DOMNode $context */
296
-        foreach ( $contextGroup->childNodes as $context ) {
297
-            if ( $context->nodeName === 'context' ) {
298
-                $cg[ 'contexts' ][] = $this->extractContent( $dom, $context );
296
+        foreach ($contextGroup->childNodes as $context) {
297
+            if ($context->nodeName === 'context') {
298
+                $cg['contexts'][] = $this->extractContent($dom, $context);
299 299
             }
300 300
         }
301 301
 
@@ -307,16 +307,16 @@  discard block
 block discarded – undo
307 307
      *
308 308
      * @return array
309 309
      */
310
-    private function extractTransUnitAltTrans( DOMElement $altTrans ) {
310
+    private function extractTransUnitAltTrans(DOMElement $altTrans) {
311 311
         $at           = [];
312
-        $at[ 'attr' ] = $this->extractTagAttributes( $altTrans );
312
+        $at['attr'] = $this->extractTagAttributes($altTrans);
313 313
 
314
-        if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) {
315
-            $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue;
314
+        if ($altTrans->getElementsByTagName('source')->length > 0) {
315
+            $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue;
316 316
         }
317 317
 
318
-        if ( $altTrans->getElementsByTagName( 'target' ) ) {
319
-            $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue;
318
+        if ($altTrans->getElementsByTagName('target')) {
319
+            $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue;
320 320
         }
321 321
 
322 322
         return $at;
@@ -327,7 +327,7 @@  discard block
 block discarded – undo
327 327
      *
328 328
      * @return bool
329 329
      */
330
-    private function extractLocked( DOMElement $locked ) {
331
-        return null !== $locked->getAttribute( 'locked' );
330
+    private function extractLocked(DOMElement $locked) {
331
+        return null !== $locked->getAttribute('locked');
332 332
     }
333 333
 }
Please login to merge, or discard this patch.