@@ -28,38 +28,38 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * @inheritDoc |
| 30 | 30 | */ |
| 31 | - protected function tagOpen( $parser, string $name, array $attr ) { |
|
| 31 | + protected function tagOpen($parser, string $name, array $attr) { |
|
| 32 | 32 | |
| 33 | - $this->handleOpenUnit( $name, $attr ); |
|
| 33 | + $this->handleOpenUnit($name, $attr); |
|
| 34 | 34 | |
| 35 | 35 | // check if we are entering into a <target> |
| 36 | - $this->checkSetInTarget( $name ); |
|
| 36 | + $this->checkSetInTarget($name); |
|
| 37 | 37 | |
| 38 | 38 | // reset Marker positions |
| 39 | - if ( 'sdl:seg-defs' == $name ) { |
|
| 39 | + if ('sdl:seg-defs' == $name) { |
|
| 40 | 40 | $this->segmentInUnitPosition = 0; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // open buffer |
| 44 | - $this->setInBuffer( $name ); |
|
| 44 | + $this->setInBuffer($name); |
|
| 45 | 45 | |
| 46 | 46 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
| 47 | 47 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
| 48 | - if ( !$this->inTarget ) { |
|
| 48 | + if (!$this->inTarget) { |
|
| 49 | 49 | |
| 50 | 50 | // costruct tag |
| 51 | 51 | $tag = "<$name "; |
| 52 | 52 | |
| 53 | - foreach ( $attr as $k => $v ) { |
|
| 53 | + foreach ($attr as $k => $v) { |
|
| 54 | 54 | |
| 55 | 55 | // if tag name is file, we must replace the target-language attribute |
| 56 | - if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) { |
|
| 56 | + if ($name == 'file' && $k == 'target-language' && !empty($this->targetLang)) { |
|
| 57 | 57 | //replace Target language with job language provided from constructor |
| 58 | 58 | $tag .= "$k=\"$this->targetLang\" "; |
| 59 | 59 | } else { |
| 60 | 60 | //normal tag flux, put attributes in it |
| 61 | 61 | // Warning, this is NOT an elseif |
| 62 | - if ( $k != 'conf' ) { |
|
| 62 | + if ($k != 'conf') { |
|
| 63 | 63 | //put also the current attribute in it if it is not a "conf" attribute |
| 64 | 64 | $tag .= "$k=\"$v\" "; |
| 65 | 65 | } |
@@ -68,11 +68,11 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | $seg = $this->getCurrentSegment(); |
| 70 | 70 | |
| 71 | - if ( 'sdl:seg' == $name && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
|
| 72 | - $tag .= $this->prepareTargetStatuses( $seg ); |
|
| 71 | + if ('sdl:seg' == $name && !empty($seg) && isset($seg['sid'])) { |
|
| 72 | + $tag .= $this->prepareTargetStatuses($seg); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|
| 75 | + $this->checkForSelfClosedTagAndFlush($parser, $tag); |
|
| 76 | 76 | |
| 77 | 77 | } |
| 78 | 78 | |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return string |
| 85 | 85 | */ |
| 86 | - protected function prepareTargetStatuses( $segment ): string { |
|
| 86 | + protected function prepareTargetStatuses($segment): string { |
|
| 87 | 87 | $statusMap = [ |
| 88 | 88 | 'NEW' => '', |
| 89 | 89 | 'DRAFT' => 'Draft', |
@@ -93,19 +93,19 @@ discard block |
||
| 93 | 93 | 'REJECTED' => 'RejectedTranslation', |
| 94 | 94 | ]; |
| 95 | 95 | |
| 96 | - return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" "; |
|
| 96 | + return "conf=\"{$statusMap[$segment['status']]}\" "; |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - protected function rebuildMarks( array $seg, string $translation ): string { |
|
| 99 | + protected function rebuildMarks(array $seg, string $translation): string { |
|
| 100 | 100 | |
| 101 | - $trailingSpaces = str_repeat( ' ', Strings::getTheNumberOfTrailingSpaces( $translation ) ); |
|
| 101 | + $trailingSpaces = str_repeat(' ', Strings::getTheNumberOfTrailingSpaces($translation)); |
|
| 102 | 102 | |
| 103 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
| 104 | - if ( $this->targetLang === 'ja-JP' ) { |
|
| 105 | - $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] ); |
|
| 103 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
| 104 | + if ($this->targetLang === 'ja-JP') { |
|
| 105 | + $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces; |
|
| 108 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . rtrim($translation) . $seg['mrk_succ_tags'] . "</mrk>" . $trailingSpaces; |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | return $translation; |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return string |
| 122 | 122 | */ |
| 123 | - protected function createTargetTag( string $translation, string $stateProp ): string { |
|
| 123 | + protected function createTargetTag(string $translation, string $stateProp): string { |
|
| 124 | 124 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
| 125 | 125 | |
| 126 | 126 | return "<target $targetLang $stateProp>$translation</target>"; |
@@ -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 | } |
@@ -36,27 +36,27 @@ discard block |
||
| 36 | 36 | /** |
| 37 | 37 | * @var string |
| 38 | 38 | */ |
| 39 | - protected string $namespace = "mtc"; // Custom namespace |
|
| 39 | + protected string $namespace = "mtc"; // Custom namespace |
|
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * @inheritDoc |
| 43 | 43 | */ |
| 44 | - protected function tagOpen( $parser, string $name, array $attr ) { |
|
| 44 | + protected function tagOpen($parser, string $name, array $attr) { |
|
| 45 | 45 | |
| 46 | - $this->handleOpenUnit( $name, $attr ); |
|
| 46 | + $this->handleOpenUnit($name, $attr); |
|
| 47 | 47 | |
| 48 | - $this->trySetAltTrans( $name );; |
|
| 49 | - $this->checkSetInTarget( $name ); |
|
| 48 | + $this->trySetAltTrans($name); ; |
|
| 49 | + $this->checkSetInTarget($name); |
|
| 50 | 50 | |
| 51 | 51 | // open buffer |
| 52 | - $this->setInBuffer( $name ); |
|
| 52 | + $this->setInBuffer($name); |
|
| 53 | 53 | |
| 54 | 54 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
| 55 | 55 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
| 56 | - if ( !$this->inTarget ) { |
|
| 56 | + if (!$this->inTarget) { |
|
| 57 | 57 | |
| 58 | 58 | // We need bufferIsActive for not target nodes with currentTransUnitIsTranslatable = 'NO' |
| 59 | - if($name === 'target' and $this->currentTransUnitIsTranslatable === 'no'){ |
|
| 59 | + if ($name === 'target' and $this->currentTransUnitIsTranslatable === 'no') { |
|
| 60 | 60 | $this->bufferIsActive = true; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -65,10 +65,10 @@ discard block |
||
| 65 | 65 | // construct tag |
| 66 | 66 | $tag .= "<$name "; |
| 67 | 67 | |
| 68 | - foreach ( $attr as $k => $v ) { |
|
| 68 | + foreach ($attr as $k => $v) { |
|
| 69 | 69 | |
| 70 | 70 | //if tag name is file, we must replace the target-language attribute |
| 71 | - if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
|
| 71 | + if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) { |
|
| 72 | 72 | //replace Target language with job language provided from constructor |
| 73 | 73 | $tag .= "$k=\"$this->targetLang\" "; |
| 74 | 74 | } else { |
@@ -79,20 +79,20 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | $seg = $this->getCurrentSegment(); |
| 81 | 81 | |
| 82 | - if ( $name === $this->tuTagName && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
|
| 82 | + if ($name === $this->tuTagName && !empty($seg) && isset($seg['sid'])) { |
|
| 83 | 83 | |
| 84 | 84 | // add `help-id` to xliff v.1* |
| 85 | - if ( strpos( $tag, 'help-id' ) === false ) { |
|
| 86 | - if ( !empty( $seg[ 'sid' ] ) ) { |
|
| 87 | - $tag .= "help-id=\"{$seg[ 'sid' ]}\" "; |
|
| 85 | + if (strpos($tag, 'help-id') === false) { |
|
| 86 | + if (!empty($seg['sid'])) { |
|
| 87 | + $tag .= "help-id=\"{$seg['sid']}\" "; |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
|
| 93 | + $tag = $this->handleOpenXliffTag($name, $attr, $tag); |
|
| 94 | 94 | |
| 95 | - $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|
| 95 | + $this->checkForSelfClosedTagAndFlush($parser, $tag); |
|
| 96 | 96 | |
| 97 | 97 | } |
| 98 | 98 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | /** |
| 103 | 103 | * @inheritDoc |
| 104 | 104 | */ |
| 105 | - protected function tagClose( $parser, string $name ) { |
|
| 105 | + protected function tagClose($parser, string $name) { |
|
| 106 | 106 | $tag = ''; |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,23 +111,23 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * self::tagOpen method |
| 113 | 113 | */ |
| 114 | - if ( !$this->isEmpty ) { |
|
| 114 | + if (!$this->isEmpty) { |
|
| 115 | 115 | |
| 116 | 116 | // write closing tag if is not a target |
| 117 | 117 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
| 118 | - if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
|
| 118 | + if (!$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no') { |
|
| 119 | 119 | $tag = "</$name>"; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - if ( 'target' == $name && !$this->inAltTrans ) { |
|
| 122 | + if ('target' == $name && !$this->inAltTrans) { |
|
| 123 | 123 | |
| 124 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 124 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 125 | 125 | |
| 126 | 126 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
| 127 | 127 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
| 128 | 128 | $tag = $this->rebuildTarget(); |
| 129 | 129 | |
| 130 | - } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
|
| 130 | + } elseif (!empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no') { |
|
| 131 | 131 | |
| 132 | 132 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
| 133 | 133 | $this->bufferIsActive = false; |
@@ -138,45 +138,45 @@ discard block |
||
| 138 | 138 | $this->targetWasWritten = true; |
| 139 | 139 | // signal we are leaving a target |
| 140 | 140 | $this->inTarget = false; |
| 141 | - $this->postProcAndFlush( $this->outputFP, $tag, true ); |
|
| 141 | + $this->postProcAndFlush($this->outputFP, $tag, true); |
|
| 142 | 142 | |
| 143 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
|
| 143 | + } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section |
|
| 144 | 144 | |
| 145 | 145 | $this->bufferIsActive = false; |
| 146 | 146 | $tag = $this->CDATABuffer . "</$name>"; |
| 147 | 147 | $this->CDATABuffer = ""; |
| 148 | 148 | |
| 149 | 149 | //flush to the pointer |
| 150 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 150 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 151 | 151 | |
| 152 | - } elseif ( $name === $this->tuTagName ) { |
|
| 152 | + } elseif ($name === $this->tuTagName) { |
|
| 153 | 153 | |
| 154 | 154 | $tag = ""; |
| 155 | 155 | |
| 156 | 156 | // handling </trans-unit> closure |
| 157 | - if ( !$this->targetWasWritten ) { |
|
| 157 | + if (!$this->targetWasWritten) { |
|
| 158 | 158 | |
| 159 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 159 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 160 | 160 | $tag = $this->rebuildTarget(); |
| 161 | 161 | } else { |
| 162 | - $tag = $this->createTargetTag( "", "" ); |
|
| 162 | + $tag = $this->createTargetTag("", ""); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $tag .= "</$this->tuTagName>"; |
|
| 167 | + $tag .= "</$this->tuTagName>"; |
|
| 168 | 168 | $this->targetWasWritten = false; |
| 169 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 169 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 170 | 170 | |
| 171 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 171 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
| 172 | 172 | $this->CDATABuffer .= "</$name>"; |
| 173 | 173 | // Do NOT Flush |
| 174 | 174 | } else { //generic tag closure do Nothing |
| 175 | 175 | // flush to pointer |
| 176 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 176 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
|
| 179 | + } elseif (in_array($name, $this->nodesToBuffer)) { |
|
| 180 | 180 | |
| 181 | 181 | $this->isEmpty = false; |
| 182 | 182 | $this->bufferIsActive = false; |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $this->CDATABuffer = ""; |
| 185 | 185 | |
| 186 | 186 | //flush to the pointer |
| 187 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 187 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 188 | 188 | |
| 189 | 189 | } else { |
| 190 | 190 | //ok, nothing to be done; reset flag for next coming tag |
@@ -192,10 +192,10 @@ discard block |
||
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // try to signal that we are leaving a target |
| 195 | - $this->tryUnsetAltTrans( $name ); |
|
| 195 | + $this->tryUnsetAltTrans($name); |
|
| 196 | 196 | |
| 197 | 197 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
| 198 | - if ( $this->tuTagName === $name ) { |
|
| 198 | + if ($this->tuTagName === $name) { |
|
| 199 | 199 | $this->currentTransUnitIsTranslatable = null; |
| 200 | 200 | $this->inTU = false; |
| 201 | 201 | $this->hasWrittenCounts = false; |
@@ -212,31 +212,31 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return string |
| 214 | 214 | */ |
| 215 | - protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
|
| 215 | + protected function prepareTranslation(array $seg, string $transUnitTranslation = ""): string { |
|
| 216 | 216 | |
| 217 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
| 218 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
| 217 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
| 218 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
| 219 | 219 | |
| 220 | - if ( $seg [ 'translation' ] == '' ) { |
|
| 220 | + if ($seg ['translation'] == '') { |
|
| 221 | 221 | $translation = $segment; |
| 222 | 222 | } else { |
| 223 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
| 224 | - $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
|
| 225 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
|
| 223 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
| 224 | + $error = (!empty($seg['error'])) ? $seg['error'] : null; |
|
| 225 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, [], $error)) { |
|
| 226 | 226 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
|
| 231 | + $transUnitTranslation .= $seg['prev_tags'] . $this->rebuildMarks($seg, $translation) . ltrim($seg['succ_tags']); |
|
| 232 | 232 | |
| 233 | 233 | return $transUnitTranslation; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - protected function rebuildMarks( array $seg, string $translation ): string { |
|
| 236 | + protected function rebuildMarks(array $seg, string $translation): string { |
|
| 237 | 237 | |
| 238 | - if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
|
| 239 | - $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
|
| 238 | + if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') { |
|
| 239 | + $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . $translation . $seg['mrk_succ_tags'] . "</mrk>"; |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | return $translation; |
@@ -251,10 +251,10 @@ discard block |
||
| 251 | 251 | * |
| 252 | 252 | * @return string |
| 253 | 253 | */ |
| 254 | - protected function createTargetTag( string $translation, string $stateProp ): string { |
|
| 254 | + protected function createTargetTag(string $translation, string $stateProp): string { |
|
| 255 | 255 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
| 256 | 256 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
| 257 | - $tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts[ 'raw_word_count' ] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts[ 'eq_word_count' ] . '</count></count-group>'; |
|
| 257 | + $tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts['raw_word_count'] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts['eq_word_count'] . '</count></count-group>'; |
|
| 258 | 258 | |
| 259 | 259 | return $tag; |
| 260 | 260 | |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | // we must reset the lastMrkId found because this is a new segment. |
| 271 | 271 | $lastMrkId = -1; |
| 272 | 272 | |
| 273 | - foreach ( $this->lastTransUnit as $pos => $seg ) { |
|
| 273 | + foreach ($this->lastTransUnit as $pos => $seg) { |
|
| 274 | 274 | |
| 275 | 275 | /* |
| 276 | 276 | * This routine works to respect the positional orders of markers. |
@@ -283,8 +283,8 @@ discard block |
||
| 283 | 283 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
| 284 | 284 | * If it is null leave it NULL |
| 285 | 285 | */ |
| 286 | - if ( (int)$seg[ "mrk_id" ] < 0 && $seg[ "mrk_id" ] !== null ) { |
|
| 287 | - $seg[ "mrk_id" ] = 0; |
|
| 286 | + if ((int)$seg["mrk_id"] < 0 && $seg["mrk_id"] !== null) { |
|
| 287 | + $seg["mrk_id"] = 0; |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /* |
@@ -293,39 +293,39 @@ discard block |
||
| 293 | 293 | * ( null <= -1 ) === true |
| 294 | 294 | * so, cast to int |
| 295 | 295 | */ |
| 296 | - if ( (int)$seg[ "mrk_id" ] <= $lastMrkId ) { |
|
| 296 | + if ((int)$seg["mrk_id"] <= $lastMrkId) { |
|
| 297 | 297 | break; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // update counts |
| 301 | - if ( !empty( $seg ) ) { |
|
| 302 | - $this->updateSegmentCounts( $seg ); |
|
| 301 | + if (!empty($seg)) { |
|
| 302 | + $this->updateSegmentCounts($seg); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | // delete translations so the prepareSegment |
| 306 | 306 | // will put source content in target tag |
| 307 | - if ( $this->sourceInTarget ) { |
|
| 308 | - $seg[ 'translation' ] = ''; |
|
| 307 | + if ($this->sourceInTarget) { |
|
| 308 | + $seg['translation'] = ''; |
|
| 309 | 309 | $this->resetCounts(); |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | // append $translation |
| 313 | - $translation = $this->prepareTranslation( $seg, $translation ); |
|
| 313 | + $translation = $this->prepareTranslation($seg, $translation); |
|
| 314 | 314 | |
| 315 | - $lastMrkId = $seg[ "mrk_id" ]; |
|
| 315 | + $lastMrkId = $seg["mrk_id"]; |
|
| 316 | 316 | |
| 317 | - [ $stateProp, $lastMrkState ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ], $lastMrkState ); |
|
| 317 | + [$stateProp, $lastMrkState] = StatusToStateAttribute::getState($this->xliffVersion, $seg['status'], $lastMrkState); |
|
| 318 | 318 | |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | //append translation |
| 322 | - return $this->createTargetTag( $translation, $stateProp ); |
|
| 322 | + return $this->createTargetTag($translation, $stateProp); |
|
| 323 | 323 | |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | protected function getCurrentSegment(): array { |
| 327 | - if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 328 | - return $this->segments[ $this->transUnits[ $this->currentTransUnitId ][ 0 ] ]; // TODO try to understand why here is needed to override the method and set 0 index hardcoded |
|
| 327 | + if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 328 | + return $this->segments[$this->transUnits[$this->currentTransUnitId][0]]; // TODO try to understand why here is needed to override the method and set 0 index hardcoded |
|
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | return []; |