@@ -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,28 +131,28 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return mixed|string |
| 133 | 133 | */ |
| 134 | - private static function removeInternalFileTagFromContent( $xliffContent, &$xliff ) { |
|
| 134 | + private static function removeInternalFileTagFromContent($xliffContent, &$xliff) { |
|
| 135 | 135 | $index = 1; |
| 136 | - $a = Strings::preg_split( '|<internal-file[\s>]|i', $xliffContent ); |
|
| 137 | - $tagMatches = count( $a ); |
|
| 136 | + $a = Strings::preg_split('|<internal-file[\s>]|i', $xliffContent); |
|
| 137 | + $tagMatches = count($a); |
|
| 138 | 138 | |
| 139 | 139 | // no match, return original string |
| 140 | - if ( $tagMatches === 1 ) { |
|
| 141 | - return $a[ 0 ]; |
|
| 140 | + if ($tagMatches === 1) { |
|
| 141 | + return $a[0]; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - $b = Strings::preg_split( '|</internal-file>|i', $a[ 1 ] ); |
|
| 145 | - $strippedContent = $a[ 0 ] . $b[ 1 ]; |
|
| 146 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $b[ 0 ] ); |
|
| 144 | + $b = Strings::preg_split('|</internal-file>|i', $a[1]); |
|
| 145 | + $strippedContent = $a[0] . $b[1]; |
|
| 146 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
| 147 | 147 | $index++; |
| 148 | 148 | |
| 149 | 149 | // Sometimes, sdlxliff files can contain more than 2 <internal-file> nodes. |
| 150 | 150 | // In this case loop and extract any other extra <internal-file> node |
| 151 | - for($i=2; $i < $tagMatches; $i++){ |
|
| 152 | - if ( isset( $a[ $i ] ) ) { |
|
| 153 | - $c = Strings::preg_split( '|</internal-file[\s>]|i', $a[ $i ] ); |
|
| 154 | - $strippedContent .= $c[ 1 ]; |
|
| 155 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $c[ 0 ] ); |
|
| 151 | + for ($i = 2; $i < $tagMatches; $i++) { |
|
| 152 | + if (isset($a[$i])) { |
|
| 153 | + $c = Strings::preg_split('|</internal-file[\s>]|i', $a[$i]); |
|
| 154 | + $strippedContent .= $c[1]; |
|
| 155 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
@@ -164,10 +164,10 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return array |
| 166 | 166 | */ |
| 167 | - private static function extractBase64( $base64 ): array { |
|
| 167 | + private static function extractBase64($base64): array { |
|
| 168 | 168 | return [ |
| 169 | 169 | 'form-type' => 'base64', |
| 170 | - 'base64' => trim( str_replace( 'form="base64">', '', $base64 ) ), |
|
| 170 | + 'base64' => trim(str_replace('form="base64">', '', $base64)), |
|
| 171 | 171 | ]; |
| 172 | 172 | } |
| 173 | 173 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return string |
| 189 | 189 | */ |
| 190 | - private static function escapeDataInOriginalMap( string $xliffContent ): string { |
|
| 191 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceSpace' ], $xliffContent ); |
|
| 192 | - $xliffContent = preg_replace_callback( '|<data(.*?)>(.*?)</data>|iU', [ XliffParser::class, 'replaceXliffTags' ], $xliffContent ); |
|
| 190 | + private static function escapeDataInOriginalMap(string $xliffContent): string { |
|
| 191 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
| 192 | + $xliffContent = preg_replace_callback('|<data(.*?)>(.*?)</data>|iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
| 193 | 193 | |
| 194 | 194 | return $xliffContent; |
| 195 | 195 | } |
@@ -208,15 +208,15 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @return string |
| 210 | 210 | */ |
| 211 | - private static function insertPlaceholderInEmptyTags( $xliffContent ): string { |
|
| 212 | - preg_match_all( '|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches ); |
|
| 211 | + private static function insertPlaceholderInEmptyTags($xliffContent): string { |
|
| 212 | + preg_match_all('|<([a-zA-Z0-9._-]+)[^>]*></\1>|m', $xliffContent, $emptyTagMatches); |
|
| 213 | 213 | |
| 214 | - if ( !empty( $emptyTagMatches[ 0 ] ) ) { |
|
| 215 | - foreach ( $emptyTagMatches[ 0 ] as $index => $emptyTagMatch ) { |
|
| 216 | - $matchedTag = $emptyTagMatches[ 1 ][ $index ]; |
|
| 214 | + if (!empty($emptyTagMatches[0])) { |
|
| 215 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
| 216 | + $matchedTag = $emptyTagMatches[1][$index]; |
|
| 217 | 217 | $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
| 218 | - $replacedTag = str_replace( '</' . $matchedTag . '>', $subst, $emptyTagMatch ); |
|
| 219 | - $xliffContent = str_replace( $emptyTagMatch, $replacedTag, $xliffContent ); |
|
| 218 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
| 219 | + $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
|
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
@@ -230,12 +230,12 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @return string |
| 232 | 232 | */ |
| 233 | - private static function replaceSpace( array $matches ): string { |
|
| 234 | - $content = str_replace( ' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[ 2 ] ); |
|
| 235 | - $content = str_replace( '\n', Placeholder::NEW_LINE_PLACEHOLDER, $content ); |
|
| 236 | - $content = str_replace( '\t', Placeholder::TAB_PLACEHOLDER, $content ); |
|
| 233 | + private static function replaceSpace(array $matches): string { |
|
| 234 | + $content = str_replace(' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[2]); |
|
| 235 | + $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
|
| 236 | + $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
|
| 237 | 237 | |
| 238 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
| 238 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
@@ -243,15 +243,15 @@ discard block |
||
| 243 | 243 | * |
| 244 | 244 | * @return string |
| 245 | 245 | */ |
| 246 | - private static function replaceXliffTags( array $matches ): string { |
|
| 246 | + private static function replaceXliffTags(array $matches): string { |
|
| 247 | 247 | $xliffTags = XliffTags::$tags; |
| 248 | - $content = $matches[ 2 ]; |
|
| 248 | + $content = $matches[2]; |
|
| 249 | 249 | |
| 250 | - foreach ( $xliffTags as $xliffTag ) { |
|
| 251 | - $content = preg_replace( '|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
| 252 | - $content = preg_replace( '|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
| 250 | + foreach ($xliffTags as $xliffTag) { |
|
| 251 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
| 252 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
| 255 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
| 256 | 256 | } |
| 257 | 257 | } |