@@ -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' ) { |
|
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 = iconv( $enc, 'UTF-8', $xliffContent ); |
|
115 | + if ($enc !== 'UTF-8') { |
|
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 = iconv($enc, 'UTF-8', $xliffContent); |
|
118 | 118 | $xliffContent = $s !== false ? $s : ""; |
119 | 119 | } |
120 | 120 | |
@@ -131,24 +131,24 @@ 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 ); |
|
136 | + $a = Strings::preg_split('|<internal-file[\s>]|i', $xliffContent); |
|
137 | 137 | |
138 | 138 | // no match, return original string |
139 | - if ( count( $a ) === 1 ) { |
|
140 | - return $a[ 0 ]; |
|
139 | + if (count($a) === 1) { |
|
140 | + return $a[0]; |
|
141 | 141 | } |
142 | 142 | |
143 | - $b = Strings::preg_split( '|</internal-file>|i', $a[ 1 ] ); |
|
144 | - $strippedContent = $a[ 0 ] . $b[ 1 ]; |
|
145 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $b[ 0 ] ); |
|
143 | + $b = Strings::preg_split('|</internal-file>|i', $a[1]); |
|
144 | + $strippedContent = $a[0] . $b[1]; |
|
145 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
146 | 146 | $index++; |
147 | 147 | |
148 | - if ( isset( $a[ 2 ] ) ) { |
|
149 | - $c = Strings::preg_split( '|</internal-file[\s>]|i', $a[ 2 ] ); |
|
150 | - $strippedContent .= $c[ 1 ]; |
|
151 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $c[ 0 ] ); |
|
148 | + if (isset($a[2])) { |
|
149 | + $c = Strings::preg_split('|</internal-file[\s>]|i', $a[2]); |
|
150 | + $strippedContent .= $c[1]; |
|
151 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | return $strippedContent; |
@@ -159,10 +159,10 @@ discard block |
||
159 | 159 | * |
160 | 160 | * @return array |
161 | 161 | */ |
162 | - private static function extractBase64( $base64 ): array { |
|
162 | + private static function extractBase64($base64): array { |
|
163 | 163 | return [ |
164 | 164 | 'form-type' => 'base64', |
165 | - 'base64' => trim( str_replace( 'form="base64">', '', $base64 ) ), |
|
165 | + 'base64' => trim(str_replace('form="base64">', '', $base64)), |
|
166 | 166 | ]; |
167 | 167 | } |
168 | 168 | |
@@ -182,9 +182,9 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return string |
184 | 184 | */ |
185 | - private static function escapeDataInOriginalMap( string $xliffContent ): string { |
|
186 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceSpace' ], $xliffContent ); |
|
187 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceXliffTags' ], $xliffContent ); |
|
185 | + private static function escapeDataInOriginalMap(string $xliffContent): string { |
|
186 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
187 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
188 | 188 | |
189 | 189 | return $xliffContent; |
190 | 190 | } |
@@ -203,15 +203,15 @@ discard block |
||
203 | 203 | * |
204 | 204 | * @return string |
205 | 205 | */ |
206 | - private static function insertPlaceholderInEmptyTags( $xliffContent ): string { |
|
207 | - preg_match_all( '|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches ); |
|
206 | + private static function insertPlaceholderInEmptyTags($xliffContent): string { |
|
207 | + preg_match_all('|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches); |
|
208 | 208 | |
209 | - if ( !empty( $emptyTagMatches[ 0 ] ) ) { |
|
210 | - foreach ( $emptyTagMatches[ 0 ] as $index => $emptyTagMatch ) { |
|
211 | - $matchedTag = $emptyTagMatches[ 1 ][ $index ]; |
|
209 | + if (!empty($emptyTagMatches[0])) { |
|
210 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
211 | + $matchedTag = $emptyTagMatches[1][$index]; |
|
212 | 212 | $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
213 | - $replacedTag = str_replace( '</' . $matchedTag . '>', $subst, $emptyTagMatch ); |
|
214 | - $xliffContent = str_replace( $emptyTagMatch, $replacedTag, $xliffContent ); |
|
213 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
214 | + $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
@@ -225,12 +225,12 @@ discard block |
||
225 | 225 | * |
226 | 226 | * @return string |
227 | 227 | */ |
228 | - private static function replaceSpace( array $matches ): string { |
|
229 | - $content = str_replace( ' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[ 2 ] ); |
|
230 | - $content = str_replace( '\n', Placeholder::NEW_LINE_PLACEHOLDER, $content ); |
|
231 | - $content = str_replace( '\t', Placeholder::TAB_PLACEHOLDER, $content ); |
|
228 | + private static function replaceSpace(array $matches): string { |
|
229 | + $content = str_replace(' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[2]); |
|
230 | + $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
|
231 | + $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
|
232 | 232 | |
233 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
233 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -238,15 +238,15 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @return string |
240 | 240 | */ |
241 | - private static function replaceXliffTags( array $matches ): string { |
|
241 | + private static function replaceXliffTags(array $matches): string { |
|
242 | 242 | $xliffTags = XliffTags::$tags; |
243 | - $content = $matches[ 2 ]; |
|
243 | + $content = $matches[2]; |
|
244 | 244 | |
245 | - foreach ( $xliffTags as $xliffTag ) { |
|
246 | - $content = preg_replace( '|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
247 | - $content = preg_replace( '|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
245 | + foreach ($xliffTags as $xliffTag) { |
|
246 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
247 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
248 | 248 | } |
249 | 249 | |
250 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
250 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
251 | 251 | } |
252 | 252 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * @param string|null $xliffProprietary |
39 | 39 | * @param LoggerInterface|null $logger |
40 | 40 | */ |
41 | - public function __construct( int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null ) { |
|
41 | + public function __construct(int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null) { |
|
42 | 42 | $this->xliffVersion = $xliffVersion; |
43 | 43 | $this->logger = $logger; |
44 | 44 | $this->xliffProprietary = $xliffProprietary; |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * @return string |
49 | 49 | */ |
50 | 50 | protected function getTuTagName(): string { |
51 | - return ( $this->xliffVersion === 1 ) ? 'trans-unit' : 'unit'; |
|
51 | + return ($this->xliffVersion === 1) ? 'trans-unit' : 'unit'; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @return array |
59 | 59 | */ |
60 | - abstract public function parse( DOMDocument $dom, ?array $output = [] ): array; |
|
60 | + abstract public function parse(DOMDocument $dom, ?array $output = []): array; |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Extract trans-unit content from the current node |
@@ -71,17 +71,17 @@ discard block |
||
71 | 71 | * @param array|null $contextGroups |
72 | 72 | * @param int|null $recursionLevel |
73 | 73 | */ |
74 | - protected function extractTuFromNode( DOMNode $childNode, array &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [], ?int $recursionLevel = 0 ) { |
|
74 | + protected function extractTuFromNode(DOMNode $childNode, array &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [], ?int $recursionLevel = 0) { |
|
75 | 75 | |
76 | - if ( $childNode->nodeType != XML_ELEMENT_NODE ) { |
|
76 | + if ($childNode->nodeType != XML_ELEMENT_NODE) { |
|
77 | 77 | return; |
78 | 78 | } |
79 | 79 | |
80 | - if ( $childNode->nodeName === 'group' ) { |
|
80 | + if ($childNode->nodeName === 'group') { |
|
81 | 81 | |
82 | 82 | // add nested context-groups |
83 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
84 | - if ( $nestedChildNode->nodeName === 'context-group' ) { |
|
83 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
84 | + if ($nestedChildNode->nodeName === 'context-group') { |
|
85 | 85 | $contextGroups[] = $nestedChildNode; |
86 | 86 | } |
87 | 87 | } |
@@ -89,23 +89,23 @@ discard block |
||
89 | 89 | // avoid infinite recursion |
90 | 90 | $recursionLevel++; |
91 | 91 | |
92 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
92 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
93 | 93 | |
94 | 94 | // nested groups |
95 | - if ( $nestedChildNode->nodeName === 'group' ) { |
|
95 | + if ($nestedChildNode->nodeName === 'group') { |
|
96 | 96 | |
97 | - if ( $recursionLevel < self::MAX_GROUP_RECURSION_LEVEL ) { |
|
98 | - $this->extractTuFromNode( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel ); |
|
97 | + if ($recursionLevel < self::MAX_GROUP_RECURSION_LEVEL) { |
|
98 | + $this->extractTuFromNode($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel); |
|
99 | 99 | } else { |
100 | - throw new OverflowException( "Maximum tag group nesting level of '" . self::MAX_GROUP_RECURSION_LEVEL . "' reached, aborting!" ); |
|
100 | + throw new OverflowException("Maximum tag group nesting level of '" . self::MAX_GROUP_RECURSION_LEVEL . "' reached, aborting!"); |
|
101 | 101 | } |
102 | 102 | |
103 | - } elseif ( $nestedChildNode->nodeName === $this->getTuTagName() ) { |
|
104 | - $this->extractTransUnit( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
103 | + } elseif ($nestedChildNode->nodeName === $this->getTuTagName()) { |
|
104 | + $this->extractTransUnit($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
105 | 105 | } |
106 | 106 | } |
107 | - } elseif ( $childNode->nodeName === $this->getTuTagName() ) { |
|
108 | - $this->extractTransUnit( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
107 | + } elseif ($childNode->nodeName === $this->getTuTagName()) { |
|
108 | + $this->extractTransUnit($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
109 | 109 | } |
110 | 110 | } |
111 | 111 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | * |
123 | 123 | * @return mixed |
124 | 124 | */ |
125 | - abstract protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ); |
|
125 | + abstract protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []); |
|
126 | 126 | |
127 | 127 | /** |
128 | 128 | * @param DOMDocument $dom |
@@ -130,10 +130,10 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @return array |
132 | 132 | */ |
133 | - protected function extractContent( DOMDocument $dom, DOMNode $node ): array { |
|
133 | + protected function extractContent(DOMDocument $dom, DOMNode $node): array { |
|
134 | 134 | return [ |
135 | - 'raw-content' => $this->extractTagContent( $dom, $node ), |
|
136 | - 'attr' => $this->extractTagAttributes( $node ) |
|
135 | + 'raw-content' => $this->extractTagContent($dom, $node), |
|
136 | + 'attr' => $this->extractTagAttributes($node) |
|
137 | 137 | ]; |
138 | 138 | } |
139 | 139 | |
@@ -150,12 +150,12 @@ discard block |
||
150 | 150 | * |
151 | 151 | * @return array |
152 | 152 | */ |
153 | - protected function extractTagAttributes( DOMNode $element ): array { |
|
153 | + protected function extractTagAttributes(DOMNode $element): array { |
|
154 | 154 | $tagAttributes = []; |
155 | 155 | |
156 | - if ( $element->hasAttributes() ) { |
|
157 | - foreach ( $element->attributes as $attr ) { |
|
158 | - $tagAttributes[ $attr->nodeName ] = $attr->nodeValue; |
|
156 | + if ($element->hasAttributes()) { |
|
157 | + foreach ($element->attributes as $attr) { |
|
158 | + $tagAttributes[$attr->nodeName] = $attr->nodeValue; |
|
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
@@ -170,17 +170,17 @@ discard block |
||
170 | 170 | * |
171 | 171 | * @return string |
172 | 172 | */ |
173 | - protected function extractTagContent( DOMDocument $dom, DOMNode $element ): string { |
|
173 | + protected function extractTagContent(DOMDocument $dom, DOMNode $element): string { |
|
174 | 174 | $childNodes = $element->hasChildNodes(); |
175 | 175 | $extractedContent = ''; |
176 | 176 | |
177 | - if ( !empty( $childNodes ) ) { |
|
178 | - foreach ( $element->childNodes as $node ) { |
|
179 | - $extractedContent .= Emoji::toEntity( Strings::fixNonWellFormedXml( $dom->saveXML( $node ) ) ); |
|
177 | + if (!empty($childNodes)) { |
|
178 | + foreach ($element->childNodes as $node) { |
|
179 | + $extractedContent .= Emoji::toEntity(Strings::fixNonWellFormedXml($dom->saveXML($node))); |
|
180 | 180 | } |
181 | 181 | } |
182 | 182 | |
183 | - return str_replace( Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent ); |
|
183 | + return str_replace(Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | /** |
@@ -191,45 +191,45 @@ discard block |
||
191 | 191 | * |
192 | 192 | * @return array |
193 | 193 | */ |
194 | - protected function extractContentWithMarksAndExtTags( DOMDocument $dom, DOMElement $childNode ): array { |
|
194 | + protected function extractContentWithMarksAndExtTags(DOMDocument $dom, DOMElement $childNode): array { |
|
195 | 195 | $source = []; |
196 | 196 | |
197 | 197 | // example: |
198 | 198 | // <g id="1"><mrk mid="0" mtype="seg">An English string with g tags</mrk></g> |
199 | - $raw = $this->extractTagContent( $dom, $childNode ); |
|
199 | + $raw = $this->extractTagContent($dom, $childNode); |
|
200 | 200 | |
201 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
201 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
202 | 202 | |
203 | 203 | $mi = 0; |
204 | - while ( isset( $markers[ $mi + 1 ] ) ) { |
|
205 | - unset( $mid ); |
|
204 | + while (isset($markers[$mi + 1])) { |
|
205 | + unset($mid); |
|
206 | 206 | |
207 | - preg_match( '|mid\s?=\s?["\'](.*?)["\']|si', $markers[ $mi + 1 ], $mid ); |
|
207 | + preg_match('|mid\s?=\s?["\'](.*?)["\']|si', $markers[$mi + 1], $mid); |
|
208 | 208 | |
209 | 209 | // if it's a Trados file the trailing spaces after </mrk> are meaningful |
210 | 210 | // so we add them to |
211 | 211 | $trailingSpaces = ''; |
212 | - if ( $this->xliffProprietary === 'trados' ) { |
|
213 | - preg_match_all( '/<\/mrk>[\s]+/iu', $markers[ $mi + 1 ], $trailingSpacesMatches ); |
|
212 | + if ($this->xliffProprietary === 'trados') { |
|
213 | + preg_match_all('/<\/mrk>[\s]+/iu', $markers[$mi + 1], $trailingSpacesMatches); |
|
214 | 214 | |
215 | - if ( isset( $trailingSpacesMatches[ 0 ] ) && count( $trailingSpacesMatches[ 0 ] ) > 0 ) { |
|
216 | - foreach ( $trailingSpacesMatches[ 0 ] as $match ) { |
|
217 | - $trailingSpaces = str_replace( '</mrk>', '', $match ); |
|
215 | + if (isset($trailingSpacesMatches[0]) && count($trailingSpacesMatches[0]) > 0) { |
|
216 | + foreach ($trailingSpacesMatches[0] as $match) { |
|
217 | + $trailingSpaces = str_replace('</mrk>', '', $match); |
|
218 | 218 | } |
219 | 219 | } |
220 | 220 | } |
221 | 221 | |
222 | 222 | //re-build the mrk tag after the split |
223 | - $originalMark = trim( '<mrk ' . $markers[ $mi + 1 ] ); |
|
223 | + $originalMark = trim('<mrk ' . $markers[$mi + 1]); |
|
224 | 224 | |
225 | - $mark_string = preg_replace( '#^<mrk\s[^>]+>(.*)#', '$1', $originalMark ); // at this point we have: ---> 'Test </mrk> </g>>' |
|
226 | - $mark_content = preg_split( '#</mrk>#si', $mark_string ); |
|
225 | + $mark_string = preg_replace('#^<mrk\s[^>]+>(.*)#', '$1', $originalMark); // at this point we have: ---> 'Test </mrk> </g>>' |
|
226 | + $mark_content = preg_split('#</mrk>#si', $mark_string); |
|
227 | 227 | |
228 | 228 | $sourceArray = [ |
229 | - 'mid' => ( isset( $mid[ 1 ] ) ) ? $mid[ 1 ] : $mi, |
|
230 | - 'ext-prec-tags' => ( $mi == 0 ? $markers[ 0 ] : "" ), |
|
231 | - 'raw-content' => ( isset( $mark_content[ 0 ] ) ) ? $mark_content[ 0 ] . $trailingSpaces : '', |
|
232 | - 'ext-succ-tags' => ( isset( $mark_content[ 1 ] ) ) ? $mark_content[ 1 ] : '', |
|
229 | + 'mid' => (isset($mid[1])) ? $mid[1] : $mi, |
|
230 | + 'ext-prec-tags' => ($mi == 0 ? $markers[0] : ""), |
|
231 | + 'raw-content' => (isset($mark_content[0])) ? $mark_content[0] . $trailingSpaces : '', |
|
232 | + 'ext-succ-tags' => (isset($mark_content[1])) ? $mark_content[1] : '', |
|
233 | 233 | ]; |
234 | 234 | |
235 | 235 | $source[] = $sourceArray; |
@@ -245,12 +245,12 @@ discard block |
||
245 | 245 | * |
246 | 246 | * @return array |
247 | 247 | */ |
248 | - protected function getDataRefMap( array $originalData ): array { |
|
248 | + protected function getDataRefMap(array $originalData): array { |
|
249 | 249 | // dataRef map |
250 | 250 | $dataRefMap = []; |
251 | - foreach ( $originalData as $datum ) { |
|
252 | - if ( isset( $datum[ 'attr' ][ 'id' ] ) ) { |
|
253 | - $dataRefMap[ $datum[ 'attr' ][ 'id' ] ] = $datum[ 'raw-content' ]; |
|
251 | + foreach ($originalData as $datum) { |
|
252 | + if (isset($datum['attr']['id'])) { |
|
253 | + $dataRefMap[$datum['attr']['id']] = $datum['raw-content']; |
|
254 | 254 | } |
255 | 255 | } |
256 | 256 | |
@@ -262,10 +262,10 @@ discard block |
||
262 | 262 | * |
263 | 263 | * @return bool |
264 | 264 | */ |
265 | - protected function stringContainsMarks( $raw ): bool { |
|
266 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
265 | + protected function stringContainsMarks($raw): bool { |
|
266 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
267 | 267 | |
268 | - return isset( $markers[ 1 ] ); |
|
268 | + return isset($markers[1]); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | * @return array |
276 | 276 | * @throws Exception |
277 | 277 | */ |
278 | - protected function JSONOrRawContentArray( $noteValue, ?bool $escapeStrings = true ): array { |
|
278 | + protected function JSONOrRawContentArray($noteValue, ?bool $escapeStrings = true): array { |
|
279 | 279 | // |
280 | 280 | // convert double escaped entites |
281 | 281 | // |
@@ -285,17 +285,17 @@ discard block |
||
285 | 285 | // &amp; ---> & |
286 | 286 | // &apos ---> ' |
287 | 287 | // |
288 | - if ( Strings::isADoubleEscapedEntity( $noteValue ) ) { |
|
289 | - $noteValue = Strings::htmlspecialchars_decode( $noteValue, true ); |
|
288 | + if (Strings::isADoubleEscapedEntity($noteValue)) { |
|
289 | + $noteValue = Strings::htmlspecialchars_decode($noteValue, true); |
|
290 | 290 | } else { |
291 | 291 | // for non escaped entities $escapeStrings is always true for security reasons |
292 | 292 | $escapeStrings = true; |
293 | 293 | } |
294 | 294 | |
295 | - if ( Strings::isJSON( $noteValue ) ) { |
|
296 | - return [ 'json' => Strings::cleanCDATA( $noteValue ) ]; |
|
295 | + if (Strings::isJSON($noteValue)) { |
|
296 | + return ['json' => Strings::cleanCDATA($noteValue)]; |
|
297 | 297 | } |
298 | 298 | |
299 | - return [ 'raw-content' => Strings::fixNonWellFormedXml( $noteValue, $escapeStrings ) ]; |
|
299 | + return ['raw-content' => Strings::fixNonWellFormedXml($noteValue, $escapeStrings)]; |
|
300 | 300 | } |
301 | 301 | } |
@@ -16,33 +16,33 @@ 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 ) { |
|
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 |
||
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 |
||
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,60 +144,60 @@ discard block |
||
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' ]; |
|
174 | - $segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ]; |
|
175 | - if ( !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) { |
|
176 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
177 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ][ 'attr' ] = $this->extractTagAttributes( $childNode ); |
|
173 | + $targetRawContent = @$output['files'][$i]['trans-units'][$j]['target']['raw-content']; |
|
174 | + $segSource = @$output['files'][$i]['trans-units'][$j]['seg-source']; |
|
175 | + if (!empty($targetRawContent) && isset($segSource) && count($segSource) > 0) { |
|
176 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
177 | + $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode); |
|
178 | 178 | } |
179 | 179 | } |
180 | 180 | |
181 | 181 | // locked |
182 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
183 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
182 | + if ($childNode->nodeName === 'sdl:seg') { |
|
183 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
184 | 184 | } |
185 | 185 | } |
186 | 186 | |
187 | 187 | // context-group |
188 | - if ( !empty( $contextGroups ) ) { |
|
189 | - foreach ( $contextGroups as $contextGroup ) { |
|
190 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
188 | + if (!empty($contextGroups)) { |
|
189 | + foreach ($contextGroups as $contextGroup) { |
|
190 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
195 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
194 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
195 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | // alt-trans |
199 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
200 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
199 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
200 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | $j++; |
@@ -210,41 +210,41 @@ discard block |
||
210 | 210 | * @return array |
211 | 211 | * @throws Exception |
212 | 212 | */ |
213 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ): array { |
|
213 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck): array { |
|
214 | 214 | $metadata = []; |
215 | 215 | |
216 | 216 | // id MUST NOT be null |
217 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
218 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
217 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
218 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
222 | 222 | * @var DOMAttr $element |
223 | 223 | */ |
224 | - foreach ( $transUnit->attributes as $element ) { |
|
224 | + foreach ($transUnit->attributes as $element) { |
|
225 | 225 | |
226 | - if ( $element->nodeName === "id" ) { |
|
226 | + if ($element->nodeName === "id") { |
|
227 | 227 | |
228 | 228 | $id = $element->nodeValue; |
229 | 229 | |
230 | - if ( strlen( $id ) > 100 ) { |
|
231 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
230 | + if (strlen($id) > 100) { |
|
231 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | $transUnitIdArrayForUniquenessCheck[] = $id; |
235 | - $metadata[ 'id' ] = $id; |
|
235 | + $metadata['id'] = $id; |
|
236 | 236 | |
237 | - } elseif ( $element->nodeName === "approved" ) { |
|
237 | + } elseif ($element->nodeName === "approved") { |
|
238 | 238 | // approved as BOOLEAN |
239 | 239 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
240 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
241 | - } elseif ( $element->nodeName === "maxwidth" ) { |
|
240 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
241 | + } elseif ($element->nodeName === "maxwidth") { |
|
242 | 242 | // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character |
243 | 243 | // we duplicate the info to allow Xliff V1 and V2 to work the same |
244 | - $metadata[ 'sizeRestriction' ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
245 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
244 | + $metadata['sizeRestriction'] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
245 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
246 | 246 | } else { |
247 | - $metadata[ $element->nodeName ] = $element->nodeValue; |
|
247 | + $metadata[$element->nodeName] = $element->nodeValue; |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | } |
@@ -259,19 +259,19 @@ discard block |
||
259 | 259 | * @return array |
260 | 260 | * @throws Exception |
261 | 261 | */ |
262 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ): array { |
|
262 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit): array { |
|
263 | 263 | $notes = []; |
264 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
264 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
265 | 265 | |
266 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
266 | + $noteValue = $this->extractTagContent($dom, $note); |
|
267 | 267 | |
268 | - if ( '' !== $noteValue ) { |
|
268 | + if ('' !== $noteValue) { |
|
269 | 269 | |
270 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
270 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
271 | 271 | |
272 | 272 | // extract all the attributes |
273 | - foreach ( $note->attributes as $attribute ) { |
|
274 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
273 | + foreach ($note->attributes as $attribute) { |
|
274 | + $extractedNote[$attribute->name] = $attribute->value; |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | $notes[] = $extractedNote; |
@@ -287,14 +287,14 @@ discard block |
||
287 | 287 | * |
288 | 288 | * @return array |
289 | 289 | */ |
290 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ): array { |
|
290 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup): array { |
|
291 | 291 | $cg = []; |
292 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
292 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
293 | 293 | |
294 | 294 | /** @var DOMNode $context */ |
295 | - foreach ( $contextGroup->childNodes as $context ) { |
|
296 | - if ( $context->nodeName === 'context' ) { |
|
297 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
295 | + foreach ($contextGroup->childNodes as $context) { |
|
296 | + if ($context->nodeName === 'context') { |
|
297 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
298 | 298 | } |
299 | 299 | } |
300 | 300 | |
@@ -306,16 +306,16 @@ discard block |
||
306 | 306 | * |
307 | 307 | * @return array |
308 | 308 | */ |
309 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
309 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
310 | 310 | $at = []; |
311 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
311 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
312 | 312 | |
313 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
314 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
313 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
314 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
315 | 315 | } |
316 | 316 | |
317 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
318 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
317 | + if ($altTrans->getElementsByTagName('target')) { |
|
318 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
319 | 319 | } |
320 | 320 | |
321 | 321 | return $at; |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | * |
327 | 327 | * @return bool |
328 | 328 | */ |
329 | - private function extractLocked( DOMElement $locked ) { |
|
330 | - return null !== $locked->getAttribute( 'locked' ); |
|
329 | + private function extractLocked(DOMElement $locked) { |
|
330 | + return null !== $locked->getAttribute('locked'); |
|
331 | 331 | } |
332 | 332 | } |
@@ -16,30 +16,30 @@ 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( $dom ); |
|
25 | + $output['files'][$i]['attr'] = $this->extractMetadata($dom); |
|
26 | 26 | |
27 | 27 | // notes |
28 | - $output[ 'files' ][ $i ][ 'notes' ] = $this->extractNotes( $file ); |
|
28 | + $output['files'][$i]['notes'] = $this->extractNotes($file); |
|
29 | 29 | |
30 | 30 | // trans-units |
31 | 31 | $transUnitIdArrayForUniquenessCheck = []; |
32 | 32 | $j = 1; |
33 | 33 | /** @var DOMElement $transUnit */ |
34 | - foreach ( $file->childNodes as $childNode ) { |
|
35 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
34 | + foreach ($file->childNodes as $childNode) { |
|
35 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // trans-unit re-count check |
39 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
40 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
41 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
42 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
39 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
40 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
41 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
42 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | $i++; |
@@ -53,23 +53,23 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return array |
55 | 55 | */ |
56 | - private function extractMetadata( DOMDocument $dom ): array { |
|
56 | + private function extractMetadata(DOMDocument $dom): array { |
|
57 | 57 | $metadata = []; |
58 | 58 | |
59 | - $xliffNode = $dom->getElementsByTagName( 'xliff' )->item( 0 ); |
|
60 | - $fileNode = $dom->getElementsByTagName( 'file' )->item( 0 ); |
|
59 | + $xliffNode = $dom->getElementsByTagName('xliff')->item(0); |
|
60 | + $fileNode = $dom->getElementsByTagName('file')->item(0); |
|
61 | 61 | |
62 | 62 | // original |
63 | - $metadata[ 'original' ] = ( null !== $fileNode->attributes->getNamedItem( 'original' ) ) ? $fileNode->attributes->getNamedItem( 'original' )->nodeValue : 'no-name'; |
|
63 | + $metadata['original'] = (null !== $fileNode->attributes->getNamedItem('original')) ? $fileNode->attributes->getNamedItem('original')->nodeValue : 'no-name'; |
|
64 | 64 | |
65 | 65 | // source-language |
66 | - $metadata[ 'source-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'srcLang' ) ) ? $xliffNode->attributes->getNamedItem( 'srcLang' )->nodeValue : 'en-US'; |
|
66 | + $metadata['source-language'] = (null !== $xliffNode->attributes->getNamedItem('srcLang')) ? $xliffNode->attributes->getNamedItem('srcLang')->nodeValue : 'en-US'; |
|
67 | 67 | |
68 | 68 | // datatype |
69 | 69 | // @TODO to be implemented |
70 | 70 | |
71 | 71 | // target-language |
72 | - $metadata[ 'target-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'trgLang' ) ) ? $xliffNode->attributes->getNamedItem( 'trgLang' )->nodeValue : 'en-US'; |
|
72 | + $metadata['target-language'] = (null !== $xliffNode->attributes->getNamedItem('trgLang')) ? $xliffNode->attributes->getNamedItem('trgLang')->nodeValue : 'en-US'; |
|
73 | 73 | |
74 | 74 | // custom MateCat x-attribute |
75 | 75 | // @TODO to be implemented |
@@ -83,16 +83,16 @@ discard block |
||
83 | 83 | * @return array |
84 | 84 | * @throws Exception |
85 | 85 | */ |
86 | - private function extractNotes( DOMElement $file ): array { |
|
86 | + private function extractNotes(DOMElement $file): array { |
|
87 | 87 | $notes = []; |
88 | 88 | |
89 | 89 | // loop <notes> to get nested <note> tag |
90 | - foreach ( $file->childNodes as $childNode ) { |
|
91 | - if ( $childNode->nodeName === 'notes' ) { |
|
92 | - foreach ( $childNode->childNodes as $note ) { |
|
93 | - $noteValue = trim( $note->nodeValue ); |
|
94 | - if ( '' !== $noteValue ) { |
|
95 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
90 | + foreach ($file->childNodes as $childNode) { |
|
91 | + if ($childNode->nodeName === 'notes') { |
|
92 | + foreach ($childNode->childNodes as $note) { |
|
93 | + $noteValue = trim($note->nodeValue); |
|
94 | + if ('' !== $noteValue) { |
|
95 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | } |
@@ -114,32 +114,32 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @throws Exception |
116 | 116 | */ |
117 | - protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ) { |
|
117 | + protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []) { |
|
118 | 118 | // metadata |
119 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
119 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
120 | 120 | |
121 | 121 | // notes |
122 | 122 | // merge <notes> with key and key-note contained in metadata <mda:metaGroup> |
123 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $transUnit ); |
|
123 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($transUnit); |
|
124 | 124 | |
125 | 125 | // uuid |
126 | - foreach ( $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] as $note ) { |
|
127 | - if ( isset( $note[ 'raw-content' ] ) && Strings::isAValidUuid( $note[ 'raw-content' ] ) ) { |
|
128 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ][ 'uuid' ] = $note[ 'raw-content' ]; |
|
126 | + foreach ($output['files'][$i]['trans-units'][$j]['notes'] as $note) { |
|
127 | + if (isset($note['raw-content']) && Strings::isAValidUuid($note['raw-content'])) { |
|
128 | + $output['files'][$i]['trans-units'][$j]['attr']['uuid'] = $note['raw-content']; |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | 132 | // original-data (exclusive for V2) |
133 | 133 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html#originaldata |
134 | - $originalData = $this->extractTransUnitOriginalData( $transUnit ); |
|
135 | - if ( !empty( $originalData ) ) { |
|
136 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'original-data' ] = $originalData; |
|
134 | + $originalData = $this->extractTransUnitOriginalData($transUnit); |
|
135 | + if (!empty($originalData)) { |
|
136 | + $output['files'][$i]['trans-units'][$j]['original-data'] = $originalData; |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // additionalTagData (exclusive for V2) |
140 | - $additionalTagData = $this->extractTransUnitAdditionalTagData( $transUnit ); |
|
141 | - if ( !empty( $additionalTagData ) ) { |
|
142 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'additional-tag-data' ] = $additionalTagData; |
|
140 | + $additionalTagData = $this->extractTransUnitAdditionalTagData($transUnit); |
|
141 | + if (!empty($additionalTagData)) { |
|
142 | + $output['files'][$i]['trans-units'][$j]['additional-tag-data'] = $additionalTagData; |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | // content |
@@ -159,54 +159,54 @@ discard block |
||
159 | 159 | |
160 | 160 | /** @var DOMElement $segment */ |
161 | 161 | $c = 0; |
162 | - foreach ( $transUnit->childNodes as $segment ) { |
|
163 | - if ( $segment->nodeName === 'segment' ) { |
|
162 | + foreach ($transUnit->childNodes as $segment) { |
|
163 | + if ($segment->nodeName === 'segment') { |
|
164 | 164 | |
165 | 165 | // check segment id consistency |
166 | - $attr = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ]; |
|
167 | - $this->checkSegmentIdConsistency( $segment, $attr ); |
|
166 | + $attr = $output['files'][$i]['trans-units'][$j]['attr']; |
|
167 | + $this->checkSegmentIdConsistency($segment, $attr); |
|
168 | 168 | |
169 | 169 | // loop <segment> to get nested <source> and <target> tag |
170 | - foreach ( $segment->childNodes as $childNode ) { |
|
171 | - if ( $childNode->nodeName === 'source' ) { |
|
172 | - $extractedSource = $this->extractContent( $dom, $childNode ); |
|
173 | - $source[ 'raw-content' ][ $c ] = $extractedSource[ 'raw-content' ]; |
|
170 | + foreach ($segment->childNodes as $childNode) { |
|
171 | + if ($childNode->nodeName === 'source') { |
|
172 | + $extractedSource = $this->extractContent($dom, $childNode); |
|
173 | + $source['raw-content'][$c] = $extractedSource['raw-content']; |
|
174 | 174 | |
175 | - if ( !empty( $extractedSource[ 'attr' ] ) ) { |
|
176 | - $source[ 'attr' ][ $c ] = $extractedSource[ 'attr' ]; |
|
175 | + if (!empty($extractedSource['attr'])) { |
|
176 | + $source['attr'][$c] = $extractedSource['attr']; |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | // append value to 'seg-source' |
180 | - if ( $this->stringContainsMarks( $extractedSource[ 'raw-content' ] ) ) { |
|
181 | - $segSource = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
180 | + if ($this->stringContainsMarks($extractedSource['raw-content'])) { |
|
181 | + $segSource = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
182 | 182 | } else { |
183 | 183 | $segSource[] = [ |
184 | - 'attr' => $this->extractTagAttributes( $segment ), |
|
185 | - 'mid' => count( $segSource ) > 0 ? count( $segSource ) : 0, |
|
184 | + 'attr' => $this->extractTagAttributes($segment), |
|
185 | + 'mid' => count($segSource) > 0 ? count($segSource) : 0, |
|
186 | 186 | 'ext-prec-tags' => '', |
187 | - 'raw-content' => $extractedSource[ 'raw-content' ], |
|
187 | + 'raw-content' => $extractedSource['raw-content'], |
|
188 | 188 | 'ext-succ-tags' => '', |
189 | 189 | ]; |
190 | 190 | } |
191 | 191 | } |
192 | 192 | |
193 | - if ( $childNode->nodeName === 'target' ) { |
|
194 | - $extractedTarget = $this->extractContent( $dom, $childNode ); |
|
195 | - $target[ 'raw-content' ][ $c ] = $extractedTarget[ 'raw-content' ]; |
|
193 | + if ($childNode->nodeName === 'target') { |
|
194 | + $extractedTarget = $this->extractContent($dom, $childNode); |
|
195 | + $target['raw-content'][$c] = $extractedTarget['raw-content']; |
|
196 | 196 | |
197 | - if ( !empty( $extractedTarget[ 'attr' ] ) ) { |
|
198 | - $target[ 'attr' ][ $c ] = $extractedTarget[ 'attr' ]; |
|
197 | + if (!empty($extractedTarget['attr'])) { |
|
198 | + $target['attr'][$c] = $extractedTarget['attr']; |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | // append value to 'seg-target' |
202 | - if ( $this->stringContainsMarks( $extractedTarget[ 'raw-content' ] ) ) { |
|
203 | - $segTarget = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
202 | + if ($this->stringContainsMarks($extractedTarget['raw-content'])) { |
|
203 | + $segTarget = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
204 | 204 | } else { |
205 | 205 | $segTarget[] = [ |
206 | - 'attr' => $this->extractTagAttributes( $segment ), |
|
207 | - 'mid' => count( $segTarget ) > 0 ? count( $segTarget ) : 0, |
|
206 | + 'attr' => $this->extractTagAttributes($segment), |
|
207 | + 'mid' => count($segTarget) > 0 ? count($segTarget) : 0, |
|
208 | 208 | 'ext-prec-tags' => '', |
209 | - 'raw-content' => $extractedTarget[ 'raw-content' ], |
|
209 | + 'raw-content' => $extractedTarget['raw-content'], |
|
210 | 210 | 'ext-succ-tags' => '', |
211 | 211 | ]; |
212 | 212 | } |
@@ -217,10 +217,10 @@ discard block |
||
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
220 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $source; |
|
221 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $target; |
|
222 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $segSource; |
|
223 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $segTarget; |
|
220 | + $output['files'][$i]['trans-units'][$j]['source'] = $source; |
|
221 | + $output['files'][$i]['trans-units'][$j]['target'] = $target; |
|
222 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $segSource; |
|
223 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $segTarget; |
|
224 | 224 | |
225 | 225 | $j++; |
226 | 226 | } |
@@ -231,41 +231,41 @@ discard block |
||
231 | 231 | * |
232 | 232 | * @return array |
233 | 233 | */ |
234 | - private function extractTransUnitMetadata( DOMElement $transUnit, ?array &$transUnitIdArrayForUniquenessCheck = [] ): array { |
|
234 | + private function extractTransUnitMetadata(DOMElement $transUnit, ?array &$transUnitIdArrayForUniquenessCheck = []): array { |
|
235 | 235 | $metadata = []; |
236 | 236 | |
237 | 237 | // id |
238 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
239 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
238 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
239 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
240 | 240 | } |
241 | 241 | |
242 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
242 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
243 | 243 | |
244 | - if ( strlen( $id ) > 100 ) { |
|
245 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
244 | + if (strlen($id) > 100) { |
|
245 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | $transUnitIdArrayForUniquenessCheck[] = $id; |
249 | - $metadata[ 'id' ] = $id; |
|
249 | + $metadata['id'] = $id; |
|
250 | 250 | |
251 | 251 | // translate |
252 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
253 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
252 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
253 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | // tGroupBegin |
257 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupBegin' ) ) { |
|
258 | - $metadata[ 'tGroupBegin' ] = $transUnit->attributes->getNamedItem( 'tGroupBegin' )->nodeValue; |
|
257 | + if (null !== $transUnit->attributes->getNamedItem('tGroupBegin')) { |
|
258 | + $metadata['tGroupBegin'] = $transUnit->attributes->getNamedItem('tGroupBegin')->nodeValue; |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | // tGroupEnd |
262 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupEnd' ) ) { |
|
263 | - $metadata[ 'tGroupEnd' ] = $transUnit->attributes->getNamedItem( 'tGroupEnd' )->nodeValue; |
|
262 | + if (null !== $transUnit->attributes->getNamedItem('tGroupEnd')) { |
|
263 | + $metadata['tGroupEnd'] = $transUnit->attributes->getNamedItem('tGroupEnd')->nodeValue; |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | // sizeRestriction |
267 | - if ( null !== $transUnit->attributes->getNamedItem( 'sizeRestriction' ) && '' !== $transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue ) { |
|
268 | - $metadata[ 'sizeRestriction' ] = (int)$transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue; |
|
267 | + if (null !== $transUnit->attributes->getNamedItem('sizeRestriction') && '' !== $transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue) { |
|
268 | + $metadata['sizeRestriction'] = (int)$transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue; |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | return $metadata; |
@@ -277,31 +277,31 @@ discard block |
||
277 | 277 | * @return array |
278 | 278 | * @throws Exception |
279 | 279 | */ |
280 | - private function extractTransUnitOriginalData( DOMElement $transUnit ): array { |
|
280 | + private function extractTransUnitOriginalData(DOMElement $transUnit): array { |
|
281 | 281 | $originalData = []; |
282 | 282 | |
283 | 283 | // loop <originalData> to get nested content |
284 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
285 | - if ( $childNode->nodeName === 'originalData' ) { |
|
286 | - foreach ( $childNode->childNodes as $data ) { |
|
287 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
288 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
284 | + foreach ($transUnit->childNodes as $childNode) { |
|
285 | + if ($childNode->nodeName === 'originalData') { |
|
286 | + foreach ($childNode->childNodes as $data) { |
|
287 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
288 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
289 | 289 | |
290 | - $dataValue = str_replace( Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue ); |
|
291 | - $dataValue = str_replace( Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue ); |
|
292 | - $dataValue = str_replace( Placeholder::TAB_PLACEHOLDER, '\t', $dataValue ); |
|
290 | + $dataValue = str_replace(Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue); |
|
291 | + $dataValue = str_replace(Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue); |
|
292 | + $dataValue = str_replace(Placeholder::TAB_PLACEHOLDER, '\t', $dataValue); |
|
293 | 293 | |
294 | - if ( '' !== $dataValue ) { |
|
294 | + if ('' !== $dataValue) { |
|
295 | 295 | |
296 | - $jsonOrRawContentArray = $this->JSONOrRawContentArray( $dataValue, false ); |
|
296 | + $jsonOrRawContentArray = $this->JSONOrRawContentArray($dataValue, false); |
|
297 | 297 | |
298 | 298 | // restore xliff tags |
299 | - if ( isset( $jsonOrRawContentArray[ 'json' ] ) ) { |
|
300 | - $jsonOrRawContentArray[ 'json' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'json' ] ); |
|
299 | + if (isset($jsonOrRawContentArray['json'])) { |
|
300 | + $jsonOrRawContentArray['json'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['json']); |
|
301 | 301 | } |
302 | 302 | |
303 | - if ( isset( $jsonOrRawContentArray[ 'raw-content' ] ) ) { |
|
304 | - $jsonOrRawContentArray[ 'raw-content' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'raw-content' ] ); |
|
303 | + if (isset($jsonOrRawContentArray['raw-content'])) { |
|
304 | + $jsonOrRawContentArray['raw-content'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['raw-content']); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | $originalData[] = array_merge( |
@@ -326,41 +326,41 @@ discard block |
||
326 | 326 | * |
327 | 327 | * @return array |
328 | 328 | */ |
329 | - private function extractTransUnitAdditionalTagData( DOMElement $transUnit ): array { |
|
329 | + private function extractTransUnitAdditionalTagData(DOMElement $transUnit): array { |
|
330 | 330 | $additionalTagData = []; |
331 | 331 | |
332 | 332 | // loop <originalData> to get nested content |
333 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
334 | - if ( $childNode->nodeName === 'memsource:additionalTagData' ) { |
|
335 | - foreach ( $childNode->childNodes as $data ) { |
|
333 | + foreach ($transUnit->childNodes as $childNode) { |
|
334 | + if ($childNode->nodeName === 'memsource:additionalTagData') { |
|
335 | + foreach ($childNode->childNodes as $data) { |
|
336 | 336 | $dataArray = []; |
337 | 337 | |
338 | 338 | // id |
339 | - if ( $data->nodeName === 'memsource:tag' ) { |
|
340 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
341 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
342 | - $dataArray[ 'attr' ][ 'id' ] = $dataId; |
|
339 | + if ($data->nodeName === 'memsource:tag') { |
|
340 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
341 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
342 | + $dataArray['attr']['id'] = $dataId; |
|
343 | 343 | } |
344 | 344 | } |
345 | 345 | |
346 | 346 | // in PHP 7.4 $data->childNodes is an empty DomNodeList, it is iterable with size 0 |
347 | 347 | // PHP 5.6 check: in php 5.6 $data->childNodes can be null |
348 | - if ( $data->childNodes != null ) { |
|
348 | + if ($data->childNodes != null) { |
|
349 | 349 | |
350 | 350 | // content |
351 | - foreach ( $data->childNodes as $datum ) { |
|
352 | - if ( $datum->nodeName === 'memsource:tagId' ) { |
|
353 | - $dataArray[ 'raw-content' ][ 'tagId' ] = $datum->nodeValue; |
|
351 | + foreach ($data->childNodes as $datum) { |
|
352 | + if ($datum->nodeName === 'memsource:tagId') { |
|
353 | + $dataArray['raw-content']['tagId'] = $datum->nodeValue; |
|
354 | 354 | } |
355 | 355 | |
356 | - if ( $datum->nodeName === 'memsource:type' ) { |
|
357 | - $dataArray[ 'raw-content' ][ 'type' ] = $datum->nodeValue; |
|
356 | + if ($datum->nodeName === 'memsource:type') { |
|
357 | + $dataArray['raw-content']['type'] = $datum->nodeValue; |
|
358 | 358 | } |
359 | 359 | } |
360 | 360 | |
361 | 361 | } |
362 | 362 | |
363 | - if ( !empty( $dataArray ) ) { |
|
363 | + if (!empty($dataArray)) { |
|
364 | 364 | $additionalTagData[] = $dataArray; |
365 | 365 | } |
366 | 366 | } |
@@ -376,15 +376,15 @@ discard block |
||
376 | 376 | * @param DOMElement $segment |
377 | 377 | * @param array $attr |
378 | 378 | */ |
379 | - private function checkSegmentIdConsistency( DOMElement $segment, array $attr ) { |
|
380 | - if ( isset( $attr[ 'tGroupBegin' ] ) && isset( $attr[ 'tGroupEnd' ] ) && $segment->attributes->getNamedItem( 'id' ) ) { |
|
381 | - $id = $segment->attributes->getNamedItem( 'id' )->nodeValue; |
|
382 | - $min = (int)$attr[ 'tGroupBegin' ]; |
|
383 | - $max = (int)$attr[ 'tGroupEnd' ]; |
|
384 | - |
|
385 | - if ( false === ( ( $min <= $id ) && ( $id <= $max ) ) ) { |
|
386 | - if ( $this->logger ) { |
|
387 | - $this->logger->warning( 'Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd' ); |
|
379 | + private function checkSegmentIdConsistency(DOMElement $segment, array $attr) { |
|
380 | + if (isset($attr['tGroupBegin']) && isset($attr['tGroupEnd']) && $segment->attributes->getNamedItem('id')) { |
|
381 | + $id = $segment->attributes->getNamedItem('id')->nodeValue; |
|
382 | + $min = (int)$attr['tGroupBegin']; |
|
383 | + $max = (int)$attr['tGroupEnd']; |
|
384 | + |
|
385 | + if (false === (($min <= $id) && ($id <= $max))) { |
|
386 | + if ($this->logger) { |
|
387 | + $this->logger->warning('Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd'); |
|
388 | 388 | } |
389 | 389 | } |
390 | 390 | } |
@@ -396,31 +396,31 @@ discard block |
||
396 | 396 | * @return array |
397 | 397 | * @throws Exception |
398 | 398 | */ |
399 | - private function extractTransUnitNotes( DOMElement $transUnit ): array { |
|
399 | + private function extractTransUnitNotes(DOMElement $transUnit): array { |
|
400 | 400 | $notes = []; |
401 | 401 | |
402 | 402 | // loop <notes> to get nested <note> tag |
403 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
404 | - if ( $childNode->nodeName == 'notes' ) { |
|
405 | - foreach ( $childNode->childNodes as $note ) { |
|
406 | - $noteValue = trim( $note->nodeValue ); |
|
407 | - if ( '' !== $noteValue ) { |
|
408 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
403 | + foreach ($transUnit->childNodes as $childNode) { |
|
404 | + if ($childNode->nodeName == 'notes') { |
|
405 | + foreach ($childNode->childNodes as $note) { |
|
406 | + $noteValue = trim($note->nodeValue); |
|
407 | + if ('' !== $noteValue) { |
|
408 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
409 | 409 | } |
410 | 410 | } |
411 | 411 | } |
412 | 412 | |
413 | - if ( $childNode->nodeName === 'mda:metadata' ) { |
|
414 | - foreach ( $childNode->childNodes as $metadata ) { |
|
415 | - if ( $metadata->nodeName === 'mda:metaGroup' ) { |
|
416 | - foreach ( $metadata->childNodes as $meta ) { |
|
417 | - if ( null !== $meta->attributes && null !== $meta->attributes->getNamedItem( 'type' ) ) { |
|
418 | - $type = $meta->attributes->getNamedItem( 'type' )->nodeValue; |
|
419 | - $metaValue = trim( $meta->nodeValue ); |
|
413 | + if ($childNode->nodeName === 'mda:metadata') { |
|
414 | + foreach ($childNode->childNodes as $metadata) { |
|
415 | + if ($metadata->nodeName === 'mda:metaGroup') { |
|
416 | + foreach ($metadata->childNodes as $meta) { |
|
417 | + if (null !== $meta->attributes && null !== $meta->attributes->getNamedItem('type')) { |
|
418 | + $type = $meta->attributes->getNamedItem('type')->nodeValue; |
|
419 | + $metaValue = trim($meta->nodeValue); |
|
420 | 420 | |
421 | - if ( '' !== $metaValue ) { |
|
421 | + if ('' !== $metaValue) { |
|
422 | 422 | $notes[] = array_merge( |
423 | - $this->JSONOrRawContentArray( $metaValue ), |
|
423 | + $this->JSONOrRawContentArray($metaValue), |
|
424 | 424 | [ |
425 | 425 | 'attr' => [ |
426 | 426 | 'type' => $type |
@@ -12,10 +12,10 @@ |
||
12 | 12 | * |
13 | 13 | * @return AbstractXliffParser |
14 | 14 | */ |
15 | - public static function getInstance( int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null ): AbstractXliffParser { |
|
15 | + public static function getInstance(int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null): AbstractXliffParser { |
|
16 | 16 | $parserClass = 'Matecat\\XliffParser\\XliffParser\\XliffParserV' . $xliffVersion; |
17 | 17 | |
18 | 18 | /** @var AbstractXliffParser $parser */ |
19 | - return new $parserClass( $xliffVersion, $xliffProprietary, $logger ); |
|
19 | + return new $parserClass($xliffVersion, $xliffProprietary, $logger); |
|
20 | 20 | } |
21 | 21 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | /** |
7 | 7 | * List of allowed Xliff tags |
8 | 8 | */ |
9 | - public static array $tags = [ 'g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk' ]; |
|
9 | + public static array $tags = ['g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk']; |
|
10 | 10 | } |
11 | 11 | \ No newline at end of file |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * |
21 | 21 | * @param array|null $tmp |
22 | 22 | */ |
23 | - public function __construct( ?array $tmp = [] ) { |
|
23 | + public function __construct(?array $tmp = []) { |
|
24 | 24 | $this->tmp = $tmp; |
25 | 25 | $this->steps = []; |
26 | 26 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * @param CheckInterface $step |
30 | 30 | */ |
31 | - public function addCheck( CheckInterface $step ) { |
|
31 | + public function addCheck(CheckInterface $step) { |
|
32 | 32 | $this->steps[] = $step; |
33 | 33 | } |
34 | 34 | |
@@ -39,13 +39,13 @@ discard block |
||
39 | 39 | $fileType = []; |
40 | 40 | |
41 | 41 | /** @var CheckInterface $step */ |
42 | - foreach ( $this->steps as $step ) { |
|
43 | - if ( null !== $step->check( $this->tmp ) ) { |
|
44 | - $fileType = $step->check( $this->tmp ); |
|
42 | + foreach ($this->steps as $step) { |
|
43 | + if (null !== $step->check($this->tmp)) { |
|
44 | + $fileType = $step->check($this->tmp); |
|
45 | 45 | } |
46 | 46 | } |
47 | 47 | |
48 | - if ( !empty( $fileType ) && $this->isValid( $fileType ) ) { |
|
48 | + if (!empty($fileType) && $this->isValid($fileType)) { |
|
49 | 49 | return $fileType; |
50 | 50 | } |
51 | 51 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return bool |
64 | 64 | */ |
65 | - private function isValid( $fileType ): bool { |
|
65 | + private function isValid($fileType): bool { |
|
66 | 66 | $mandatoryKeys = [ |
67 | 67 | 'proprietary', |
68 | 68 | 'proprietary_name', |
@@ -70,6 +70,6 @@ discard block |
||
70 | 70 | 'converter_version', |
71 | 71 | ]; |
72 | 72 | |
73 | - return array_keys( $fileType ) === $mandatoryKeys; |
|
73 | + return array_keys($fileType) === $mandatoryKeys; |
|
74 | 74 | } |
75 | 75 | } |
@@ -8,16 +8,16 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( ?array $tmp = [] ): ?array { |
|
11 | + public function check(?array $tmp = []): ?array { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - if ( stripos( $tmp[ 0 ], 'sdl:version' ) !== false ) { |
|
14 | + if (isset($tmp[0])) { |
|
15 | + if (stripos($tmp[0], 'sdl:version') !== false) { |
|
16 | 16 | //little trick, we consider not proprietary Sdlxliff files because we can handle them |
17 | - $fileType[ 'proprietary' ] = false; |
|
18 | - $fileType[ 'proprietary_name' ] = 'SDL Studio '; |
|
19 | - $fileType[ 'proprietary_short_name' ] = 'trados'; |
|
20 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
17 | + $fileType['proprietary'] = false; |
|
18 | + $fileType['proprietary_name'] = 'SDL Studio '; |
|
19 | + $fileType['proprietary_short_name'] = 'trados'; |
|
20 | + $fileType['converter_version'] = 'legacy'; |
|
21 | 21 | |
22 | 22 | return $fileType; |
23 | 23 | } |