@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @return array |
24 | 24 | */ |
25 | - public static function getInfoFromXliffContent( $xliffContent ) { |
|
25 | + public static function getInfoFromXliffContent($xliffContent) { |
|
26 | 26 | self::reset(); |
27 | - $tmp = self::getFirst1024CharsFromXliff( $xliffContent, null ); |
|
27 | + $tmp = self::getFirst1024CharsFromXliff($xliffContent, null); |
|
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( $fullPathToFile ) { |
|
37 | + public static function getInfo($fullPathToFile) { |
|
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( $tmp ) { |
|
50 | + private static function getInfoFromTmp($tmp) { |
|
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( $tmp ) { |
|
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($tmp) { |
|
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 | } |
@@ -99,29 +99,29 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @return array|false |
101 | 101 | */ |
102 | - private static function getFirst1024CharsFromXliff( $stringData = null, $fullPathToFile = null ) { |
|
103 | - if ( !empty( $stringData ) && empty( $fullPathToFile ) ) { |
|
102 | + private static function getFirst1024CharsFromXliff($stringData = null, $fullPathToFile = null) { |
|
103 | + if (!empty($stringData) && empty($fullPathToFile)) { |
|
104 | 104 | $pathInfo = []; |
105 | - $stringData = substr( $stringData, 0, 1024 ); |
|
106 | - } elseif ( empty( $stringData ) && !empty( $fullPathToFile ) ) { |
|
107 | - $pathInfo = Files::pathInfo( $fullPathToFile ); |
|
105 | + $stringData = substr($stringData, 0, 1024); |
|
106 | + } elseif (empty($stringData) && !empty($fullPathToFile)) { |
|
107 | + $pathInfo = Files::pathInfo($fullPathToFile); |
|
108 | 108 | |
109 | - if ( is_file( $fullPathToFile ) ) { |
|
110 | - $file_pointer = fopen( "$fullPathToFile", 'r' ); |
|
109 | + if (is_file($fullPathToFile)) { |
|
110 | + $file_pointer = fopen("$fullPathToFile", 'r'); |
|
111 | 111 | // Checking Requirements (By specs, I know that xliff version is in the first 1KB) |
112 | - $stringData = fread( $file_pointer, 1024 ); |
|
113 | - fclose( $file_pointer ); |
|
112 | + $stringData = fread($file_pointer, 1024); |
|
113 | + fclose($file_pointer); |
|
114 | 114 | } |
115 | - } elseif ( !empty( $stringData ) && !empty( $fullPathToFile ) ) { |
|
116 | - $pathInfo = Files::pathInfo( $fullPathToFile ); |
|
115 | + } elseif (!empty($stringData) && !empty($fullPathToFile)) { |
|
116 | + $pathInfo = Files::pathInfo($fullPathToFile); |
|
117 | 117 | } |
118 | 118 | |
119 | - if ( !empty( $pathInfo ) && !Files::isXliff( $fullPathToFile ) ) { |
|
119 | + if (!empty($pathInfo) && !Files::isXliff($fullPathToFile)) { |
|
120 | 120 | return false; |
121 | 121 | } |
122 | 122 | |
123 | - if ( !empty( $stringData ) ) { |
|
124 | - return [ $stringData ]; |
|
123 | + if (!empty($stringData)) { |
|
124 | + return [$stringData]; |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | return false; |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | * @throws NotSupportedVersionException |
134 | 134 | * @throws NotValidFileException |
135 | 135 | */ |
136 | - protected static function checkVersion( $tmp ) { |
|
137 | - if ( isset( $tmp[ 0 ] ) ) { |
|
138 | - self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] ); |
|
136 | + protected static function checkVersion($tmp) { |
|
137 | + if (isset($tmp[0])) { |
|
138 | + self::$fileType['version'] = XliffVersionDetector::detect($tmp[0]); |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
@@ -146,20 +146,20 @@ discard block |
||
146 | 146 | * @throws NotSupportedVersionException |
147 | 147 | * @throws NotValidFileException |
148 | 148 | */ |
149 | - public static function getInfoByStringData( $stringData ) { |
|
149 | + public static function getInfoByStringData($stringData) { |
|
150 | 150 | self::reset(); |
151 | 151 | |
152 | - $tmp = self::getFirst1024CharsFromXliff( $stringData ); |
|
153 | - self::$fileType[ 'info' ] = []; |
|
154 | - self::checkVersion( $tmp ); |
|
152 | + $tmp = self::getFirst1024CharsFromXliff($stringData); |
|
153 | + self::$fileType['info'] = []; |
|
154 | + self::checkVersion($tmp); |
|
155 | 155 | |
156 | 156 | // run CheckXliffProprietaryPipeline |
157 | - $pipeline = self::runPipeline( $tmp ); |
|
157 | + $pipeline = self::runPipeline($tmp); |
|
158 | 158 | |
159 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
160 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
161 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
162 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
159 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
160 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
161 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
162 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
163 | 163 | |
164 | 164 | return self::$fileType; |
165 | 165 | } |
@@ -171,21 +171,21 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @return bool|int |
173 | 173 | */ |
174 | - public static function fileMustBeConverted( $fullPath, $enforceOnXliff = false, $filterAddress = null ) { |
|
174 | + public static function fileMustBeConverted($fullPath, $enforceOnXliff = false, $filterAddress = null) { |
|
175 | 175 | $convert = true; |
176 | 176 | |
177 | - $fileType = self::getInfo( $fullPath ); |
|
178 | - $memoryFileType = Files::getMemoryFileType( $fullPath ); |
|
177 | + $fileType = self::getInfo($fullPath); |
|
178 | + $memoryFileType = Files::getMemoryFileType($fullPath); |
|
179 | 179 | |
180 | - if ( Files::isXliff( $fullPath ) || $memoryFileType ) { |
|
181 | - if ( !empty( $filterAddress ) ) { |
|
180 | + if (Files::isXliff($fullPath) || $memoryFileType) { |
|
181 | + if (!empty($filterAddress)) { |
|
182 | 182 | |
183 | 183 | //conversion enforce |
184 | - if ( !$enforceOnXliff ) { |
|
184 | + if (!$enforceOnXliff) { |
|
185 | 185 | |
186 | 186 | //if file is not proprietary AND Enforce is disabled |
187 | 187 | //we take it as is |
188 | - if ( !$fileType[ 'proprietary' ] || $memoryFileType ) { |
|
188 | + if (!$fileType['proprietary'] || $memoryFileType) { |
|
189 | 189 | $convert = false; |
190 | 190 | //ok don't convert a standard sdlxliff |
191 | 191 | } |
@@ -193,16 +193,16 @@ discard block |
||
193 | 193 | //if conversion enforce is active |
194 | 194 | //we force all xliff files but not files produced by SDL Studio because we can handle them |
195 | 195 | if ( |
196 | - $fileType[ 'proprietary_short_name' ] == 'matecat_converter' |
|
197 | - || $fileType[ 'proprietary_short_name' ] == 'trados' |
|
198 | - || $fileType[ 'proprietary_short_name' ] == 'xliff_v2' |
|
196 | + $fileType['proprietary_short_name'] == 'matecat_converter' |
|
197 | + || $fileType['proprietary_short_name'] == 'trados' |
|
198 | + || $fileType['proprietary_short_name'] == 'xliff_v2' |
|
199 | 199 | || $memoryFileType |
200 | 200 | ) { |
201 | 201 | $convert = false; |
202 | 202 | //ok don't convert a standard sdlxliff |
203 | 203 | } |
204 | 204 | } |
205 | - } elseif ( $fileType[ 'proprietary' ] ) { |
|
205 | + } elseif ($fileType['proprietary']) { |
|
206 | 206 | |
207 | 207 | /** |
208 | 208 | * Application misconfiguration. |
@@ -8,15 +8,15 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - if ( stripos( $tmp[ 0 ], 'globalsight' ) !== false ) { |
|
16 | - $fileType[ 'proprietary' ] = true; |
|
17 | - $fileType[ 'proprietary_name' ] = 'GlobalSight Download File'; |
|
18 | - $fileType[ 'proprietary_short_name' ] = 'globalsight'; |
|
19 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
14 | + if (isset($tmp[0])) { |
|
15 | + if (stripos($tmp[0], 'globalsight') !== false) { |
|
16 | + $fileType['proprietary'] = true; |
|
17 | + $fileType['proprietary_name'] = 'GlobalSight Download File'; |
|
18 | + $fileType['proprietary_short_name'] = 'globalsight'; |
|
19 | + $fileType['converter_version'] = 'legacy'; |
|
20 | 20 | |
21 | 21 | return $fileType; |
22 | 22 | } |
@@ -8,22 +8,22 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|void|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $versionMatches ); |
|
16 | - preg_match( '|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr( $tmp[ 0 ], 0, 1000 ), $xmlnsMatches ); |
|
14 | + if (isset($tmp[0])) { |
|
15 | + preg_match('|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr($tmp[0], 0, 1000), $versionMatches); |
|
16 | + preg_match('|<xliff.*?\sxmlns\s?=\s?["\']urn:oasis:names:tc:xliff:document:(.*?)["\']|si', substr($tmp[0], 0, 1000), $xmlnsMatches); |
|
17 | 17 | |
18 | - if ( !empty( $versionMatches ) && !empty( $xmlnsMatches ) ) { |
|
19 | - $version = $versionMatches[ 1 ]; |
|
20 | - $xmlns = $xmlnsMatches[ 1 ]; |
|
18 | + if (!empty($versionMatches) && !empty($xmlnsMatches)) { |
|
19 | + $version = $versionMatches[1]; |
|
20 | + $xmlns = $xmlnsMatches[1]; |
|
21 | 21 | |
22 | - if ( $version === $xmlns && $version >= 2 ) { |
|
23 | - $fileType[ 'proprietary' ] = false; |
|
24 | - $fileType[ 'proprietary_name' ] = 'Xliff v' . $version . ' File'; |
|
25 | - $fileType[ 'proprietary_short_name' ] = 'xliff_v2'; |
|
26 | - $fileType[ 'converter_version' ] = '2.0'; |
|
22 | + if ($version === $xmlns && $version >= 2) { |
|
23 | + $fileType['proprietary'] = false; |
|
24 | + $fileType['proprietary_name'] = 'Xliff v' . $version . ' File'; |
|
25 | + $fileType['proprietary_short_name'] = 'xliff_v2'; |
|
26 | + $fileType['converter_version'] = '2.0'; |
|
27 | 27 | |
28 | 28 | return $fileType; |
29 | 29 | } |
@@ -3,16 +3,16 @@ |
||
3 | 3 | namespace Matecat\XliffParser\XliffUtils\CheckPipeline; |
4 | 4 | |
5 | 5 | class CheckSDL implements CheckInterface { |
6 | - public function check( $tmp ) { |
|
6 | + public function check($tmp) { |
|
7 | 7 | $fileType = []; |
8 | 8 | |
9 | - if ( isset( $tmp[ 0 ] ) ) { |
|
10 | - if ( stripos( $tmp[ 0 ], 'sdl:version' ) !== false ) { |
|
9 | + if (isset($tmp[0])) { |
|
10 | + if (stripos($tmp[0], 'sdl:version') !== false) { |
|
11 | 11 | //little trick, we consider not proprietary Sdlxliff files because we can handle them |
12 | - $fileType[ 'proprietary' ] = false; |
|
13 | - $fileType[ 'proprietary_name' ] = 'SDL Studio '; |
|
14 | - $fileType[ 'proprietary_short_name' ] = 'trados'; |
|
15 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
12 | + $fileType['proprietary'] = false; |
|
13 | + $fileType['proprietary_name'] = 'SDL Studio '; |
|
14 | + $fileType['proprietary_short_name'] = 'trados'; |
|
15 | + $fileType['converter_version'] = 'legacy'; |
|
16 | 16 | |
17 | 17 | return $fileType; |
18 | 18 | } |
@@ -8,20 +8,20 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ) { |
|
11 | + public function check($tmp) { |
|
12 | 12 | $fileType = []; |
13 | 13 | |
14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
15 | - preg_match( '#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[ 0 ], $matches ); |
|
16 | - if ( !empty( $matches ) ) { |
|
17 | - $fileType[ 'proprietary' ] = false; |
|
18 | - $fileType[ 'proprietary_name' ] = 'MateCAT Converter'; |
|
19 | - $fileType[ 'proprietary_short_name' ] = 'matecat_converter'; |
|
20 | - if ( isset( $matches[ 2 ] ) ) { |
|
21 | - $fileType[ 'converter_version' ] = $matches[ 2 ]; |
|
14 | + if (isset($tmp[0])) { |
|
15 | + preg_match('#tool-id\s*=\s*"matecat-converter(\s+([^"]+))?"#i', $tmp[0], $matches); |
|
16 | + if (!empty($matches)) { |
|
17 | + $fileType['proprietary'] = false; |
|
18 | + $fileType['proprietary_name'] = 'MateCAT Converter'; |
|
19 | + $fileType['proprietary_short_name'] = 'matecat_converter'; |
|
20 | + if (isset($matches[2])) { |
|
21 | + $fileType['converter_version'] = $matches[2]; |
|
22 | 22 | } else { |
23 | 23 | // First converter release didn't specify version |
24 | - $fileType[ 'converter_version' ] = '1.0'; |
|
24 | + $fileType['converter_version'] = '1.0'; |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | return $fileType; |
@@ -8,5 +8,5 @@ |
||
8 | 8 | * |
9 | 9 | * @return array|null |
10 | 10 | */ |
11 | - public function check( $tmp ); |
|
11 | + public function check($tmp); |
|
12 | 12 | } |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | /** |
10 | 10 | * @var array |
11 | 11 | */ |
12 | - private static $versions_1 = [ '1.0', '1.1', '1.2' ]; |
|
12 | + private static $versions_1 = ['1.0', '1.1', '1.2']; |
|
13 | 13 | |
14 | 14 | /** |
15 | 15 | * @var array |
16 | 16 | */ |
17 | - private static $versions_2 = [ '2.0', '2.1' ]; |
|
17 | + private static $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( $xliffContent ) { |
|
27 | - preg_match( '|<xliff.*?\sversion\s?=\s?["\'](.*?)["\']|si', substr( $xliffContent, 0, 1000 ), $versionMatches ); |
|
26 | + public static function detect($xliffContent) { |
|
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( $version ) { |
|
45 | - if ( in_array( $version, self::$versions_1 ) ) { |
|
44 | + private static function resolveVersion($version) { |
|
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 | } |
@@ -18,13 +18,13 @@ |
||
18 | 18 | * |
19 | 19 | * @return SdlXliffSAXTranslationReplacer|XliffSAXTranslationReplacer |
20 | 20 | */ |
21 | - public static function getInstance( $originalXliffPath, &$data, &$transUnits, $targetLang, $outputFilePath, $setSourceInTarget, LoggerInterface $logger = null, XliffReplacerCallbackInterface $callback = null ) { |
|
22 | - $info = XliffProprietaryDetect::getInfo( $originalXliffPath ); |
|
21 | + public static function getInstance($originalXliffPath, &$data, &$transUnits, $targetLang, $outputFilePath, $setSourceInTarget, LoggerInterface $logger = null, XliffReplacerCallbackInterface $callback = null) { |
|
22 | + $info = XliffProprietaryDetect::getInfo($originalXliffPath); |
|
23 | 23 | |
24 | - if ( $info[ 'proprietary_short_name' ] !== 'trados' ) { |
|
25 | - return new XliffSAXTranslationReplacer( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback ); |
|
24 | + if ($info['proprietary_short_name'] !== 'trados') { |
|
25 | + return new XliffSAXTranslationReplacer($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback); |
|
26 | 26 | } |
27 | 27 | |
28 | - return new SdlXliffSAXTranslationReplacer( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback ); |
|
28 | + return new SdlXliffSAXTranslationReplacer($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback); |
|
29 | 29 | } |
30 | 30 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @param LoggerInterface $logger |
26 | 26 | */ |
27 | - public function __construct( LoggerInterface $logger = null ) { |
|
27 | + public function __construct(LoggerInterface $logger = null) { |
|
28 | 28 | $this->logger = $logger; |
29 | 29 | } |
30 | 30 | |
@@ -39,11 +39,11 @@ discard block |
||
39 | 39 | * @param bool $setSourceInTarget |
40 | 40 | * @param XliffReplacerCallbackInterface|null $callback |
41 | 41 | */ |
42 | - public function replaceTranslation( $originalXliffPath, &$data, &$transUnits, $targetLang, $outputFile, $setSourceInTarget = false, XliffReplacerCallbackInterface $callback = null ) { |
|
42 | + public function replaceTranslation($originalXliffPath, &$data, &$transUnits, $targetLang, $outputFile, $setSourceInTarget = false, XliffReplacerCallbackInterface $callback = null) { |
|
43 | 43 | try { |
44 | - $parser = XliffReplacerFactory::getInstance( $originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback ); |
|
44 | + $parser = XliffReplacerFactory::getInstance($originalXliffPath, $data, $transUnits, $targetLang, $outputFile, $setSourceInTarget, $this->logger, $callback); |
|
45 | 45 | $parser->replaceTranslation(); |
46 | - } catch ( \Exception $exception ) { |
|
46 | + } catch (\Exception $exception) { |
|
47 | 47 | // do nothing |
48 | 48 | } |
49 | 49 | } |
@@ -61,29 +61,29 @@ discard block |
||
61 | 61 | * @throws Exception\NotValidFileException |
62 | 62 | * @throws Exception\XmlParsingException |
63 | 63 | */ |
64 | - public function xliffToArray( $xliffContent, $collapseEmptyTags = false ) { |
|
64 | + public function xliffToArray($xliffContent, $collapseEmptyTags = false) { |
|
65 | 65 | $xliff = []; |
66 | - $xliffContent = self::forceUft8Encoding( $xliffContent, $xliff ); |
|
67 | - $xliffVersion = XliffVersionDetector::detect( $xliffContent ); |
|
68 | - $info = XliffProprietaryDetect::getInfoFromXliffContent( $xliffContent ); |
|
66 | + $xliffContent = self::forceUft8Encoding($xliffContent, $xliff); |
|
67 | + $xliffVersion = XliffVersionDetector::detect($xliffContent); |
|
68 | + $info = XliffProprietaryDetect::getInfoFromXliffContent($xliffContent); |
|
69 | 69 | |
70 | - if ( $xliffVersion === 1 ) { |
|
71 | - $xliffContent = self::removeInternalFileTagFromContent( $xliffContent, $xliff ); |
|
70 | + if ($xliffVersion === 1) { |
|
71 | + $xliffContent = self::removeInternalFileTagFromContent($xliffContent, $xliff); |
|
72 | 72 | } |
73 | 73 | |
74 | - if ( $xliffVersion === 2 ) { |
|
75 | - $xliffContent = self::escapeDataInOriginalMap( $xliffContent ); |
|
74 | + if ($xliffVersion === 2) { |
|
75 | + $xliffContent = self::escapeDataInOriginalMap($xliffContent); |
|
76 | 76 | } |
77 | 77 | |
78 | - if ( $collapseEmptyTags === false ) { |
|
79 | - $xliffContent = self::insertPlaceholderInEmptyTags( $xliffContent ); |
|
78 | + if ($collapseEmptyTags === false) { |
|
79 | + $xliffContent = self::insertPlaceholderInEmptyTags($xliffContent); |
|
80 | 80 | } |
81 | 81 | |
82 | - $xliffProprietary = ( isset( $info[ 'proprietary_short_name' ] ) && null !== $info[ 'proprietary_short_name' ] ) ? $info[ 'proprietary_short_name' ] : null; |
|
83 | - $parser = XliffParserFactory::getInstance( $xliffVersion, $xliffProprietary, $this->logger ); |
|
84 | - $dom = XmlParser::parse( $xliffContent ); |
|
82 | + $xliffProprietary = (isset($info['proprietary_short_name']) && null !== $info['proprietary_short_name']) ? $info['proprietary_short_name'] : null; |
|
83 | + $parser = XliffParserFactory::getInstance($xliffVersion, $xliffProprietary, $this->logger); |
|
84 | + $dom = XmlParser::parse($xliffContent); |
|
85 | 85 | |
86 | - return $parser->parse( $dom, $xliff ); |
|
86 | + return $parser->parse($dom, $xliff); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -95,12 +95,12 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @return string |
97 | 97 | */ |
98 | - private static function forceUft8Encoding( $xliffContent, &$xliff ) { |
|
99 | - $enc = mb_detect_encoding( $xliffContent ); |
|
98 | + private static function forceUft8Encoding($xliffContent, &$xliff) { |
|
99 | + $enc = mb_detect_encoding($xliffContent); |
|
100 | 100 | |
101 | - if ( $enc !== 'UTF-8' ) { |
|
102 | - $xliff[ 'parser-warnings' ][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
103 | - $s = iconv( $enc, 'UTF-8', $xliffContent ); |
|
101 | + if ($enc !== 'UTF-8') { |
|
102 | + $xliff['parser-warnings'][] = "Input identified as $enc ans converted UTF-8. May not be a problem if the content is English only"; |
|
103 | + $s = iconv($enc, 'UTF-8', $xliffContent); |
|
104 | 104 | $xliffContent = $s !== false ? $s : ""; |
105 | 105 | } |
106 | 106 | |
@@ -117,24 +117,24 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return mixed|string |
119 | 119 | */ |
120 | - private static function removeInternalFileTagFromContent( $xliffContent, &$xliff ) { |
|
120 | + private static function removeInternalFileTagFromContent($xliffContent, &$xliff) { |
|
121 | 121 | $index = 1; |
122 | - $a = Strings::preg_split( '|<internal-file[\s>]|si', $xliffContent ); |
|
122 | + $a = Strings::preg_split('|<internal-file[\s>]|si', $xliffContent); |
|
123 | 123 | |
124 | 124 | // no match, return original string |
125 | - if ( count( $a ) === 1 ) { |
|
126 | - return $a[ 0 ]; |
|
125 | + if (count($a) === 1) { |
|
126 | + return $a[0]; |
|
127 | 127 | } |
128 | 128 | |
129 | - $b = Strings::preg_split( '|</internal-file>|si', $a[ 1 ] ); |
|
130 | - $strippedContent = $a[ 0 ] . $b[ 1 ]; |
|
131 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $b[ 0 ] ); |
|
129 | + $b = Strings::preg_split('|</internal-file>|si', $a[1]); |
|
130 | + $strippedContent = $a[0] . $b[1]; |
|
131 | + $xliff['files'][$index]['reference'][] = self::extractBase64($b[0]); |
|
132 | 132 | $index++; |
133 | 133 | |
134 | - if ( isset( $a[ 2 ] ) ) { |
|
135 | - $c = Strings::preg_split( '|</internal-file[\s>]|si', $a[ 2 ] ); |
|
136 | - $strippedContent .= $c[ 1 ]; |
|
137 | - $xliff[ 'files' ][ $index ][ 'reference' ][] = self::extractBase64( $c[ 0 ] ); |
|
134 | + if (isset($a[2])) { |
|
135 | + $c = Strings::preg_split('|</internal-file[\s>]|si', $a[2]); |
|
136 | + $strippedContent .= $c[1]; |
|
137 | + $xliff['files'][$index]['reference'][] = self::extractBase64($c[0]); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | return $strippedContent; |
@@ -145,10 +145,10 @@ discard block |
||
145 | 145 | * |
146 | 146 | * @return array |
147 | 147 | */ |
148 | - private static function extractBase64( $base64 ) { |
|
148 | + private static function extractBase64($base64) { |
|
149 | 149 | return [ |
150 | 150 | 'form-type' => 'base64', |
151 | - 'base64' => trim( str_replace( 'form="base64">', '', $base64 ) ), |
|
151 | + 'base64' => trim(str_replace('form="base64">', '', $base64)), |
|
152 | 152 | ]; |
153 | 153 | } |
154 | 154 | |
@@ -168,9 +168,9 @@ discard block |
||
168 | 168 | * |
169 | 169 | * @return string |
170 | 170 | */ |
171 | - private static function escapeDataInOriginalMap( $xliffContent ) { |
|
172 | - $xliffContent = preg_replace_callback( '/<data(.*?)>(.*?)<\/data>/iU', [ XliffParser::class, 'replaceSpace' ], $xliffContent ); |
|
173 | - $xliffContent = preg_replace_callback( '/<data(.*?)>(.*?)<\/data>/iU', [ XliffParser::class, 'replaceXliffTags' ], $xliffContent ); |
|
171 | + private static function escapeDataInOriginalMap($xliffContent) { |
|
172 | + $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceSpace'], $xliffContent); |
|
173 | + $xliffContent = preg_replace_callback('/<data(.*?)>(.*?)<\/data>/iU', [XliffParser::class, 'replaceXliffTags'], $xliffContent); |
|
174 | 174 | |
175 | 175 | return $xliffContent; |
176 | 176 | } |
@@ -189,15 +189,15 @@ discard block |
||
189 | 189 | * |
190 | 190 | * @return string |
191 | 191 | */ |
192 | - private static function insertPlaceholderInEmptyTags( $xliffContent ) { |
|
193 | - preg_match_all( '/<([a-zA-Z0-9._-]+)[^>]*><\/\1>/sm', $xliffContent, $emptyTagMatches ); |
|
192 | + private static function insertPlaceholderInEmptyTags($xliffContent) { |
|
193 | + preg_match_all('/<([a-zA-Z0-9._-]+)[^>]*><\/\1>/sm', $xliffContent, $emptyTagMatches); |
|
194 | 194 | |
195 | - if ( !empty( $emptyTagMatches[ 0 ] ) ) { |
|
196 | - foreach ( $emptyTagMatches[ 0 ] as $index => $emptyTagMatch ) { |
|
197 | - $matchedTag = $emptyTagMatches[ 1 ][ $index ]; |
|
195 | + if (!empty($emptyTagMatches[0])) { |
|
196 | + foreach ($emptyTagMatches[0] as $index => $emptyTagMatch) { |
|
197 | + $matchedTag = $emptyTagMatches[1][$index]; |
|
198 | 198 | $subst = Placeholder::EMPTY_TAG_PLACEHOLDER . '</' . $matchedTag . '>'; |
199 | - $replacedTag = str_replace( '</' . $matchedTag . '>', $subst, $emptyTagMatch ); |
|
200 | - $xliffContent = str_replace( $emptyTagMatch, $replacedTag, $xliffContent ); |
|
199 | + $replacedTag = str_replace('</' . $matchedTag . '>', $subst, $emptyTagMatch); |
|
200 | + $xliffContent = str_replace($emptyTagMatch, $replacedTag, $xliffContent); |
|
201 | 201 | } |
202 | 202 | } |
203 | 203 | |
@@ -211,12 +211,12 @@ discard block |
||
211 | 211 | * |
212 | 212 | * @return string |
213 | 213 | */ |
214 | - private static function replaceSpace( $matches ) { |
|
215 | - $content = str_replace( ' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[ 2 ] ); |
|
216 | - $content = str_replace( '\n', Placeholder::NEW_LINE_PLACEHOLDER, $content ); |
|
217 | - $content = str_replace( '\t', Placeholder::TAB_PLACEHOLDER, $content ); |
|
214 | + private static function replaceSpace($matches) { |
|
215 | + $content = str_replace(' ', Placeholder::WHITE_SPACE_PLACEHOLDER, $matches[2]); |
|
216 | + $content = str_replace('\n', Placeholder::NEW_LINE_PLACEHOLDER, $content); |
|
217 | + $content = str_replace('\t', Placeholder::TAB_PLACEHOLDER, $content); |
|
218 | 218 | |
219 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
219 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
@@ -224,15 +224,15 @@ discard block |
||
224 | 224 | * |
225 | 225 | * @return string |
226 | 226 | */ |
227 | - private static function replaceXliffTags( $matches ) { |
|
227 | + private static function replaceXliffTags($matches) { |
|
228 | 228 | $xliffTags = XliffTags::$tags; |
229 | - $content = $matches[ 2 ]; |
|
229 | + $content = $matches[2]; |
|
230 | 230 | |
231 | - foreach ( $xliffTags as $xliffTag ) { |
|
232 | - $content = preg_replace( '|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
233 | - $content = preg_replace( '|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content ); |
|
231 | + foreach ($xliffTags as $xliffTag) { |
|
232 | + $content = preg_replace('|<(' . $xliffTag . '.*?)>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
233 | + $content = preg_replace('|<(/' . $xliffTag . ')>|si', Placeholder::LT_PLACEHOLDER . "$1" . Placeholder::GT_PLACEHOLDER, $content); |
|
234 | 234 | } |
235 | 235 | |
236 | - return '<data' . $matches[ 1 ] . '>' . $content . '</data>'; |
|
236 | + return '<data' . $matches[1] . '>' . $content . '</data>'; |
|
237 | 237 | } |
238 | 238 | } |