@@ -9,12 +9,12 @@ discard block |
||
| 9 | 9 | /** |
| 10 | 10 | * @var array |
| 11 | 11 | */ |
| 12 | - private static array $versions_1 = [ '1.0', '1.1', '1.2' ]; |
|
| 12 | + private static array $versions_1 = ['1.0', '1.1', '1.2']; |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * @var array |
| 16 | 16 | */ |
| 17 | - private static array $versions_2 = [ '2.0', '2.1' ]; |
|
| 17 | + private static array $versions_2 = ['2.0', '2.1']; |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * @param string $xliffContent |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | * @throws NotSupportedVersionException |
| 24 | 24 | * @throws NotValidFileException |
| 25 | 25 | */ |
| 26 | - public static function detect( string $xliffContent ): int { |
|
| 27 | - preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $xliffContent, 0, 1000 ), $versionMatches ); |
|
| 26 | + public static function detect(string $xliffContent): int { |
|
| 27 | + preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($xliffContent, 0, 1000), $versionMatches); |
|
| 28 | 28 | |
| 29 | - if ( empty( $versionMatches ) ) { |
|
| 30 | - throw new NotValidFileException( 'This is not a valid xliff file' ); |
|
| 29 | + if (empty($versionMatches)) { |
|
| 30 | + throw new NotValidFileException('This is not a valid xliff file'); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - $version = $versionMatches[ 1 ]; |
|
| 33 | + $version = $versionMatches[1]; |
|
| 34 | 34 | |
| 35 | - return self::resolveVersion( $version ); |
|
| 35 | + return self::resolveVersion($version); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -41,15 +41,15 @@ discard block |
||
| 41 | 41 | * @return int |
| 42 | 42 | * @throws NotSupportedVersionException |
| 43 | 43 | */ |
| 44 | - private static function resolveVersion( string $version ): int { |
|
| 45 | - if ( in_array( $version, self::$versions_1 ) ) { |
|
| 44 | + private static function resolveVersion(string $version): int { |
|
| 45 | + if (in_array($version, self::$versions_1)) { |
|
| 46 | 46 | return 1; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - if ( in_array( $version, self::$versions_2 ) ) { |
|
| 49 | + if (in_array($version, self::$versions_2)) { |
|
| 50 | 50 | return 2; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - throw new NotSupportedVersionException( 'Not supported version' ); |
|
| 53 | + throw new NotSupportedVersionException('Not supported version'); |
|
| 54 | 54 | } |
| 55 | 55 | } |
@@ -28,14 +28,14 @@ |
||
| 28 | 28 | ?LoggerInterface $logger = null, |
| 29 | 29 | ?XliffReplacerCallbackInterface $callback = null |
| 30 | 30 | ): AbstractXliffReplacer { |
| 31 | - $info = XliffProprietaryDetect::getInfo( $originalXliffPath ); |
|
| 31 | + $info = XliffProprietaryDetect::getInfo($originalXliffPath); |
|
| 32 | 32 | |
| 33 | - if ( $info[ 'version' ] == 1 && $info[ 'proprietary_short_name' ] !== 'trados' ) { |
|
| 34 | - return new Xliff12( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback ); |
|
| 35 | - } elseif ( $info[ 'version' ] == 2 ) { |
|
| 36 | - return new Xliff20( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback ); |
|
| 33 | + if ($info['version'] == 1 && $info['proprietary_short_name'] !== 'trados') { |
|
| 34 | + return new Xliff12($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback); |
|
| 35 | + } elseif ($info['version'] == 2) { |
|
| 36 | + return new Xliff20($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - return new XliffSdl( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback ); |
|
| 39 | + return new XliffSdl($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback); |
|
| 40 | 40 | } |
| 41 | 41 | } |
@@ -12,5 +12,5 @@ |
||
| 12 | 12 | * |
| 13 | 13 | * @return bool |
| 14 | 14 | */ |
| 15 | - public function thereAreErrors( int $segmentId, string $segment, string $translation, ?array $dataRefMap = [], ?string $error = null ): bool; |
|
| 15 | + public function thereAreErrors(int $segmentId, string $segment, string $translation, ?array $dataRefMap = [], ?string $error = null): bool; |
|
| 16 | 16 | } |
@@ -22,11 +22,11 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @return array |
| 24 | 24 | */ |
| 25 | - public static function getInfoFromXliffContent( string $xliffContent ): array { |
|
| 25 | + public static function getInfoFromXliffContent(string $xliffContent): array { |
|
| 26 | 26 | self::reset(); |
| 27 | - $tmp = self::getFirst1024CharsFromXliff( $xliffContent ); |
|
| 27 | + $tmp = self::getFirst1024CharsFromXliff($xliffContent); |
|
| 28 | 28 | |
| 29 | - return self::getInfoFromTmp( $tmp ); |
|
| 29 | + return self::getInfoFromTmp($tmp); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
@@ -34,12 +34,12 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @return array |
| 36 | 36 | */ |
| 37 | - public static function getInfo( string $fullPathToFile ): array { |
|
| 37 | + public static function getInfo(string $fullPathToFile): array { |
|
| 38 | 38 | self::reset(); |
| 39 | - $tmp = self::getFirst1024CharsFromXliff( null, $fullPathToFile ); |
|
| 40 | - self::$fileType[ 'info' ] = Files::pathInfo( $fullPathToFile ); |
|
| 39 | + $tmp = self::getFirst1024CharsFromXliff(null, $fullPathToFile); |
|
| 40 | + self::$fileType['info'] = Files::pathInfo($fullPathToFile); |
|
| 41 | 41 | |
| 42 | - return self::getInfoFromTmp( $tmp ); |
|
| 42 | + return self::getInfoFromTmp($tmp); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -47,21 +47,21 @@ discard block |
||
| 47 | 47 | * |
| 48 | 48 | * @return array |
| 49 | 49 | */ |
| 50 | - private static function getInfoFromTmp( array $tmp ): array { |
|
| 50 | + private static function getInfoFromTmp(array $tmp): array { |
|
| 51 | 51 | try { |
| 52 | - self::checkVersion( $tmp ); |
|
| 53 | - } catch ( Exception $ignore ) { |
|
| 52 | + self::checkVersion($tmp); |
|
| 53 | + } catch (Exception $ignore) { |
|
| 54 | 54 | // do nothing |
| 55 | 55 | // self::$fileType[ 'version' ] is left empty |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | // run CheckXliffProprietaryPipeline |
| 59 | - $pipeline = self::runPipeline( $tmp ); |
|
| 59 | + $pipeline = self::runPipeline($tmp); |
|
| 60 | 60 | |
| 61 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
| 62 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
| 63 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
| 64 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
| 61 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
| 62 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
| 63 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
| 64 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
| 65 | 65 | |
| 66 | 66 | return self::$fileType; |
| 67 | 67 | } |
@@ -71,12 +71,12 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return array |
| 73 | 73 | */ |
| 74 | - private static function runPipeline( ?array $tmp = [] ): array { |
|
| 75 | - $pipeline = new CheckXliffProprietaryPipeline( $tmp ); |
|
| 76 | - $pipeline->addCheck( new CheckSDL() ); |
|
| 77 | - $pipeline->addCheck( new CheckGlobalSight() ); |
|
| 78 | - $pipeline->addCheck( new CheckMateCATConverter() ); |
|
| 79 | - $pipeline->addCheck( new CheckXliffVersion2() ); |
|
| 74 | + private static function runPipeline(?array $tmp = []): array { |
|
| 75 | + $pipeline = new CheckXliffProprietaryPipeline($tmp); |
|
| 76 | + $pipeline->addCheck(new CheckSDL()); |
|
| 77 | + $pipeline->addCheck(new CheckGlobalSight()); |
|
| 78 | + $pipeline->addCheck(new CheckMateCATConverter()); |
|
| 79 | + $pipeline->addCheck(new CheckXliffVersion2()); |
|
| 80 | 80 | |
| 81 | 81 | return $pipeline->run(); |
| 82 | 82 | } |
@@ -98,9 +98,9 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return string |
| 100 | 100 | */ |
| 101 | - private static function getFirst1024CharsFromString( ?string $stringData ): string { |
|
| 102 | - if ( !empty( $stringData ) ) { |
|
| 103 | - return substr( $stringData, 0, 1024 ); |
|
| 101 | + private static function getFirst1024CharsFromString(?string $stringData): string { |
|
| 102 | + if (!empty($stringData)) { |
|
| 103 | + return substr($stringData, 0, 1024); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | return ''; |
@@ -111,13 +111,13 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * @return string |
| 113 | 113 | */ |
| 114 | - private static function getFirst1024CharsFromFile( ?string $fullPathToFile ): string { |
|
| 114 | + private static function getFirst1024CharsFromFile(?string $fullPathToFile): string { |
|
| 115 | 115 | $stringData = ''; |
| 116 | - if ( !empty( $fullPathToFile ) && is_file( $fullPathToFile ) ) { |
|
| 117 | - $file_pointer = fopen( "$fullPathToFile", 'r' ); |
|
| 116 | + if (!empty($fullPathToFile) && is_file($fullPathToFile)) { |
|
| 117 | + $file_pointer = fopen("$fullPathToFile", 'r'); |
|
| 118 | 118 | // Checking Requirements (By specs, I know that xliff version is in the first 1KB) |
| 119 | - $stringData = fread( $file_pointer, 1024 ); |
|
| 120 | - fclose( $file_pointer ); |
|
| 119 | + $stringData = fread($file_pointer, 1024); |
|
| 120 | + fclose($file_pointer); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | return $stringData; |
@@ -130,13 +130,13 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return string[] |
| 132 | 132 | */ |
| 133 | - private static function getFirst1024CharsFromXliff( ?string $stringData = null, string $fullPathToFile = null ): ?array { |
|
| 134 | - $stringData = static::getFirst1024CharsFromString( $stringData ); |
|
| 135 | - if ( empty( $stringData ) ) { |
|
| 136 | - $stringData = static::getFirst1024CharsFromFile( $fullPathToFile ); |
|
| 133 | + private static function getFirst1024CharsFromXliff(?string $stringData = null, string $fullPathToFile = null): ?array { |
|
| 134 | + $stringData = static::getFirst1024CharsFromString($stringData); |
|
| 135 | + if (empty($stringData)) { |
|
| 136 | + $stringData = static::getFirst1024CharsFromFile($fullPathToFile); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - return !empty( $stringData ) ? [ $stringData ] : []; |
|
| 139 | + return !empty($stringData) ? [$stringData] : []; |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | * @throws NotSupportedVersionException |
| 146 | 146 | * @throws NotValidFileException |
| 147 | 147 | */ |
| 148 | - protected static function checkVersion( array $tmp ) { |
|
| 149 | - if ( isset( $tmp[ 0 ] ) ) { |
|
| 150 | - self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] ); |
|
| 148 | + protected static function checkVersion(array $tmp) { |
|
| 149 | + if (isset($tmp[0])) { |
|
| 150 | + self::$fileType['version'] = XliffVersionDetector::detect($tmp[0]); |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | |
@@ -158,20 +158,20 @@ discard block |
||
| 158 | 158 | * @throws NotSupportedVersionException |
| 159 | 159 | * @throws NotValidFileException |
| 160 | 160 | */ |
| 161 | - public static function getInfoByStringData( string $stringData ): array { |
|
| 161 | + public static function getInfoByStringData(string $stringData): array { |
|
| 162 | 162 | self::reset(); |
| 163 | 163 | |
| 164 | - $tmp = self::getFirst1024CharsFromXliff( $stringData ); |
|
| 165 | - self::$fileType[ 'info' ] = []; |
|
| 166 | - self::checkVersion( $tmp ); |
|
| 164 | + $tmp = self::getFirst1024CharsFromXliff($stringData); |
|
| 165 | + self::$fileType['info'] = []; |
|
| 166 | + self::checkVersion($tmp); |
|
| 167 | 167 | |
| 168 | 168 | // run CheckXliffProprietaryPipeline |
| 169 | - $pipeline = self::runPipeline( $tmp ); |
|
| 169 | + $pipeline = self::runPipeline($tmp); |
|
| 170 | 170 | |
| 171 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
| 172 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
| 173 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
| 174 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
| 171 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
| 172 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
| 173 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
| 174 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
| 175 | 175 | |
| 176 | 176 | return self::$fileType; |
| 177 | 177 | } |
@@ -183,33 +183,33 @@ discard block |
||
| 183 | 183 | * |
| 184 | 184 | * @return bool|int |
| 185 | 185 | */ |
| 186 | - public static function fileMustBeConverted( string $fullPath, bool $enforceOnXliff = false, ?string $filterAddress = null ) { |
|
| 186 | + public static function fileMustBeConverted(string $fullPath, bool $enforceOnXliff = false, ?string $filterAddress = null) { |
|
| 187 | 187 | $convert = true; |
| 188 | 188 | |
| 189 | - $fileType = self::getInfo( $fullPath ); |
|
| 190 | - $memoryFileType = Files::getMemoryFileType( $fullPath ); |
|
| 189 | + $fileType = self::getInfo($fullPath); |
|
| 190 | + $memoryFileType = Files::getMemoryFileType($fullPath); |
|
| 191 | 191 | |
| 192 | - if ( Files::isXliff( $fullPath ) || $memoryFileType ) { |
|
| 193 | - if ( !empty( $filterAddress ) ) { |
|
| 192 | + if (Files::isXliff($fullPath) || $memoryFileType) { |
|
| 193 | + if (!empty($filterAddress)) { |
|
| 194 | 194 | |
| 195 | 195 | //conversion enforce |
| 196 | - if ( !$enforceOnXliff ) { |
|
| 196 | + if (!$enforceOnXliff) { |
|
| 197 | 197 | |
| 198 | 198 | //if file is not proprietary AND Enforce is disabled |
| 199 | 199 | //we take it as is |
| 200 | - if ( !$fileType[ 'proprietary' ] || $memoryFileType ) { |
|
| 200 | + if (!$fileType['proprietary'] || $memoryFileType) { |
|
| 201 | 201 | $convert = false; |
| 202 | 202 | //ok don't convert a standard sdlxliff |
| 203 | 203 | } |
| 204 | 204 | } else { |
| 205 | 205 | //if conversion enforce is active |
| 206 | 206 | //we force all xliff files but not files produced by SDL Studio because we can handle them |
| 207 | - if ( in_array( $fileType[ 'proprietary_short_name' ], [ 'matecat_converter', 'trados', 'xliff_v2' ] ) || $memoryFileType ) { |
|
| 207 | + if (in_array($fileType['proprietary_short_name'], ['matecat_converter', 'trados', 'xliff_v2']) || $memoryFileType) { |
|
| 208 | 208 | $convert = false; |
| 209 | 209 | //ok don't convert a standard sdlxliff |
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | - } elseif ( $fileType[ 'proprietary' ] ) { |
|
| 212 | + } elseif ($fileType['proprietary']) { |
|
| 213 | 213 | |
| 214 | 214 | /** |
| 215 | 215 | * Application misconfiguration. |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return array|mixed |
| 20 | 20 | */ |
| 21 | - public static function pathInfo( string $path, ?int $options = 15 ) { |
|
| 22 | - $rawPath = explode( DIRECTORY_SEPARATOR, $path ); |
|
| 21 | + public static function pathInfo(string $path, ?int $options = 15) { |
|
| 22 | + $rawPath = explode(DIRECTORY_SEPARATOR, $path); |
|
| 23 | 23 | |
| 24 | - $basename = array_pop( $rawPath ); |
|
| 25 | - $dirname = implode( DIRECTORY_SEPARATOR, $rawPath ); |
|
| 24 | + $basename = array_pop($rawPath); |
|
| 25 | + $dirname = implode(DIRECTORY_SEPARATOR, $rawPath); |
|
| 26 | 26 | |
| 27 | - $explodedFileName = explode( ".", $basename ); |
|
| 28 | - $extension = strtolower( array_pop( $explodedFileName ) ); |
|
| 29 | - $filename = implode( ".", $explodedFileName ); |
|
| 27 | + $explodedFileName = explode(".", $basename); |
|
| 28 | + $extension = strtolower(array_pop($explodedFileName)); |
|
| 29 | + $filename = implode(".", $explodedFileName); |
|
| 30 | 30 | |
| 31 | 31 | $returnArray = []; |
| 32 | 32 | |
@@ -39,17 +39,17 @@ discard block |
||
| 39 | 39 | |
| 40 | 40 | // foreach flag, add in $return_array the corresponding field, |
| 41 | 41 | // obtained by variable name correspondence |
| 42 | - foreach ( $flagMap as $field => $i ) { |
|
| 42 | + foreach ($flagMap as $field => $i) { |
|
| 43 | 43 | //binary AND |
| 44 | - if ( ( $options & $i ) > 0 ) { |
|
| 44 | + if (($options & $i) > 0) { |
|
| 45 | 45 | //variable substitution: $field can be one between 'dirname', 'basename', 'extension', 'filename' |
| 46 | 46 | // $$field gets the value of the variable named $field |
| 47 | - $returnArray[ $field ] = $$field; |
|
| 47 | + $returnArray[$field] = $$field; |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if ( count( $returnArray ) == 1 ) { |
|
| 52 | - $returnArray = array_pop( $returnArray ); |
|
| 51 | + if (count($returnArray) == 1) { |
|
| 52 | + $returnArray = array_pop($returnArray); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | return $returnArray; |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @return ?string |
| 62 | 62 | */ |
| 63 | - public static function getExtension( $path ): ?string { |
|
| 64 | - $pathInfo = self::pathInfo( $path ); |
|
| 63 | + public static function getExtension($path): ?string { |
|
| 64 | + $pathInfo = self::pathInfo($path); |
|
| 65 | 65 | |
| 66 | - if ( empty( $pathInfo ) ) { |
|
| 66 | + if (empty($pathInfo)) { |
|
| 67 | 67 | return null; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - return strtolower( $pathInfo[ 'extension' ] ); |
|
| 70 | + return strtolower($pathInfo['extension']); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -75,14 +75,14 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return bool |
| 77 | 77 | */ |
| 78 | - public static function isXliff( ?string $path ): bool { |
|
| 79 | - $extension = self::getExtension( $path ); |
|
| 78 | + public static function isXliff(?string $path): bool { |
|
| 79 | + $extension = self::getExtension($path); |
|
| 80 | 80 | |
| 81 | - if ( !$extension ) { |
|
| 81 | + if (!$extension) { |
|
| 82 | 82 | return false; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - switch ( $extension ) { |
|
| 85 | + switch ($extension) { |
|
| 86 | 86 | case 'xliff': |
| 87 | 87 | case 'sdlxliff': |
| 88 | 88 | case 'tmx': |
@@ -98,14 +98,14 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool|string |
| 100 | 100 | */ |
| 101 | - public static function getMemoryFileType( string $path ) { |
|
| 102 | - $pathInfo = self::pathInfo( $path ); |
|
| 101 | + public static function getMemoryFileType(string $path) { |
|
| 102 | + $pathInfo = self::pathInfo($path); |
|
| 103 | 103 | |
| 104 | - if ( empty( $pathInfo ) ) { |
|
| 104 | + if (empty($pathInfo)) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - switch ( strtolower( $pathInfo[ 'extension' ] ) ) { |
|
| 108 | + switch (strtolower($pathInfo['extension'])) { |
|
| 109 | 109 | case 'tmx': |
| 110 | 110 | return 'tmx'; |
| 111 | 111 | default: |
@@ -118,8 +118,8 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @return bool |
| 120 | 120 | */ |
| 121 | - public static function isTMXFile( $path ): bool { |
|
| 122 | - return self::getMemoryFileType( $path ) === 'tmx'; |
|
| 121 | + public static function isTMXFile($path): bool { |
|
| 122 | + return self::getMemoryFileType($path) === 'tmx'; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | * @return bool |
| 129 | 129 | */ |
| 130 | - public static function isGlossaryFile( $path ): bool { |
|
| 131 | - return self::getMemoryFileType( $path ) === 'glossary'; // return false |
|
| 130 | + public static function isGlossaryFile($path): bool { |
|
| 131 | + return self::getMemoryFileType($path) === 'glossary'; // return false |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | ?string $lastMrkState = null |
| 27 | 27 | ): array { |
| 28 | 28 | |
| 29 | - $status = empty( $status ) ? TranslationStatus::STATUS_APPROVED2 : $status; |
|
| 29 | + $status = empty($status) ? TranslationStatus::STATUS_APPROVED2 : $status; |
|
| 30 | 30 | |
| 31 | 31 | $stateLevelsMap = [ |
| 32 | 32 | TranslationStatus::STATUS_APPROVED2 => 100, |
@@ -37,26 +37,26 @@ discard block |
||
| 37 | 37 | TranslationStatus::STATUS_NEW => 50 |
| 38 | 38 | ]; |
| 39 | 39 | |
| 40 | - $orderedValues = array_flip( $stateLevelsMap ); |
|
| 40 | + $orderedValues = array_flip($stateLevelsMap); |
|
| 41 | 41 | |
| 42 | 42 | // Define state mappings for different statuses |
| 43 | 43 | $stateMap = [ |
| 44 | - TranslationStatus::STATUS_APPROVED2 => [ "state=\"final\"", TranslationStatus::STATUS_APPROVED2 ], |
|
| 44 | + TranslationStatus::STATUS_APPROVED2 => ["state=\"final\"", TranslationStatus::STATUS_APPROVED2], |
|
| 45 | 45 | TranslationStatus::STATUS_APPROVED => [ |
| 46 | - ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"", |
|
| 46 | + ($xliffVersion === 2) ? "state=\"reviewed\"" : "state=\"signed-off\"", |
|
| 47 | 47 | TranslationStatus::STATUS_APPROVED |
| 48 | 48 | ], |
| 49 | - TranslationStatus::STATUS_TRANSLATED => [ "state=\"translated\"", TranslationStatus::STATUS_TRANSLATED ], |
|
| 49 | + TranslationStatus::STATUS_TRANSLATED => ["state=\"translated\"", TranslationStatus::STATUS_TRANSLATED], |
|
| 50 | 50 | TranslationStatus::STATUS_REJECTED => [ |
| 51 | - ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"", |
|
| 51 | + ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"needs-review-translation\"", |
|
| 52 | 52 | TranslationStatus::STATUS_REJECTED |
| 53 | 53 | ], |
| 54 | 54 | TranslationStatus::STATUS_NEW => [ |
| 55 | - ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"", |
|
| 55 | + ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"", |
|
| 56 | 56 | TranslationStatus::STATUS_NEW |
| 57 | 57 | ], |
| 58 | 58 | TranslationStatus::STATUS_DRAFT => [ |
| 59 | - ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"", |
|
| 59 | + ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"", |
|
| 60 | 60 | TranslationStatus::STATUS_DRAFT |
| 61 | 61 | ], |
| 62 | 62 | ]; |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | // ---> $seg[ 'status' ] is NULL |
| 69 | 69 | // If lastMrkState is empty |
| 70 | 70 | $minStatus = min( |
| 71 | - $stateLevelsMap[ $status ], |
|
| 72 | - ( $stateLevelsMap[ $lastMrkState ] ?? $stateLevelsMap[ TranslationStatus::STATUS_NEW ] ) |
|
| 71 | + $stateLevelsMap[$status], |
|
| 72 | + ($stateLevelsMap[$lastMrkState] ?? $stateLevelsMap[TranslationStatus::STATUS_NEW]) |
|
| 73 | 73 | ); |
| 74 | 74 | |
| 75 | 75 | // If the last mark state is set, get the minimum value, otherwise get the current state |
| 76 | - [ $state_prop, $lastMrkState ] = empty( $lastMrkState ) ? $stateMap[ $status ] : $stateMap[ $orderedValues[ $minStatus ] ]; |
|
| 76 | + [$state_prop, $lastMrkState] = empty($lastMrkState) ? $stateMap[$status] : $stateMap[$orderedValues[$minStatus]]; |
|
| 77 | 77 | |
| 78 | - return [ $state_prop, $lastMrkState ]; |
|
| 78 | + return [$state_prop, $lastMrkState]; |
|
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | |
@@ -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 | } |
@@ -7,29 +7,29 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | abstract class AbstractXliffReplacer { |
| 9 | 9 | protected $originalFP; |
| 10 | - protected $outputFP; // output stream pointer |
|
| 10 | + protected $outputFP; // output stream pointer |
|
| 11 | 11 | |
| 12 | - protected string $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
| 13 | - protected bool $inTU = false; // flag to check whether we are in a <trans-unit> |
|
| 14 | - protected bool $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
| 15 | - protected bool $inAltTrans = false; // flag to check whether we are in an <alt-trans> (xliff 1.2) or <mtc:matches> (xliff 2.0) |
|
| 12 | + protected string $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
| 13 | + protected bool $inTU = false; // flag to check whether we are in a <trans-unit> |
|
| 14 | + protected bool $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
| 15 | + protected bool $inAltTrans = false; // flag to check whether we are in an <alt-trans> (xliff 1.2) or <mtc:matches> (xliff 2.0) |
|
| 16 | 16 | protected string $alternativeMatchesTag = ""; // polymorphic tag name for xliff 1.2 and 2.0 |
| 17 | - protected bool $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
| 18 | - protected bool $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
| 19 | - protected string $CDATABuffer = ""; // buffer for special tag |
|
| 20 | - protected string $namespace = ""; // Custom namespace |
|
| 21 | - protected bool $bufferIsActive = false; // flag for buffeting |
|
| 17 | + protected bool $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
| 18 | + protected bool $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
| 19 | + protected string $CDATABuffer = ""; // buffer for special tag |
|
| 20 | + protected string $namespace = ""; // Custom namespace |
|
| 21 | + protected bool $bufferIsActive = false; // flag for buffeting |
|
| 22 | 22 | |
| 23 | - protected int $offset = 0; // offset for SAX pointer |
|
| 23 | + protected int $offset = 0; // offset for SAX pointer |
|
| 24 | 24 | |
| 25 | - protected string $currentBuffer; // the current piece of text it's been parsed |
|
| 26 | - protected int $len; // length of the currentBuffer |
|
| 27 | - protected array $segments; // array of translations |
|
| 25 | + protected string $currentBuffer; // the current piece of text it's been parsed |
|
| 26 | + protected int $len; // length of the currentBuffer |
|
| 27 | + protected array $segments; // array of translations |
|
| 28 | 28 | protected array $lastTransUnit = []; |
| 29 | 29 | protected int $segmentInUnitPosition = 0; |
| 30 | - protected ?string $currentTransUnitId = null; // id of current <trans-unit> |
|
| 30 | + protected ?string $currentTransUnitId = null; // id of current <trans-unit> |
|
| 31 | 31 | protected ?string $currentTransUnitIsTranslatable = null; // 'translate' attribute of current <trans-unit> |
| 32 | - protected bool $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
| 32 | + protected bool $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
| 33 | 33 | protected string $targetLang; |
| 34 | 34 | protected bool $sourceInTarget = false; |
| 35 | 35 | |
@@ -79,8 +79,8 @@ discard block |
||
| 79 | 79 | XliffReplacerCallbackInterface $callback = null |
| 80 | 80 | ) { |
| 81 | 81 | self::$INTERNAL_TAG_PLACEHOLDER = $this->getInternalTagPlaceholder(); |
| 82 | - $this->createOutputFileIfDoesNotExist( $outputFilePath ); |
|
| 83 | - $this->setFileDescriptors( $originalXliffPath, $outputFilePath ); |
|
| 82 | + $this->createOutputFileIfDoesNotExist($outputFilePath); |
|
| 83 | + $this->setFileDescriptors($originalXliffPath, $outputFilePath); |
|
| 84 | 84 | $this->xliffVersion = $xliffVersion; |
| 85 | 85 | $this->segments = $segments; |
| 86 | 86 | $this->targetLang = $trgLang; |
@@ -91,17 +91,17 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | public function replaceTranslation() { |
| 94 | - fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' ); |
|
| 94 | + fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>'); |
|
| 95 | 95 | |
| 96 | 96 | //create Sax parser |
| 97 | 97 | $xmlParser = $this->initSaxParser(); |
| 98 | 98 | |
| 99 | - while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) { |
|
| 99 | + while ($this->currentBuffer = fread($this->originalFP, 4096)) { |
|
| 100 | 100 | /* |
| 101 | 101 | preprocess file |
| 102 | 102 | */ |
| 103 | 103 | // obfuscate entities because sax automatically does html_entity_decode |
| 104 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
| 104 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
| 105 | 105 | |
| 106 | 106 | //avoid cutting entities in half: |
| 107 | 107 | //the last fread could have truncated an entity (say, '<' in '&l'), thus invalidating the escaping |
@@ -109,27 +109,27 @@ discard block |
||
| 109 | 109 | // 9 is the max length of an entity. So, suppose that the & is at the end of buffer, |
| 110 | 110 | // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end |
| 111 | 111 | //it can't be an entity, exit the loop |
| 112 | - while ( true ) { |
|
| 113 | - $_ampPos = strpos( $temporary_check_buffer, '&' ); |
|
| 112 | + while (true) { |
|
| 113 | + $_ampPos = strpos($temporary_check_buffer, '&'); |
|
| 114 | 114 | |
| 115 | 115 | //check for real entity or escape it to safely exit from the loop!!! |
| 116 | - if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) { |
|
| 116 | + if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) { |
|
| 117 | 117 | break; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | //if an entity is still present, fetch some more and repeat the escaping |
| 121 | - $this->currentBuffer .= fread( $this->originalFP, 9 ); |
|
| 122 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
| 121 | + $this->currentBuffer .= fread($this->originalFP, 9); |
|
| 122 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | //free stuff outside the loop |
| 126 | - unset( $temporary_check_buffer ); |
|
| 126 | + unset($temporary_check_buffer); |
|
| 127 | 127 | |
| 128 | - $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
| 129 | - $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
| 128 | + $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
| 129 | + $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
| 130 | 130 | |
| 131 | 131 | //get length of chunk |
| 132 | - $this->len = strlen( $this->currentBuffer ); |
|
| 132 | + $this->len = strlen($this->currentBuffer); |
|
| 133 | 133 | |
| 134 | 134 | /* |
| 135 | 135 | * Get the accumulated this->offset in the document: |
@@ -139,12 +139,12 @@ discard block |
||
| 139 | 139 | $this->offset += $this->len; |
| 140 | 140 | |
| 141 | 141 | //parse chunk of text |
| 142 | - $this->runParser( $xmlParser ); |
|
| 142 | + $this->runParser($xmlParser); |
|
| 143 | 143 | |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // close Sax parser |
| 147 | - $this->closeSaxParser( $xmlParser ); |
|
| 147 | + $this->closeSaxParser($xmlParser); |
|
| 148 | 148 | |
| 149 | 149 | } |
| 150 | 150 | |
@@ -153,15 +153,15 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @return void |
| 155 | 155 | */ |
| 156 | - protected function runParser( $xmlParser ) { |
|
| 156 | + protected function runParser($xmlParser) { |
|
| 157 | 157 | //parse chunk of text |
| 158 | - if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) { |
|
| 158 | + if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) { |
|
| 159 | 159 | //if unable, raise an exception |
| 160 | - throw new RuntimeException( sprintf( |
|
| 160 | + throw new RuntimeException(sprintf( |
|
| 161 | 161 | "XML error: %s at line %d", |
| 162 | - xml_error_string( xml_get_error_code( $xmlParser ) ), |
|
| 163 | - xml_get_current_line_number( $xmlParser ) |
|
| 164 | - ) ); |
|
| 162 | + xml_error_string(xml_get_error_code($xmlParser)), |
|
| 163 | + xml_get_current_line_number($xmlParser) |
|
| 164 | + )); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
@@ -170,18 +170,18 @@ discard block |
||
| 170 | 170 | * |
| 171 | 171 | * @return string |
| 172 | 172 | */ |
| 173 | - protected function getLastCharacter( $parser ): string { |
|
| 173 | + protected function getLastCharacter($parser): string { |
|
| 174 | 174 | |
| 175 | 175 | //this logic helps detecting empty tags |
| 176 | 176 | //get current position of SAX pointer in all the stream of data is has read so far: |
| 177 | 177 | //it points at the end of current tag |
| 178 | - $idx = xml_get_current_byte_index( $parser ); |
|
| 178 | + $idx = xml_get_current_byte_index($parser); |
|
| 179 | 179 | |
| 180 | 180 | //check whether the bounds of current tag are entirely in current buffer or the end of the current tag |
| 181 | 181 | //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
| 182 | 182 | //this check is necessary because we may have truncated a tag in half with current read, |
| 183 | 183 | //and the other half may be encountered in the next buffer it will be passed |
| 184 | - return $this->currentBuffer[ $idx - $this->offset ] ?? $this->currentBuffer[ $this->len - 1 ]; |
|
| 184 | + return $this->currentBuffer[$idx - $this->offset] ?? $this->currentBuffer[$this->len - 1]; |
|
| 185 | 185 | |
| 186 | 186 | } |
| 187 | 187 | |
@@ -192,19 +192,19 @@ discard block |
||
| 192 | 192 | return "§" . |
| 193 | 193 | substr( |
| 194 | 194 | str_replace( |
| 195 | - [ '+', '/' ], |
|
| 195 | + ['+', '/'], |
|
| 196 | 196 | '', |
| 197 | - base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) ) |
|
| 197 | + base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong)) |
|
| 198 | 198 | ), |
| 199 | 199 | 0, |
| 200 | 200 | 4 |
| 201 | 201 | ); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - private function createOutputFileIfDoesNotExist( string $outputFilePath ) { |
|
| 204 | + private function createOutputFileIfDoesNotExist(string $outputFilePath) { |
|
| 205 | 205 | // create output file |
| 206 | - if ( !file_exists( $outputFilePath ) ) { |
|
| 207 | - touch( $outputFilePath ); |
|
| 206 | + if (!file_exists($outputFilePath)) { |
|
| 207 | + touch($outputFilePath); |
|
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | 210 | |
@@ -212,13 +212,13 @@ discard block |
||
| 212 | 212 | * @param string $originalXliffPath |
| 213 | 213 | * @param string $outputFilePath |
| 214 | 214 | */ |
| 215 | - private function setFileDescriptors( string $originalXliffPath, string $outputFilePath ) { |
|
| 216 | - $this->outputFP = fopen( $outputFilePath, 'w+' ); |
|
| 215 | + private function setFileDescriptors(string $originalXliffPath, string $outputFilePath) { |
|
| 216 | + $this->outputFP = fopen($outputFilePath, 'w+'); |
|
| 217 | 217 | |
| 218 | 218 | $streamArgs = null; |
| 219 | 219 | |
| 220 | - if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) { |
|
| 221 | - throw new RuntimeException( "could not open XML input" ); |
|
| 220 | + if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) { |
|
| 221 | + throw new RuntimeException("could not open XML input"); |
|
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | |
@@ -228,12 +228,12 @@ discard block |
||
| 228 | 228 | public function __destruct() { |
| 229 | 229 | //this stream can be closed outside the class |
| 230 | 230 | //to permit multiple concurrent downloads, so suppress warnings |
| 231 | - if ( is_resource( $this->originalFP ) ) { |
|
| 232 | - fclose( $this->originalFP ); |
|
| 231 | + if (is_resource($this->originalFP)) { |
|
| 232 | + fclose($this->originalFP); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - if ( is_resource( $this->outputFP ) ) { |
|
| 236 | - fclose( $this->outputFP ); |
|
| 235 | + if (is_resource($this->outputFP)) { |
|
| 236 | + fclose($this->outputFP); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | } |
@@ -244,11 +244,11 @@ discard block |
||
| 244 | 244 | * @return resource |
| 245 | 245 | */ |
| 246 | 246 | protected function initSaxParser() { |
| 247 | - $xmlSaxParser = xml_parser_create( 'UTF-8' ); |
|
| 248 | - xml_set_object( $xmlSaxParser, $this ); |
|
| 249 | - xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false ); |
|
| 250 | - xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' ); |
|
| 251 | - xml_set_character_data_handler( $xmlSaxParser, 'characterData' ); |
|
| 247 | + $xmlSaxParser = xml_parser_create('UTF-8'); |
|
| 248 | + xml_set_object($xmlSaxParser, $this); |
|
| 249 | + xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false); |
|
| 250 | + xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose'); |
|
| 251 | + xml_set_character_data_handler($xmlSaxParser, 'characterData'); |
|
| 252 | 252 | |
| 253 | 253 | return $xmlSaxParser; |
| 254 | 254 | } |
@@ -256,8 +256,8 @@ discard block |
||
| 256 | 256 | /** |
| 257 | 257 | * @param resource $xmlSaxParser |
| 258 | 258 | */ |
| 259 | - protected function closeSaxParser( $xmlSaxParser ) { |
|
| 260 | - xml_parser_free( $xmlSaxParser ); |
|
| 259 | + protected function closeSaxParser($xmlSaxParser) { |
|
| 260 | + xml_parser_free($xmlSaxParser); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return mixed |
| 269 | 269 | */ |
| 270 | - abstract protected function tagOpen( $parser, string $name, array $attr ); |
|
| 270 | + abstract protected function tagOpen($parser, string $name, array $attr); |
|
| 271 | 271 | |
| 272 | 272 | /** |
| 273 | 273 | * @param resource $parser |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | * |
| 276 | 276 | * @return mixed |
| 277 | 277 | */ |
| 278 | - abstract protected function tagClose( $parser, string $name ); |
|
| 278 | + abstract protected function tagClose($parser, string $name); |
|
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | 281 | * @param resource $parser |
@@ -283,11 +283,11 @@ discard block |
||
| 283 | 283 | * |
| 284 | 284 | * @return mixed |
| 285 | 285 | */ |
| 286 | - protected function characterData( $parser, string $data ): void { |
|
| 286 | + protected function characterData($parser, string $data): void { |
|
| 287 | 287 | // don't write <target> data |
| 288 | - if ( !$this->inTarget && !$this->bufferIsActive ) { |
|
| 289 | - $this->postProcAndFlush( $this->outputFP, $data ); |
|
| 290 | - } elseif ( $this->bufferIsActive ) { |
|
| 288 | + if (!$this->inTarget && !$this->bufferIsActive) { |
|
| 289 | + $this->postProcAndFlush($this->outputFP, $data); |
|
| 290 | + } elseif ($this->bufferIsActive) { |
|
| 291 | 291 | $this->CDATABuffer .= $data; |
| 292 | 292 | } |
| 293 | 293 | } |
@@ -299,19 +299,19 @@ discard block |
||
| 299 | 299 | * @param string $data |
| 300 | 300 | * @param bool $treatAsCDATA |
| 301 | 301 | */ |
| 302 | - protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) { |
|
| 302 | + protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false) { |
|
| 303 | 303 | //postprocess string |
| 304 | - $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data ); |
|
| 305 | - $data = str_replace( ' ', ' ', $data ); |
|
| 306 | - if ( !$treatAsCDATA ) { |
|
| 304 | + $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data); |
|
| 305 | + $data = str_replace(' ', ' ', $data); |
|
| 306 | + if (!$treatAsCDATA) { |
|
| 307 | 307 | //unix2dos |
| 308 | - $data = str_replace( "\r\n", "\r", $data ); |
|
| 309 | - $data = str_replace( "\n", "\r", $data ); |
|
| 310 | - $data = str_replace( "\r", "\r\n", $data ); |
|
| 308 | + $data = str_replace("\r\n", "\r", $data); |
|
| 309 | + $data = str_replace("\n", "\r", $data); |
|
| 310 | + $data = str_replace("\r", "\r\n", $data); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | //flush to disk |
| 314 | - fwrite( $fp, $data ); |
|
| 314 | + fwrite($fp, $data); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | /** |
@@ -320,19 +320,19 @@ discard block |
||
| 320 | 320 | * |
| 321 | 321 | * @return void |
| 322 | 322 | */ |
| 323 | - protected function handleOpenUnit( string $name, array $attr ) { |
|
| 323 | + protected function handleOpenUnit(string $name, array $attr) { |
|
| 324 | 324 | |
| 325 | 325 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
| 326 | - if ( $this->tuTagName === $name ) { |
|
| 326 | + if ($this->tuTagName === $name) { |
|
| 327 | 327 | $this->inTU = true; |
| 328 | 328 | |
| 329 | 329 | // get id |
| 330 | 330 | // trim to first 100 characters because this is the limit on Matecat's DB |
| 331 | - $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
|
| 331 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
| 332 | 332 | |
| 333 | 333 | // `translate` attribute can be only yes or no |
| 334 | 334 | // current 'translate' attribute of the current trans-unit |
| 335 | - $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ]; |
|
| 335 | + $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate']; |
|
| 336 | 336 | |
| 337 | 337 | $this->setLastTransUnitSegments(); |
| 338 | 338 | |
@@ -346,15 +346,15 @@ discard block |
||
| 346 | 346 | * |
| 347 | 347 | * @return string |
| 348 | 348 | */ |
| 349 | - protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { |
|
| 349 | + protected function handleOpenXliffTag(string $name, array $attr, string $tag): string { |
|
| 350 | 350 | |
| 351 | 351 | // Add MateCat specific namespace. |
| 352 | 352 | // Add trgLang |
| 353 | - if ( $name === 'xliff' ) { |
|
| 354 | - if ( !array_key_exists( 'xmlns:' . $this->namespace, $attr ) ) { |
|
| 353 | + if ($name === 'xliff') { |
|
| 354 | + if (!array_key_exists('xmlns:' . $this->namespace, $attr)) { |
|
| 355 | 355 | $tag .= ' xmlns:' . $this->namespace . '="https://www.matecat.com" '; |
| 356 | 356 | } |
| 357 | - $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag ); |
|
| 357 | + $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag); |
|
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | return $tag; |
@@ -366,11 +366,11 @@ discard block |
||
| 366 | 366 | * |
| 367 | 367 | * @return void |
| 368 | 368 | */ |
| 369 | - protected function checkSetInTarget( string $name ) { |
|
| 369 | + protected function checkSetInTarget(string $name) { |
|
| 370 | 370 | |
| 371 | 371 | // check if we are entering into a <target> |
| 372 | - if ( 'target' == $name && !$this->inAltTrans ) { |
|
| 373 | - if ( $this->currentTransUnitIsTranslatable === 'no' ) { |
|
| 372 | + if ('target' == $name && !$this->inAltTrans) { |
|
| 373 | + if ($this->currentTransUnitIsTranslatable === 'no') { |
|
| 374 | 374 | $this->inTarget = false; |
| 375 | 375 | } else { |
| 376 | 376 | $this->inTarget = true; |
@@ -379,12 +379,12 @@ discard block |
||
| 379 | 379 | |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - protected function trySetAltTrans( string $name ) { |
|
| 382 | + protected function trySetAltTrans(string $name) { |
|
| 383 | 383 | $this->inAltTrans = $this->inAltTrans || $this->alternativeMatchesTag == $name; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | - protected function tryUnsetAltTrans( string $name ) { |
|
| 387 | - if ( $this->alternativeMatchesTag == $name ) { |
|
| 386 | + protected function tryUnsetAltTrans(string $name) { |
|
| 387 | + if ($this->alternativeMatchesTag == $name) { |
|
| 388 | 388 | $this->inAltTrans = false; |
| 389 | 389 | } |
| 390 | 390 | } |
@@ -394,15 +394,15 @@ discard block |
||
| 394 | 394 | * |
| 395 | 395 | * @return void |
| 396 | 396 | */ |
| 397 | - protected function setInBuffer( string $name ) { |
|
| 398 | - if ( in_array( $name, $this->nodesToBuffer ) ) { |
|
| 397 | + protected function setInBuffer(string $name) { |
|
| 398 | + if (in_array($name, $this->nodesToBuffer)) { |
|
| 399 | 399 | $this->bufferIsActive = true; |
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | // We need bufferIsActive for <target> nodes with currentTransUnitIsTranslatable = 'NO' |
| 403 | 403 | // because in the other case, the target can be chunked into pieces by xml_set_character_data_handler() |
| 404 | 404 | // and this can potentially lead to a wrong string rebuild by postProcAndFlush function if the internal placeholders are split |
| 405 | - if($name === 'target' and $this->currentTransUnitIsTranslatable === 'no'){ |
|
| 405 | + if ($name === 'target' and $this->currentTransUnitIsTranslatable === 'no') { |
|
| 406 | 406 | $this->bufferIsActive = true; |
| 407 | 407 | } |
| 408 | 408 | } |
@@ -410,24 +410,24 @@ discard block |
||
| 410 | 410 | /** |
| 411 | 411 | * @param array $seg |
| 412 | 412 | */ |
| 413 | - protected function updateSegmentCounts( array $seg = [] ) { |
|
| 413 | + protected function updateSegmentCounts(array $seg = []) { |
|
| 414 | 414 | |
| 415 | - $raw_word_count = $seg[ 'raw_word_count' ]; |
|
| 416 | - $eq_word_count = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 ); |
|
| 415 | + $raw_word_count = $seg['raw_word_count']; |
|
| 416 | + $eq_word_count = (floor($seg['eq_word_count'] * 100) / 100); |
|
| 417 | 417 | |
| 418 | - $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [ |
|
| 418 | + $this->counts['segments_count_array'][$seg['sid']] = [ |
|
| 419 | 419 | 'raw_word_count' => $raw_word_count, |
| 420 | 420 | 'eq_word_count' => $eq_word_count, |
| 421 | 421 | ]; |
| 422 | 422 | |
| 423 | - $this->counts[ 'raw_word_count' ] += $raw_word_count; |
|
| 424 | - $this->counts[ 'eq_word_count' ] += $eq_word_count; |
|
| 423 | + $this->counts['raw_word_count'] += $raw_word_count; |
|
| 424 | + $this->counts['eq_word_count'] += $eq_word_count; |
|
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | protected function resetCounts() { |
| 428 | - $this->counts[ 'segments_count_array' ] = []; |
|
| 429 | - $this->counts[ 'raw_word_count' ] = 0; |
|
| 430 | - $this->counts[ 'eq_word_count' ] = 0; |
|
| 428 | + $this->counts['segments_count_array'] = []; |
|
| 429 | + $this->counts['raw_word_count'] = 0; |
|
| 430 | + $this->counts['eq_word_count'] = 0; |
|
| 431 | 431 | } |
| 432 | 432 | |
| 433 | 433 | /** |
@@ -436,16 +436,16 @@ discard block |
||
| 436 | 436 | * |
| 437 | 437 | * @return void |
| 438 | 438 | */ |
| 439 | - protected function checkForSelfClosedTagAndFlush( $parser, string $tag ) { |
|
| 439 | + protected function checkForSelfClosedTagAndFlush($parser, string $tag) { |
|
| 440 | 440 | |
| 441 | - $lastChar = $this->getLastCharacter( $parser ); |
|
| 441 | + $lastChar = $this->getLastCharacter($parser); |
|
| 442 | 442 | |
| 443 | 443 | //trim last space |
| 444 | - $tag = rtrim( $tag ); |
|
| 444 | + $tag = rtrim($tag); |
|
| 445 | 445 | |
| 446 | 446 | //detect empty tag |
| 447 | 447 | $this->isEmpty = $lastChar == '/'; |
| 448 | - if ( $this->isEmpty ) { |
|
| 448 | + if ($this->isEmpty) { |
|
| 449 | 449 | $tag .= $lastChar; |
| 450 | 450 | } |
| 451 | 451 | |
@@ -453,11 +453,11 @@ discard block |
||
| 453 | 453 | $tag .= ">"; |
| 454 | 454 | |
| 455 | 455 | //set a Buffer for the segSource Source tag |
| 456 | - if ( $this->bufferIsActive ) { // we are opening a critical CDATA section |
|
| 456 | + if ($this->bufferIsActive) { // we are opening a critical CDATA section |
|
| 457 | 457 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
| 458 | 458 | $this->CDATABuffer .= $tag; |
| 459 | 459 | } else { |
| 460 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
| 460 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | } |
@@ -482,18 +482,18 @@ discard block |
||
| 482 | 482 | */ |
| 483 | 483 | $this->lastTransUnit = []; |
| 484 | 484 | |
| 485 | - if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
| 485 | + if (!isset($this->transUnits[$this->currentTransUnitId])) { |
|
| 486 | 486 | return; |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
| 489 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
| 490 | 490 | $last_value = null; |
| 491 | - $segmentsCount = count( $listOfSegmentsIds ); |
|
| 492 | - for ( $i = 0; $i < $segmentsCount; $i++ ) { |
|
| 493 | - $id = $listOfSegmentsIds[ $i ]; |
|
| 494 | - if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
|
| 495 | - $last_value = $listOfSegmentsIds[ $i ]; |
|
| 496 | - $this->lastTransUnit[] = $this->segments[ $id ]; |
|
| 491 | + $segmentsCount = count($listOfSegmentsIds); |
|
| 492 | + for ($i = 0; $i < $segmentsCount; $i++) { |
|
| 493 | + $id = $listOfSegmentsIds[$i]; |
|
| 494 | + if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) { |
|
| 495 | + $last_value = $listOfSegmentsIds[$i]; |
|
| 496 | + $this->lastTransUnit[] = $this->segments[$id]; |
|
| 497 | 497 | } |
| 498 | 498 | } |
| 499 | 499 | } |
@@ -504,10 +504,10 @@ discard block |
||
| 504 | 504 | protected function getCurrentSegment(): array { |
| 505 | 505 | if ( |
| 506 | 506 | $this->currentTransUnitIsTranslatable !== 'no' && |
| 507 | - isset( $this->transUnits[ $this->currentTransUnitId ] ) && |
|
| 508 | - isset( $this->segments[ $this->segmentInUnitPosition ] ) |
|
| 507 | + isset($this->transUnits[$this->currentTransUnitId]) && |
|
| 508 | + isset($this->segments[$this->segmentInUnitPosition]) |
|
| 509 | 509 | ) { |
| 510 | - return $this->segments[ $this->segmentInUnitPosition ]; |
|
| 510 | + return $this->segments[$this->segmentInUnitPosition]; |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | return []; |