@@ -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 | } |
@@ -93,21 +93,21 @@ discard block |
||
93 | 93 | ]; |
94 | 94 | } |
95 | 95 | |
96 | - private static function getFirst1024CharsFromString( ?string $stringData ): string { |
|
97 | - if ( !empty( $stringData ) ) { |
|
98 | - return substr( $stringData, 0, 1024 ); |
|
96 | + private static function getFirst1024CharsFromString(?string $stringData): string { |
|
97 | + if (!empty($stringData)) { |
|
98 | + return substr($stringData, 0, 1024); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | return ''; |
102 | 102 | } |
103 | 103 | |
104 | - private static function getFirst1024CharsFromFile( string $fullPathToFile ): string { |
|
104 | + private static function getFirst1024CharsFromFile(string $fullPathToFile): string { |
|
105 | 105 | $stringData = ''; |
106 | - if ( !empty( $fullPathToFile ) && is_file( $fullPathToFile ) ) { |
|
107 | - $file_pointer = fopen( "$fullPathToFile", 'r' ); |
|
106 | + if (!empty($fullPathToFile) && is_file($fullPathToFile)) { |
|
107 | + $file_pointer = fopen("$fullPathToFile", 'r'); |
|
108 | 108 | // Checking Requirements (By specs, I know that xliff version is in the first 1KB) |
109 | - $stringData = fread( $file_pointer, 1024 ); |
|
110 | - fclose( $file_pointer ); |
|
109 | + $stringData = fread($file_pointer, 1024); |
|
110 | + fclose($file_pointer); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | return $stringData; |
@@ -120,13 +120,13 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @return string[] |
122 | 122 | */ |
123 | - private static function getFirst1024CharsFromXliff( ?string $stringData = null, string $fullPathToFile = null ): ?array { |
|
124 | - $stringData = static::getFirst1024CharsFromString( $stringData ); |
|
125 | - if ( empty( $stringData ) ) { |
|
126 | - $stringData = static::getFirst1024CharsFromFile( $fullPathToFile ); |
|
123 | + private static function getFirst1024CharsFromXliff(?string $stringData = null, string $fullPathToFile = null): ?array { |
|
124 | + $stringData = static::getFirst1024CharsFromString($stringData); |
|
125 | + if (empty($stringData)) { |
|
126 | + $stringData = static::getFirst1024CharsFromFile($fullPathToFile); |
|
127 | 127 | } |
128 | 128 | |
129 | - return !empty( $stringData ) ? [ $stringData ] : []; |
|
129 | + return !empty($stringData) ? [$stringData] : []; |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | /** |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * @throws NotSupportedVersionException |
136 | 136 | * @throws NotValidFileException |
137 | 137 | */ |
138 | - protected static function checkVersion( array $tmp ) { |
|
139 | - if ( isset( $tmp[ 0 ] ) ) { |
|
140 | - self::$fileType[ 'version' ] = XliffVersionDetector::detect( $tmp[ 0 ] ); |
|
138 | + protected static function checkVersion(array $tmp) { |
|
139 | + if (isset($tmp[0])) { |
|
140 | + self::$fileType['version'] = XliffVersionDetector::detect($tmp[0]); |
|
141 | 141 | } |
142 | 142 | } |
143 | 143 | |
@@ -148,20 +148,20 @@ discard block |
||
148 | 148 | * @throws NotSupportedVersionException |
149 | 149 | * @throws NotValidFileException |
150 | 150 | */ |
151 | - public static function getInfoByStringData( string $stringData ): array { |
|
151 | + public static function getInfoByStringData(string $stringData): array { |
|
152 | 152 | self::reset(); |
153 | 153 | |
154 | - $tmp = self::getFirst1024CharsFromXliff( $stringData ); |
|
155 | - self::$fileType[ 'info' ] = []; |
|
156 | - self::checkVersion( $tmp ); |
|
154 | + $tmp = self::getFirst1024CharsFromXliff($stringData); |
|
155 | + self::$fileType['info'] = []; |
|
156 | + self::checkVersion($tmp); |
|
157 | 157 | |
158 | 158 | // run CheckXliffProprietaryPipeline |
159 | - $pipeline = self::runPipeline( $tmp ); |
|
159 | + $pipeline = self::runPipeline($tmp); |
|
160 | 160 | |
161 | - self::$fileType[ 'proprietary' ] = $pipeline[ 'proprietary' ]; |
|
162 | - self::$fileType[ 'proprietary_name' ] = $pipeline[ 'proprietary_name' ]; |
|
163 | - self::$fileType[ 'proprietary_short_name' ] = $pipeline[ 'proprietary_short_name' ]; |
|
164 | - self::$fileType[ 'converter_version' ] = $pipeline[ 'converter_version' ]; |
|
161 | + self::$fileType['proprietary'] = $pipeline['proprietary']; |
|
162 | + self::$fileType['proprietary_name'] = $pipeline['proprietary_name']; |
|
163 | + self::$fileType['proprietary_short_name'] = $pipeline['proprietary_short_name']; |
|
164 | + self::$fileType['converter_version'] = $pipeline['converter_version']; |
|
165 | 165 | |
166 | 166 | return self::$fileType; |
167 | 167 | } |
@@ -173,21 +173,21 @@ discard block |
||
173 | 173 | * |
174 | 174 | * @return bool|int |
175 | 175 | */ |
176 | - public static function fileMustBeConverted( string $fullPath, ?bool $enforceOnXliff = false, ?string $filterAddress = null ) { |
|
176 | + public static function fileMustBeConverted(string $fullPath, ?bool $enforceOnXliff = false, ?string $filterAddress = null) { |
|
177 | 177 | $convert = true; |
178 | 178 | |
179 | - $fileType = self::getInfo( $fullPath ); |
|
180 | - $memoryFileType = Files::getMemoryFileType( $fullPath ); |
|
179 | + $fileType = self::getInfo($fullPath); |
|
180 | + $memoryFileType = Files::getMemoryFileType($fullPath); |
|
181 | 181 | |
182 | - if ( Files::isXliff( $fullPath ) || $memoryFileType ) { |
|
183 | - if ( !empty( $filterAddress ) ) { |
|
182 | + if (Files::isXliff($fullPath) || $memoryFileType) { |
|
183 | + if (!empty($filterAddress)) { |
|
184 | 184 | |
185 | 185 | //conversion enforce |
186 | - if ( !$enforceOnXliff ) { |
|
186 | + if (!$enforceOnXliff) { |
|
187 | 187 | |
188 | 188 | //if file is not proprietary AND Enforce is disabled |
189 | 189 | //we take it as is |
190 | - if ( !$fileType[ 'proprietary' ] || $memoryFileType ) { |
|
190 | + if (!$fileType['proprietary'] || $memoryFileType) { |
|
191 | 191 | $convert = false; |
192 | 192 | //ok don't convert a standard sdlxliff |
193 | 193 | } |
@@ -195,16 +195,16 @@ discard block |
||
195 | 195 | //if conversion enforce is active |
196 | 196 | //we force all xliff files but not files produced by SDL Studio because we can handle them |
197 | 197 | if ( |
198 | - $fileType[ 'proprietary_short_name' ] == 'matecat_converter' |
|
199 | - || $fileType[ 'proprietary_short_name' ] == 'trados' |
|
200 | - || $fileType[ 'proprietary_short_name' ] == 'xliff_v2' |
|
198 | + $fileType['proprietary_short_name'] == 'matecat_converter' |
|
199 | + || $fileType['proprietary_short_name'] == 'trados' |
|
200 | + || $fileType['proprietary_short_name'] == 'xliff_v2' |
|
201 | 201 | || $memoryFileType |
202 | 202 | ) { |
203 | 203 | $convert = false; |
204 | 204 | //ok don't convert a standard sdlxliff |
205 | 205 | } |
206 | 206 | } |
207 | - } elseif ( $fileType[ 'proprietary' ] ) { |
|
207 | + } elseif ($fileType['proprietary']) { |
|
208 | 208 | |
209 | 209 | /** |
210 | 210 | * Application misconfiguration. |
@@ -7,26 +7,26 @@ 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 $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
16 | - protected bool $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
17 | - protected string $CDATABuffer = ""; // buffer for special tag |
|
18 | - protected bool $bufferIsActive = false; // buffer for special tag |
|
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 $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
16 | + protected bool $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
17 | + protected string $CDATABuffer = ""; // buffer for special tag |
|
18 | + protected bool $bufferIsActive = false; // buffer for special tag |
|
19 | 19 | |
20 | - protected int $offset = 0; // offset for SAX pointer |
|
20 | + protected int $offset = 0; // offset for SAX pointer |
|
21 | 21 | |
22 | - protected string $currentBuffer; // the current piece of text it's been parsed |
|
23 | - protected int $len; // length of the currentBuffer |
|
24 | - protected array $segments; // array of translations |
|
22 | + protected string $currentBuffer; // the current piece of text it's been parsed |
|
23 | + protected int $len; // length of the currentBuffer |
|
24 | + protected array $segments; // array of translations |
|
25 | 25 | protected array $lastTransUnit = []; |
26 | 26 | protected int $segmentInUnitPosition = 0; |
27 | - protected ?string $currentTransUnitId = null; // id of current <trans-unit> |
|
27 | + protected ?string $currentTransUnitId = null; // id of current <trans-unit> |
|
28 | 28 | protected ?string $currentTransUnitIsTranslatable = null; // 'translate' attribute of current <trans-unit> |
29 | - protected bool $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
29 | + protected bool $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
30 | 30 | protected string $targetLang; |
31 | 31 | protected bool $sourceInTarget = false; |
32 | 32 | |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | XliffReplacerCallbackInterface $callback = null |
77 | 77 | ) { |
78 | 78 | self::$INTERNAL_TAG_PLACEHOLDER = $this->getInternalTagPlaceholder(); |
79 | - $this->createOutputFileIfDoesNotExist( $outputFilePath ); |
|
80 | - $this->setFileDescriptors( $originalXliffPath, $outputFilePath ); |
|
79 | + $this->createOutputFileIfDoesNotExist($outputFilePath); |
|
80 | + $this->setFileDescriptors($originalXliffPath, $outputFilePath); |
|
81 | 81 | $this->xliffVersion = $xliffVersion; |
82 | - $this->tuTagName = ( $this->xliffVersion === 2 ) ? 'unit' : 'trans-unit'; |
|
82 | + $this->tuTagName = ($this->xliffVersion === 2) ? 'unit' : 'trans-unit'; |
|
83 | 83 | $this->segments = $segments; |
84 | 84 | $this->targetLang = $trgLang; |
85 | 85 | $this->sourceInTarget = $setSourceInTarget; |
@@ -89,17 +89,17 @@ discard block |
||
89 | 89 | } |
90 | 90 | |
91 | 91 | public function replaceTranslation() { |
92 | - fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' ); |
|
92 | + fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>'); |
|
93 | 93 | |
94 | 94 | //create Sax parser |
95 | 95 | $xmlParser = $this->initSaxParser(); |
96 | 96 | |
97 | - while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) { |
|
97 | + while ($this->currentBuffer = fread($this->originalFP, 4096)) { |
|
98 | 98 | /* |
99 | 99 | preprocess file |
100 | 100 | */ |
101 | 101 | // obfuscate entities because sax automatically does html_entity_decode |
102 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
102 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
103 | 103 | |
104 | 104 | //avoid cutting entities in half: |
105 | 105 | //the last fread could have truncated an entity (say, '<' in '&l'), thus invalidating the escaping |
@@ -107,27 +107,27 @@ discard block |
||
107 | 107 | // 9 is the max length of an entity. So, suppose that the & is at the end of buffer, |
108 | 108 | // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end |
109 | 109 | //it can't be an entity, exit the loop |
110 | - while ( true ) { |
|
111 | - $_ampPos = strpos( $temporary_check_buffer, '&' ); |
|
110 | + while (true) { |
|
111 | + $_ampPos = strpos($temporary_check_buffer, '&'); |
|
112 | 112 | |
113 | 113 | //check for real entity or escape it to safely exit from the loop!!! |
114 | - if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) { |
|
114 | + if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) { |
|
115 | 115 | break; |
116 | 116 | } |
117 | 117 | |
118 | 118 | //if an entity is still present, fetch some more and repeat the escaping |
119 | - $this->currentBuffer .= fread( $this->originalFP, 9 ); |
|
120 | - $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
119 | + $this->currentBuffer .= fread($this->originalFP, 9); |
|
120 | + $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | //free stuff outside the loop |
124 | - unset( $temporary_check_buffer ); |
|
124 | + unset($temporary_check_buffer); |
|
125 | 125 | |
126 | - $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
127 | - $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer ); |
|
126 | + $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
127 | + $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer); |
|
128 | 128 | |
129 | 129 | //get length of chunk |
130 | - $this->len = strlen( $this->currentBuffer ); |
|
130 | + $this->len = strlen($this->currentBuffer); |
|
131 | 131 | |
132 | 132 | /* |
133 | 133 | * Get the accumulated this->offset in the document: |
@@ -137,12 +137,12 @@ discard block |
||
137 | 137 | $this->offset += $this->len; |
138 | 138 | |
139 | 139 | //parse chunk of text |
140 | - $this->runParser( $xmlParser ); |
|
140 | + $this->runParser($xmlParser); |
|
141 | 141 | |
142 | 142 | } |
143 | 143 | |
144 | 144 | // close Sax parser |
145 | - $this->closeSaxParser( $xmlParser ); |
|
145 | + $this->closeSaxParser($xmlParser); |
|
146 | 146 | |
147 | 147 | } |
148 | 148 | |
@@ -151,15 +151,15 @@ discard block |
||
151 | 151 | * |
152 | 152 | * @return void |
153 | 153 | */ |
154 | - protected function runParser( $xmlParser ) { |
|
154 | + protected function runParser($xmlParser) { |
|
155 | 155 | //parse chunk of text |
156 | - if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) { |
|
156 | + if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) { |
|
157 | 157 | //if unable, raise an exception |
158 | - throw new RuntimeException( sprintf( |
|
158 | + throw new RuntimeException(sprintf( |
|
159 | 159 | "XML error: %s at line %d", |
160 | - xml_error_string( xml_get_error_code( $xmlParser ) ), |
|
161 | - xml_get_current_line_number( $xmlParser ) |
|
162 | - ) ); |
|
160 | + xml_error_string(xml_get_error_code($xmlParser)), |
|
161 | + xml_get_current_line_number($xmlParser) |
|
162 | + )); |
|
163 | 163 | } |
164 | 164 | } |
165 | 165 | |
@@ -168,24 +168,24 @@ discard block |
||
168 | 168 | * |
169 | 169 | * @return string |
170 | 170 | */ |
171 | - protected function getLastCharacter( $parser ): string { |
|
171 | + protected function getLastCharacter($parser): string { |
|
172 | 172 | |
173 | 173 | //this logic helps detecting empty tags |
174 | 174 | //get current position of SAX pointer in all the stream of data is has read so far: |
175 | 175 | //it points at the end of current tag |
176 | - $idx = xml_get_current_byte_index( $parser ); |
|
176 | + $idx = xml_get_current_byte_index($parser); |
|
177 | 177 | |
178 | 178 | //check whether the bounds of current tag are entirely in current buffer or the end of the current tag |
179 | 179 | //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
180 | 180 | //this check is necessary because we may have truncated a tag in half with current read, |
181 | 181 | //and the other half may be encountered in the next buffer it will be passed |
182 | - if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) { |
|
182 | + if (isset($this->currentBuffer[$idx - $this->offset])) { |
|
183 | 183 | //if this tag entire lenght fitted in the buffer, the last char must be the last |
184 | 184 | //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
185 | - $lastChar = $this->currentBuffer[ $idx - $this->offset ]; |
|
185 | + $lastChar = $this->currentBuffer[$idx - $this->offset]; |
|
186 | 186 | } else { |
187 | 187 | //if it's out, simple use the last character of the chunk |
188 | - $lastChar = $this->currentBuffer[ $this->len - 1 ]; |
|
188 | + $lastChar = $this->currentBuffer[$this->len - 1]; |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | return $lastChar; |
@@ -199,19 +199,19 @@ discard block |
||
199 | 199 | return "§" . |
200 | 200 | substr( |
201 | 201 | str_replace( |
202 | - [ '+', '/' ], |
|
202 | + ['+', '/'], |
|
203 | 203 | '', |
204 | - base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) ) |
|
204 | + base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong)) |
|
205 | 205 | ), |
206 | 206 | 0, |
207 | 207 | 4 |
208 | 208 | ); |
209 | 209 | } |
210 | 210 | |
211 | - private function createOutputFileIfDoesNotExist( string $outputFilePath ) { |
|
211 | + private function createOutputFileIfDoesNotExist(string $outputFilePath) { |
|
212 | 212 | // create output file |
213 | - if ( !file_exists( $outputFilePath ) ) { |
|
214 | - touch( $outputFilePath ); |
|
213 | + if (!file_exists($outputFilePath)) { |
|
214 | + touch($outputFilePath); |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
@@ -219,13 +219,13 @@ discard block |
||
219 | 219 | * @param string $originalXliffPath |
220 | 220 | * @param string $outputFilePath |
221 | 221 | */ |
222 | - private function setFileDescriptors( string $originalXliffPath, string $outputFilePath ) { |
|
223 | - $this->outputFP = fopen( $outputFilePath, 'w+' ); |
|
222 | + private function setFileDescriptors(string $originalXliffPath, string $outputFilePath) { |
|
223 | + $this->outputFP = fopen($outputFilePath, 'w+'); |
|
224 | 224 | |
225 | 225 | $streamArgs = null; |
226 | 226 | |
227 | - if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) { |
|
228 | - throw new RuntimeException( "could not open XML input" ); |
|
227 | + if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) { |
|
228 | + throw new RuntimeException("could not open XML input"); |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | |
@@ -235,8 +235,8 @@ discard block |
||
235 | 235 | public function __destruct() { |
236 | 236 | //this stream can be closed outside the class |
237 | 237 | //to permit multiple concurrent downloads, so suppress warnings |
238 | - @fclose( $this->originalFP ); |
|
239 | - fclose( $this->outputFP ); |
|
238 | + @fclose($this->originalFP); |
|
239 | + fclose($this->outputFP); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | /** |
@@ -245,11 +245,11 @@ discard block |
||
245 | 245 | * @return resource |
246 | 246 | */ |
247 | 247 | protected function initSaxParser() { |
248 | - $xmlSaxParser = xml_parser_create( 'UTF-8' ); |
|
249 | - xml_set_object( $xmlSaxParser, $this ); |
|
250 | - xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false ); |
|
251 | - xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' ); |
|
252 | - xml_set_character_data_handler( $xmlSaxParser, 'characterData' ); |
|
248 | + $xmlSaxParser = xml_parser_create('UTF-8'); |
|
249 | + xml_set_object($xmlSaxParser, $this); |
|
250 | + xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false); |
|
251 | + xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose'); |
|
252 | + xml_set_character_data_handler($xmlSaxParser, 'characterData'); |
|
253 | 253 | |
254 | 254 | return $xmlSaxParser; |
255 | 255 | } |
@@ -257,8 +257,8 @@ discard block |
||
257 | 257 | /** |
258 | 258 | * @param resource $xmlSaxParser |
259 | 259 | */ |
260 | - protected function closeSaxParser( $xmlSaxParser ) { |
|
261 | - xml_parser_free( $xmlSaxParser ); |
|
260 | + protected function closeSaxParser($xmlSaxParser) { |
|
261 | + xml_parser_free($xmlSaxParser); |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | * |
269 | 269 | * @return mixed |
270 | 270 | */ |
271 | - abstract protected function tagOpen( $parser, string $name, array $attr ); |
|
271 | + abstract protected function tagOpen($parser, string $name, array $attr); |
|
272 | 272 | |
273 | 273 | /** |
274 | 274 | * @param resource $parser |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | * |
277 | 277 | * @return mixed |
278 | 278 | */ |
279 | - abstract protected function tagClose( $parser, string $name ); |
|
279 | + abstract protected function tagClose($parser, string $name); |
|
280 | 280 | |
281 | 281 | /** |
282 | 282 | * @param resource $parser |
@@ -284,11 +284,11 @@ discard block |
||
284 | 284 | * |
285 | 285 | * @return mixed |
286 | 286 | */ |
287 | - protected function characterData( $parser, string $data ): void { |
|
287 | + protected function characterData($parser, string $data): void { |
|
288 | 288 | // don't write <target> data |
289 | - if ( !$this->inTarget && !$this->bufferIsActive ) { |
|
290 | - $this->postProcAndFlush( $this->outputFP, $data ); |
|
291 | - } elseif ( $this->bufferIsActive ) { |
|
289 | + if (!$this->inTarget && !$this->bufferIsActive) { |
|
290 | + $this->postProcAndFlush($this->outputFP, $data); |
|
291 | + } elseif ($this->bufferIsActive) { |
|
292 | 292 | $this->CDATABuffer .= $data; |
293 | 293 | } |
294 | 294 | } |
@@ -300,19 +300,19 @@ discard block |
||
300 | 300 | * @param string $data |
301 | 301 | * @param bool $treatAsCDATA |
302 | 302 | */ |
303 | - protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) { |
|
303 | + protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false) { |
|
304 | 304 | //postprocess string |
305 | - $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data ); |
|
306 | - $data = str_replace( ' ', ' ', $data ); |
|
307 | - if ( !$treatAsCDATA ) { |
|
305 | + $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data); |
|
306 | + $data = str_replace(' ', ' ', $data); |
|
307 | + if (!$treatAsCDATA) { |
|
308 | 308 | //unix2dos |
309 | - $data = str_replace( "\r\n", "\r", $data ); |
|
310 | - $data = str_replace( "\n", "\r", $data ); |
|
311 | - $data = str_replace( "\r", "\r\n", $data ); |
|
309 | + $data = str_replace("\r\n", "\r", $data); |
|
310 | + $data = str_replace("\n", "\r", $data); |
|
311 | + $data = str_replace("\r", "\r\n", $data); |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | //flush to disk |
315 | - fwrite( $fp, $data ); |
|
315 | + fwrite($fp, $data); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | /** |
@@ -321,19 +321,19 @@ discard block |
||
321 | 321 | * |
322 | 322 | * @return void |
323 | 323 | */ |
324 | - protected function handleOpenUnit( string $name, array $attr ) { |
|
324 | + protected function handleOpenUnit(string $name, array $attr) { |
|
325 | 325 | |
326 | 326 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
327 | - if ( $this->tuTagName === $name ) { |
|
327 | + if ($this->tuTagName === $name) { |
|
328 | 328 | $this->inTU = true; |
329 | 329 | |
330 | 330 | // get id |
331 | 331 | // trim to first 100 characters because this is the limit on Matecat's DB |
332 | - $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
|
332 | + $this->currentTransUnitId = substr($attr['id'], 0, 100); |
|
333 | 333 | |
334 | 334 | // `translate` attribute can be only yes or no |
335 | 335 | // current 'translate' attribute of the current trans-unit |
336 | - $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ]; |
|
336 | + $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate']; |
|
337 | 337 | |
338 | 338 | $this->setLastTransUnitSegments(); |
339 | 339 | |
@@ -347,15 +347,15 @@ discard block |
||
347 | 347 | * |
348 | 348 | * @return string |
349 | 349 | */ |
350 | - protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { |
|
350 | + protected function handleOpenXliffTag(string $name, array $attr, string $tag): string { |
|
351 | 351 | |
352 | 352 | // Add MateCat specific namespace. |
353 | 353 | // Add trgLang |
354 | - if ( $name === 'xliff' ) { |
|
355 | - if ( !array_key_exists( 'xmlns:mtc', $attr ) ) { |
|
354 | + if ($name === 'xliff') { |
|
355 | + if (!array_key_exists('xmlns:mtc', $attr)) { |
|
356 | 356 | $tag .= ' xmlns:mtc="https://www.matecat.com" '; |
357 | 357 | } |
358 | - $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag ); |
|
358 | + $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag); |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | return $tag; |
@@ -367,10 +367,10 @@ discard block |
||
367 | 367 | * |
368 | 368 | * @return void |
369 | 369 | */ |
370 | - protected function checkSetInTarget( string $name ) { |
|
370 | + protected function checkSetInTarget(string $name) { |
|
371 | 371 | // check if we are entering into a <target> |
372 | - if ( 'target' === $name ) { |
|
373 | - if ( $this->currentTransUnitIsTranslatable === 'no' ) { |
|
372 | + if ('target' === $name) { |
|
373 | + if ($this->currentTransUnitIsTranslatable === 'no') { |
|
374 | 374 | $this->inTarget = false; |
375 | 375 | } else { |
376 | 376 | $this->inTarget = true; |
@@ -383,8 +383,8 @@ discard block |
||
383 | 383 | * |
384 | 384 | * @return void |
385 | 385 | */ |
386 | - protected function setInBuffer( string $name ) { |
|
387 | - if ( in_array( $name, $this->nodesToBuffer ) ) { |
|
386 | + protected function setInBuffer(string $name) { |
|
387 | + if (in_array($name, $this->nodesToBuffer)) { |
|
388 | 388 | $this->bufferIsActive = true; |
389 | 389 | } |
390 | 390 | } |
@@ -392,24 +392,24 @@ discard block |
||
392 | 392 | /** |
393 | 393 | * @param array $seg |
394 | 394 | */ |
395 | - protected function updateSegmentCounts( array $seg = [] ) { |
|
395 | + protected function updateSegmentCounts(array $seg = []) { |
|
396 | 396 | |
397 | - $raw_word_count = $seg[ 'raw_word_count' ]; |
|
398 | - $eq_word_count = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 ); |
|
397 | + $raw_word_count = $seg['raw_word_count']; |
|
398 | + $eq_word_count = (floor($seg['eq_word_count'] * 100) / 100); |
|
399 | 399 | |
400 | - $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [ |
|
400 | + $this->counts['segments_count_array'][$seg['sid']] = [ |
|
401 | 401 | 'raw_word_count' => $raw_word_count, |
402 | 402 | 'eq_word_count' => $eq_word_count, |
403 | 403 | ]; |
404 | 404 | |
405 | - $this->counts[ 'raw_word_count' ] += $raw_word_count; |
|
406 | - $this->counts[ 'eq_word_count' ] += $eq_word_count; |
|
405 | + $this->counts['raw_word_count'] += $raw_word_count; |
|
406 | + $this->counts['eq_word_count'] += $eq_word_count; |
|
407 | 407 | } |
408 | 408 | |
409 | 409 | protected function resetCounts() { |
410 | - $this->counts[ 'segments_count_array' ] = []; |
|
411 | - $this->counts[ 'raw_word_count' ] = 0; |
|
412 | - $this->counts[ 'eq_word_count' ] = 0; |
|
410 | + $this->counts['segments_count_array'] = []; |
|
411 | + $this->counts['raw_word_count'] = 0; |
|
412 | + $this->counts['eq_word_count'] = 0; |
|
413 | 413 | } |
414 | 414 | |
415 | 415 | /** |
@@ -418,16 +418,16 @@ discard block |
||
418 | 418 | * |
419 | 419 | * @return void |
420 | 420 | */ |
421 | - protected function checkForSelfClosedTagAndFlush( $parser, string $tag ) { |
|
421 | + protected function checkForSelfClosedTagAndFlush($parser, string $tag) { |
|
422 | 422 | |
423 | - $lastChar = $this->getLastCharacter( $parser ); |
|
423 | + $lastChar = $this->getLastCharacter($parser); |
|
424 | 424 | |
425 | 425 | //trim last space |
426 | - $tag = rtrim( $tag ); |
|
426 | + $tag = rtrim($tag); |
|
427 | 427 | |
428 | 428 | //detect empty tag |
429 | 429 | $this->isEmpty = $lastChar == '/'; |
430 | - if ( $this->isEmpty ) { |
|
430 | + if ($this->isEmpty) { |
|
431 | 431 | $tag .= $lastChar; |
432 | 432 | } |
433 | 433 | |
@@ -435,11 +435,11 @@ discard block |
||
435 | 435 | $tag .= ">"; |
436 | 436 | |
437 | 437 | //set a Buffer for the segSource Source tag |
438 | - if ( $this->bufferIsActive ) { // we are opening a critical CDATA section |
|
438 | + if ($this->bufferIsActive) { // we are opening a critical CDATA section |
|
439 | 439 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
440 | 440 | $this->CDATABuffer .= $tag; |
441 | 441 | } else { |
442 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
442 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | } |
@@ -464,18 +464,18 @@ discard block |
||
464 | 464 | */ |
465 | 465 | $this->lastTransUnit = []; |
466 | 466 | |
467 | - if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
467 | + if (!isset($this->transUnits[$this->currentTransUnitId])) { |
|
468 | 468 | return; |
469 | 469 | } |
470 | 470 | |
471 | - $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
|
471 | + $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId]; |
|
472 | 472 | $last_value = null; |
473 | - $segmentsCount = count( $listOfSegmentsIds ); |
|
474 | - for ( $i = 0; $i < $segmentsCount; $i++ ) { |
|
475 | - $id = $listOfSegmentsIds[ $i ]; |
|
476 | - if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
|
477 | - $last_value = $listOfSegmentsIds[ $i ]; |
|
478 | - $this->lastTransUnit[] = $this->segments[ $id ]; |
|
473 | + $segmentsCount = count($listOfSegmentsIds); |
|
474 | + for ($i = 0; $i < $segmentsCount; $i++) { |
|
475 | + $id = $listOfSegmentsIds[$i]; |
|
476 | + if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) { |
|
477 | + $last_value = $listOfSegmentsIds[$i]; |
|
478 | + $this->lastTransUnit[] = $this->segments[$id]; |
|
479 | 479 | } |
480 | 480 | } |
481 | 481 | |
@@ -485,8 +485,8 @@ discard block |
||
485 | 485 | * @return array |
486 | 486 | */ |
487 | 487 | protected function getCurrentSegment(): array { |
488 | - if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
489 | - return $this->segments[ $this->segmentInUnitPosition ]; |
|
488 | + if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) { |
|
489 | + return $this->segments[$this->segmentInUnitPosition]; |
|
490 | 490 | } |
491 | 491 | |
492 | 492 | return []; |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | /** |
21 | 21 | * @var bool |
22 | 22 | */ |
23 | - protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
23 | + protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
24 | 24 | /** |
25 | 25 | * @var array |
26 | 26 | */ |
@@ -35,22 +35,22 @@ discard block |
||
35 | 35 | /** |
36 | 36 | * @inheritDoc |
37 | 37 | */ |
38 | - protected function tagOpen( $parser, string $name, array $attr ) { |
|
38 | + protected function tagOpen($parser, string $name, array $attr) { |
|
39 | 39 | |
40 | - $this->handleOpenUnit( $name, $attr ); |
|
40 | + $this->handleOpenUnit($name, $attr); |
|
41 | 41 | |
42 | - if ( 'mda:metadata' === $name ) { |
|
42 | + if ('mda:metadata' === $name) { |
|
43 | 43 | $this->unitContainsMda = true; |
44 | 44 | } |
45 | 45 | |
46 | - $this->checkSetInTarget( $name ); |
|
46 | + $this->checkSetInTarget($name); |
|
47 | 47 | |
48 | 48 | // open buffer |
49 | - $this->setInBuffer( $name ); |
|
49 | + $this->setInBuffer($name); |
|
50 | 50 | |
51 | 51 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
52 | 52 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
53 | - if ( !$this->inTarget ) { |
|
53 | + if (!$this->inTarget) { |
|
54 | 54 | |
55 | 55 | $tag = ''; |
56 | 56 | |
@@ -72,48 +72,48 @@ discard block |
||
72 | 72 | // |
73 | 73 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
74 | 74 | // |
75 | - if ( in_array( $name, [ 'notes', 'originalData', 'segment', 'ignorable' ] ) && |
|
75 | + if (in_array($name, ['notes', 'originalData', 'segment', 'ignorable']) && |
|
76 | 76 | $this->unitContainsMda === false && |
77 | - !empty( $this->transUnits[ $this->currentTransUnitId ] ) && |
|
77 | + !empty($this->transUnits[$this->currentTransUnitId]) && |
|
78 | 78 | !$this->hasWrittenCounts |
79 | 79 | ) { |
80 | 80 | // we need to update counts here |
81 | 81 | $this->updateCounts(); |
82 | 82 | $this->hasWrittenCounts = true; |
83 | - $tag .= $this->getWordCountGroupForXliffV2(); |
|
83 | + $tag .= $this->getWordCountGroupForXliffV2(); |
|
84 | 84 | $this->unitContainsMda = true; |
85 | 85 | } |
86 | 86 | |
87 | 87 | // construct tag |
88 | 88 | $tag .= "<$name "; |
89 | 89 | |
90 | - foreach ( $attr as $k => $v ) { |
|
90 | + foreach ($attr as $k => $v) { |
|
91 | 91 | //normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute |
92 | - if ( $k != 'state' ) { |
|
92 | + if ($k != 'state') { |
|
93 | 93 | $tag .= "$k=\"$v\" "; |
94 | 94 | } |
95 | 95 | } |
96 | 96 | |
97 | 97 | $seg = $this->getCurrentSegment(); |
98 | 98 | |
99 | - if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) { |
|
99 | + if ($name === $this->tuTagName and !empty($seg) and isset($seg['sid'])) { |
|
100 | 100 | |
101 | 101 | // add `mtc:segment-id` to xliff v.2* |
102 | - if ( strpos( $tag, 'mtc:segment-id' ) === false ) { |
|
103 | - $tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" "; |
|
102 | + if (strpos($tag, 'mtc:segment-id') === false) { |
|
103 | + $tag .= "mtc:segment-id=\"{$seg['sid']}\" "; |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | } |
107 | 107 | |
108 | 108 | // replace state for xliff v2 |
109 | - if ( 'segment' === $name ) { // add state to segment in Xliff v2 |
|
110 | - [ $stateProp, ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ] ); |
|
109 | + if ('segment' === $name) { // add state to segment in Xliff v2 |
|
110 | + [$stateProp, ] = StatusToStateAttribute::getState($this->xliffVersion, $seg['status']); |
|
111 | 111 | $tag .= $stateProp; |
112 | 112 | } |
113 | 113 | |
114 | - $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
|
114 | + $tag = $this->handleOpenXliffTag($name, $attr, $tag); |
|
115 | 115 | |
116 | - $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
|
116 | + $this->checkForSelfClosedTagAndFlush($parser, $tag); |
|
117 | 117 | |
118 | 118 | } |
119 | 119 | |
@@ -126,10 +126,10 @@ discard block |
||
126 | 126 | * |
127 | 127 | * @return string |
128 | 128 | */ |
129 | - protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { |
|
130 | - $tag = parent::handleOpenXliffTag( $name, $attr, $tag ); |
|
129 | + protected function handleOpenXliffTag(string $name, array $attr, string $tag): string { |
|
130 | + $tag = parent::handleOpenXliffTag($name, $attr, $tag); |
|
131 | 131 | // add oasis xliff 20 namespace |
132 | - if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
|
132 | + if ($name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) { |
|
133 | 133 | $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
134 | 134 | } |
135 | 135 | |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | /** |
140 | 140 | * @inheritDoc |
141 | 141 | */ |
142 | - protected function tagClose( $parser, string $name ) { |
|
142 | + protected function tagClose($parser, string $name) { |
|
143 | 143 | $tag = ''; |
144 | 144 | |
145 | 145 | /** |
@@ -148,32 +148,32 @@ discard block |
||
148 | 148 | * |
149 | 149 | * self::tagOpen method |
150 | 150 | */ |
151 | - if ( !$this->isEmpty ) { |
|
151 | + if (!$this->isEmpty) { |
|
152 | 152 | |
153 | - if ( !$this->inTarget ) { |
|
153 | + if (!$this->inTarget) { |
|
154 | 154 | $tag = "</$name>"; |
155 | 155 | } |
156 | 156 | |
157 | - if ( 'target' == $name ) { |
|
157 | + if ('target' == $name) { |
|
158 | 158 | |
159 | - if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
|
159 | + if (isset($this->transUnits[$this->currentTransUnitId])) { |
|
160 | 160 | |
161 | 161 | $seg = $this->getCurrentSegment(); |
162 | 162 | |
163 | 163 | // update counts |
164 | - if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
|
165 | - $this->updateSegmentCounts( $seg ); |
|
164 | + if (!$this->hasWrittenCounts && !empty($seg)) { |
|
165 | + $this->updateSegmentCounts($seg); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | // delete translations so the prepareSegment |
169 | 169 | // will put source content in target tag |
170 | - if ( $this->sourceInTarget ) { |
|
171 | - $seg[ 'translation' ] = ''; |
|
170 | + if ($this->sourceInTarget) { |
|
171 | + $seg['translation'] = ''; |
|
172 | 172 | $this->resetCounts(); |
173 | 173 | } |
174 | 174 | |
175 | 175 | // append $translation |
176 | - $translation = $this->prepareTranslation( $seg ); |
|
176 | + $translation = $this->prepareTranslation($seg); |
|
177 | 177 | |
178 | 178 | //append translation |
179 | 179 | $tag = "<target>$translation</target>"; |
@@ -183,22 +183,22 @@ discard block |
||
183 | 183 | // signal we are leaving a target |
184 | 184 | $this->targetWasWritten = true; |
185 | 185 | $this->inTarget = false; |
186 | - $this->postProcAndFlush( $this->outputFP, $tag, true ); |
|
186 | + $this->postProcAndFlush($this->outputFP, $tag, true); |
|
187 | 187 | |
188 | - } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
|
188 | + } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section |
|
189 | 189 | |
190 | 190 | $this->bufferIsActive = false; |
191 | 191 | |
192 | 192 | // only for Xliff 2.* |
193 | 193 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
194 | - if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) { |
|
194 | + if ('mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts) { |
|
195 | 195 | |
196 | 196 | // we need to update counts here |
197 | 197 | $this->updateCounts(); |
198 | 198 | $this->hasWrittenCounts = true; |
199 | 199 | |
200 | 200 | $tag = $this->CDATABuffer; |
201 | - $tag .= $this->getWordCountGroupForXliffV2( false ); |
|
201 | + $tag .= $this->getWordCountGroupForXliffV2(false); |
|
202 | 202 | $tag .= " </mda:metadata>"; |
203 | 203 | |
204 | 204 | } else { |
@@ -208,19 +208,19 @@ discard block |
||
208 | 208 | $this->CDATABuffer = ""; |
209 | 209 | |
210 | 210 | //flush to the pointer |
211 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
211 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
212 | 212 | |
213 | - } elseif ( 'segment' === $name ) { |
|
213 | + } elseif ('segment' === $name) { |
|
214 | 214 | |
215 | 215 | // only for Xliff 2.* |
216 | 216 | // if segment has no <target> add it BEFORE </segment> |
217 | - if ( !$this->targetWasWritten ) { |
|
217 | + if (!$this->targetWasWritten) { |
|
218 | 218 | |
219 | 219 | $seg = $this->getCurrentSegment(); |
220 | 220 | |
221 | - if ( isset( $seg[ 'translation' ] ) ) { |
|
221 | + if (isset($seg['translation'])) { |
|
222 | 222 | |
223 | - $translation = $this->prepareTranslation( $seg ); |
|
223 | + $translation = $this->prepareTranslation($seg); |
|
224 | 224 | // replace the tag |
225 | 225 | $tag = "<target>$translation</target>"; |
226 | 226 | |
@@ -233,17 +233,17 @@ discard block |
||
233 | 233 | // update segmentPositionInTu |
234 | 234 | $this->segmentInUnitPosition++; |
235 | 235 | |
236 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
236 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
237 | 237 | |
238 | 238 | // we are leaving <segment>, reset $segmentHasTarget |
239 | 239 | $this->targetWasWritten = false; |
240 | 240 | |
241 | - } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
241 | + } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
|
242 | 242 | $this->CDATABuffer .= "</$name>"; |
243 | 243 | // Do NOT Flush |
244 | 244 | } else { //generic tag closure do Nothing |
245 | 245 | // flush to pointer |
246 | - $this->postProcAndFlush( $this->outputFP, $tag ); |
|
246 | + $this->postProcAndFlush($this->outputFP, $tag); |
|
247 | 247 | } |
248 | 248 | } else { |
249 | 249 | //ok, nothing to be done; reset flag for next coming tag |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | } |
252 | 252 | |
253 | 253 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
254 | - if ( $this->tuTagName === $name ) { |
|
254 | + if ($this->tuTagName === $name) { |
|
255 | 255 | $this->currentTransUnitIsTranslatable = null; |
256 | 256 | $this->inTU = false; |
257 | 257 | $this->unitContainsMda = false; |
@@ -267,8 +267,8 @@ discard block |
||
267 | 267 | private function updateCounts() { |
268 | 268 | |
269 | 269 | $seg = $this->getCurrentSegment(); |
270 | - if ( !empty( $seg ) ) { |
|
271 | - $this->updateSegmentCounts( $seg ); |
|
270 | + if (!empty($seg)) { |
|
271 | + $this->updateSegmentCounts($seg); |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | } |
@@ -278,30 +278,30 @@ discard block |
||
278 | 278 | * |
279 | 279 | * @return string |
280 | 280 | */ |
281 | - private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string { |
|
281 | + private function getWordCountGroupForXliffV2(bool $withMetadataTag = true): string { |
|
282 | 282 | |
283 | 283 | $this->mdaGroupCounter++; |
284 | - $segments_count_array = $this->counts[ 'segments_count_array' ]; |
|
284 | + $segments_count_array = $this->counts['segments_count_array']; |
|
285 | 285 | |
286 | 286 | $tag = ''; |
287 | 287 | |
288 | - if ( $withMetadataTag === true ) { |
|
288 | + if ($withMetadataTag === true) { |
|
289 | 289 | $tag .= '<mda:metadata>'; |
290 | 290 | } |
291 | 291 | |
292 | 292 | $index = 0; |
293 | - foreach ( $segments_count_array as $segments_count_item ) { |
|
293 | + foreach ($segments_count_array as $segments_count_item) { |
|
294 | 294 | |
295 | 295 | $id = 'word_count_tu[' . $this->currentTransUnitId . '][' . $index . ']'; |
296 | 296 | $index++; |
297 | 297 | |
298 | 298 | $tag .= " <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\"> |
299 | - <mda:meta type=\"x-matecat-raw\">" . $segments_count_item[ 'raw_word_count' ] . "</mda:meta> |
|
300 | - <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item[ 'eq_word_count' ] . "</mda:meta> |
|
299 | + <mda:meta type=\"x-matecat-raw\">" . $segments_count_item['raw_word_count'] . "</mda:meta> |
|
300 | + <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item['eq_word_count'] . "</mda:meta> |
|
301 | 301 | </mda:metaGroup>"; |
302 | 302 | } |
303 | 303 | |
304 | - if ( $withMetadataTag === true ) { |
|
304 | + if ($withMetadataTag === true) { |
|
305 | 305 | $tag .= '</mda:metadata>'; |
306 | 306 | } |
307 | 307 | |
@@ -316,18 +316,18 @@ discard block |
||
316 | 316 | * |
317 | 317 | * @return string |
318 | 318 | */ |
319 | - protected function prepareTranslation( array $seg ): string { |
|
319 | + protected function prepareTranslation(array $seg): string { |
|
320 | 320 | |
321 | - $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
|
322 | - $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
|
323 | - $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
|
321 | + $segment = Strings::removeDangerousChars($seg ['segment']); |
|
322 | + $translation = Strings::removeDangerousChars($seg ['translation']); |
|
323 | + $dataRefMap = (isset($seg['data_ref_map'])) ? Strings::jsonToArray($seg['data_ref_map']) : []; |
|
324 | 324 | |
325 | - if ( $seg [ 'translation' ] == '' ) { |
|
325 | + if ($seg ['translation'] == '') { |
|
326 | 326 | $translation = $segment; |
327 | 327 | } else { |
328 | - if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
|
329 | - $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
|
330 | - if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
|
328 | + if ($this->callback instanceof XliffReplacerCallbackInterface) { |
|
329 | + $error = (!empty($seg['error'])) ? $seg['error'] : null; |
|
330 | + if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) { |
|
331 | 331 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
332 | 332 | } |
333 | 333 | } |