@@ -17,8 +17,8 @@ discard block |
||
| 17 | 17 | * @return string |
| 18 | 18 | * @throws Exception |
| 19 | 19 | */ |
| 20 | - public static function cleanCDATA( string $testString ): string { |
|
| 21 | - $cleanXMLContent = new SimpleXMLElement( '<rootNoteNode>' . $testString . '</rootNoteNode>', LIBXML_NOCDATA ); |
|
| 20 | + public static function cleanCDATA(string $testString): string { |
|
| 21 | + $cleanXMLContent = new SimpleXMLElement('<rootNoteNode>' . $testString . '</rootNoteNode>', LIBXML_NOCDATA); |
|
| 22 | 22 | |
| 23 | 23 | return $cleanXMLContent->__toString(); |
| 24 | 24 | } |
@@ -28,28 +28,28 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return bool |
| 30 | 30 | */ |
| 31 | - public static function isJSON( string $string ): bool { |
|
| 32 | - if ( is_numeric( $string ) ) { |
|
| 31 | + public static function isJSON(string $string): bool { |
|
| 32 | + if (is_numeric($string)) { |
|
| 33 | 33 | return false; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | try { |
| 37 | - $string = Strings::cleanCDATA( $string ); |
|
| 38 | - } catch ( Exception $e ) { |
|
| 37 | + $string = Strings::cleanCDATA($string); |
|
| 38 | + } catch (Exception $e) { |
|
| 39 | 39 | return false; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - $string = trim( $string ); |
|
| 43 | - if ( empty( $string ) ) { |
|
| 42 | + $string = trim($string); |
|
| 43 | + if (empty($string)) { |
|
| 44 | 44 | return false; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // String representation in json is "quoted", but we want to accept only object or arrays. |
| 48 | 48 | // exclude strings and numbers and other primitive types |
| 49 | - if ( in_array( $string [ 0 ], [ "{", "[" ] ) ) { |
|
| 50 | - json_decode( $string ); |
|
| 49 | + if (in_array($string [0], ["{", "["])) { |
|
| 50 | + json_decode($string); |
|
| 51 | 51 | |
| 52 | - return empty( self::getLastJsonError()[ 0 ] ); |
|
| 52 | + return empty(self::getLastJsonError()[0]); |
|
| 53 | 53 | } else { |
| 54 | 54 | return false; // Not accepted: string or primitive types. |
| 55 | 55 | } |
@@ -61,10 +61,10 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return array |
| 63 | 63 | */ |
| 64 | - public static function jsonToArray( string $string ): array { |
|
| 65 | - $decodedJSON = json_decode( $string, true ); |
|
| 64 | + public static function jsonToArray(string $string): array { |
|
| 65 | + $decodedJSON = json_decode($string, true); |
|
| 66 | 66 | |
| 67 | - return ( is_array( $decodedJSON ) ) ? $decodedJSON : []; |
|
| 67 | + return (is_array($decodedJSON)) ? $decodedJSON : []; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private static function raiseLastJsonException() { |
| 75 | 75 | |
| 76 | - [ $msg, $error ] = self::getLastJsonError(); |
|
| 76 | + [$msg, $error] = self::getLastJsonError(); |
|
| 77 | 77 | |
| 78 | - if ( $error != JSON_ERROR_NONE ) { |
|
| 79 | - throw new NotValidJSONException( $msg, $error ); |
|
| 78 | + if ($error != JSON_ERROR_NONE) { |
|
| 79 | + throw new NotValidJSONException($msg, $error); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | } |
@@ -86,11 +86,11 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | private static function getLastJsonError(): array { |
| 88 | 88 | |
| 89 | - if ( function_exists( "json_last_error" ) ) { |
|
| 89 | + if (function_exists("json_last_error")) { |
|
| 90 | 90 | |
| 91 | 91 | $error = json_last_error(); |
| 92 | 92 | |
| 93 | - switch ( $error ) { |
|
| 93 | + switch ($error) { |
|
| 94 | 94 | case JSON_ERROR_NONE: |
| 95 | 95 | $msg = null; # - No errors |
| 96 | 96 | break; |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | break; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - return [ $msg, $error ]; |
|
| 117 | + return [$msg, $error]; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - return [ null, JSON_ERROR_NONE ]; |
|
| 120 | + return [null, JSON_ERROR_NONE]; |
|
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | |
@@ -140,11 +140,11 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | - public static function fixNonWellFormedXml( string $content, ?bool $escapeStrings = true ): string { |
|
| 144 | - if ( self::$find_xliff_tags_reg === null ) { |
|
| 143 | + public static function fixNonWellFormedXml(string $content, ?bool $escapeStrings = true): string { |
|
| 144 | + if (self::$find_xliff_tags_reg === null) { |
|
| 145 | 145 | // Convert the list of tags in a regexp list, for example "g|x|bx|ex" |
| 146 | 146 | $xliffTags = XliffTags::$tags; |
| 147 | - $xliff_tags_reg_list = implode( '|', $xliffTags ); |
|
| 147 | + $xliff_tags_reg_list = implode('|', $xliffTags); |
|
| 148 | 148 | // Regexp to find all the XLIFF tags: |
| 149 | 149 | // </? -> matches the tag start, for both opening and |
| 150 | 150 | // closure tags (see the optional slash) |
@@ -165,30 +165,30 @@ discard block |
||
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Find all the XLIFF tags |
| 168 | - preg_match_all( self::$find_xliff_tags_reg, $content, $matches ); |
|
| 169 | - $tags = (array)$matches[ 0 ]; |
|
| 168 | + preg_match_all(self::$find_xliff_tags_reg, $content, $matches); |
|
| 169 | + $tags = (array)$matches[0]; |
|
| 170 | 170 | |
| 171 | 171 | // Prepare placeholders |
| 172 | 172 | $tags_placeholders = []; |
| 173 | - $tagsNum = count( $tags ); |
|
| 174 | - for ( $i = 0; $i < $tagsNum; $i++ ) { |
|
| 175 | - $tag = $tags[ $i ]; |
|
| 176 | - $tags_placeholders[ $tag ] = "#@!XLIFF-TAG-$i!@#"; |
|
| 173 | + $tagsNum = count($tags); |
|
| 174 | + for ($i = 0; $i < $tagsNum; $i++) { |
|
| 175 | + $tag = $tags[$i]; |
|
| 176 | + $tags_placeholders[$tag] = "#@!XLIFF-TAG-$i!@#"; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Replace all XLIFF tags with placeholders that will not be escaped |
| 180 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
| 181 | - $content = str_replace( $tag, $placeholder, $content ); |
|
| 180 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
| 181 | + $content = str_replace($tag, $placeholder, $content); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Escape the string with the remaining non-XLIFF tags |
| 185 | - if ( $escapeStrings ) { |
|
| 186 | - $content = htmlspecialchars( $content, ENT_NOQUOTES, 'UTF-8', false ); |
|
| 185 | + if ($escapeStrings) { |
|
| 186 | + $content = htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8', false); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // Put again in place the original XLIFF tags replacing placeholders |
| 190 | - foreach ( $tags_placeholders as $tag => $placeholder ) { |
|
| 191 | - $content = str_replace( $placeholder, $tag, $content ); |
|
| 190 | + foreach ($tags_placeholders as $tag => $placeholder) { |
|
| 191 | + $content = str_replace($placeholder, $tag, $content); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | return $content; |
@@ -199,17 +199,17 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return string |
| 201 | 201 | */ |
| 202 | - public static function removeDangerousChars( $string ): string { |
|
| 202 | + public static function removeDangerousChars($string): string { |
|
| 203 | 203 | // clean invalid xml entities ( characters with ascii < 32 and different from 0A, 0D and 09 |
| 204 | 204 | $regexpEntity = '/&#x(0[0-8BCEF]|1[\dA-F]|7F);/u'; |
| 205 | 205 | |
| 206 | 206 | // remove binary chars in some xliff files |
| 207 | 207 | $regexpAscii = '/[\x{00}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}\x{7F}]/u'; |
| 208 | 208 | |
| 209 | - $string = preg_replace( $regexpAscii, '', $string ); |
|
| 210 | - $string = preg_replace( $regexpEntity, '', $string ); |
|
| 209 | + $string = preg_replace($regexpAscii, '', $string); |
|
| 210 | + $string = preg_replace($regexpEntity, '', $string); |
|
| 211 | 211 | |
| 212 | - return !empty( $string ) || strlen( $string ) > 0 ? $string : ""; |
|
| 212 | + return !empty($string) || strlen($string) > 0 ? $string : ""; |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | |
@@ -219,15 +219,15 @@ discard block |
||
| 219 | 219 | * |
| 220 | 220 | * @return string |
| 221 | 221 | */ |
| 222 | - public static function htmlspecialchars_decode( string $string, ?bool $onlyEscapedEntities = false ): string { |
|
| 223 | - if ( false === $onlyEscapedEntities ) { |
|
| 224 | - return htmlspecialchars_decode( $string, ENT_NOQUOTES ); |
|
| 222 | + public static function htmlspecialchars_decode(string $string, ?bool $onlyEscapedEntities = false): string { |
|
| 223 | + if (false === $onlyEscapedEntities) { |
|
| 224 | + return htmlspecialchars_decode($string, ENT_NOQUOTES); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - return preg_replace_callback( self::$htmlEntityRegex, |
|
| 228 | - function ( $match ) { |
|
| 229 | - return self::htmlspecialchars_decode( $match[ 0 ] ); |
|
| 230 | - }, $string ); |
|
| 227 | + return preg_replace_callback(self::$htmlEntityRegex, |
|
| 228 | + function($match) { |
|
| 229 | + return self::htmlspecialchars_decode($match[0]); |
|
| 230 | + }, $string); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -242,8 +242,8 @@ discard block |
||
| 242 | 242 | * |
| 243 | 243 | * @return bool |
| 244 | 244 | */ |
| 245 | - public static function isADoubleEscapedEntity( string $str ): bool { |
|
| 246 | - return preg_match( self::$htmlEntityRegex, $str ) != 0; |
|
| 245 | + public static function isADoubleEscapedEntity(string $str): bool { |
|
| 246 | + return preg_match(self::$htmlEntityRegex, $str) != 0; |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
@@ -251,8 +251,8 @@ discard block |
||
| 251 | 251 | * |
| 252 | 252 | * @return bool |
| 253 | 253 | */ |
| 254 | - public static function isAValidUuid( $uuid ) { |
|
| 255 | - return preg_match( '/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid ) === 1; |
|
| 254 | + public static function isAValidUuid($uuid) { |
|
| 255 | + return preg_match('/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/', $uuid) === 1; |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | /** |
@@ -261,8 +261,8 @@ discard block |
||
| 261 | 261 | * |
| 262 | 262 | * @return array|false|string[] |
| 263 | 263 | */ |
| 264 | - public static function preg_split( $pattern, $subject ) { |
|
| 265 | - return preg_split( $pattern, $subject, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 264 | + public static function preg_split($pattern, $subject) { |
|
| 265 | + return preg_split($pattern, $subject, -1, PREG_SPLIT_NO_EMPTY); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -270,8 +270,8 @@ discard block |
||
| 270 | 270 | * |
| 271 | 271 | * @return int |
| 272 | 272 | */ |
| 273 | - public static function getTheNumberOfTrailingSpaces( $segment ): int { |
|
| 274 | - return mb_strlen( $segment ) - mb_strlen( rtrim( $segment, ' ' ) ); |
|
| 273 | + public static function getTheNumberOfTrailingSpaces($segment): int { |
|
| 274 | + return mb_strlen($segment) - mb_strlen(rtrim($segment, ' ')); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * @param string|null $xliffProprietary |
| 39 | 39 | * @param LoggerInterface|null $logger |
| 40 | 40 | */ |
| 41 | - public function __construct( int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null ) { |
|
| 41 | + public function __construct(int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null) { |
|
| 42 | 42 | $this->xliffVersion = $xliffVersion; |
| 43 | 43 | $this->logger = $logger; |
| 44 | 44 | $this->xliffProprietary = $xliffProprietary; |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @return string |
| 49 | 49 | */ |
| 50 | 50 | protected function getTuTagName(): string { |
| 51 | - return ( $this->xliffVersion === 1 ) ? 'trans-unit' : 'unit'; |
|
| 51 | + return ($this->xliffVersion === 1) ? 'trans-unit' : 'unit'; |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return array |
| 59 | 59 | */ |
| 60 | - abstract public function parse( DOMDocument $dom, ?array $output = [] ): array; |
|
| 60 | + abstract public function parse(DOMDocument $dom, ?array $output = []): array; |
|
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * Extract trans-unit content from the current node |
@@ -71,17 +71,17 @@ discard block |
||
| 71 | 71 | * @param array|null $contextGroups |
| 72 | 72 | * @param int|null $recursionLevel |
| 73 | 73 | */ |
| 74 | - protected function extractTuFromNode( DOMNode $childNode, array &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [], ?int $recursionLevel = 0 ) { |
|
| 74 | + protected function extractTuFromNode(DOMNode $childNode, array &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [], ?int $recursionLevel = 0) { |
|
| 75 | 75 | |
| 76 | - if ( $childNode->nodeType != XML_ELEMENT_NODE ) { |
|
| 76 | + if ($childNode->nodeType != XML_ELEMENT_NODE) { |
|
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ( $childNode->nodeName === 'group' ) { |
|
| 80 | + if ($childNode->nodeName === 'group') { |
|
| 81 | 81 | |
| 82 | 82 | // add nested context-groups |
| 83 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
| 84 | - if ( $nestedChildNode->nodeName === 'context-group' ) { |
|
| 83 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
| 84 | + if ($nestedChildNode->nodeName === 'context-group') { |
|
| 85 | 85 | $contextGroups[] = $nestedChildNode; |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -89,23 +89,23 @@ discard block |
||
| 89 | 89 | // avoid infinite recursion |
| 90 | 90 | $recursionLevel++; |
| 91 | 91 | |
| 92 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
| 92 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
| 93 | 93 | |
| 94 | 94 | // nested groups |
| 95 | - if ( $nestedChildNode->nodeName === 'group' ) { |
|
| 95 | + if ($nestedChildNode->nodeName === 'group') { |
|
| 96 | 96 | |
| 97 | - if ( $recursionLevel < self::MAX_GROUP_RECURSION_LEVEL ) { |
|
| 98 | - $this->extractTuFromNode( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel ); |
|
| 97 | + if ($recursionLevel < self::MAX_GROUP_RECURSION_LEVEL) { |
|
| 98 | + $this->extractTuFromNode($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups, $recursionLevel); |
|
| 99 | 99 | } else { |
| 100 | - throw new OverflowException( "Maximum tag group nesting level of '" . self::MAX_GROUP_RECURSION_LEVEL . "' reached, aborting!" ); |
|
| 100 | + throw new OverflowException("Maximum tag group nesting level of '" . self::MAX_GROUP_RECURSION_LEVEL . "' reached, aborting!"); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - } elseif ( $nestedChildNode->nodeName === $this->getTuTagName() ) { |
|
| 104 | - $this->extractTransUnit( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
| 103 | + } elseif ($nestedChildNode->nodeName === $this->getTuTagName()) { |
|
| 104 | + $this->extractTransUnit($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | - } elseif ( $childNode->nodeName === $this->getTuTagName() ) { |
|
| 108 | - $this->extractTransUnit( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups ); |
|
| 107 | + } elseif ($childNode->nodeName === $this->getTuTagName()) { |
|
| 108 | + $this->extractTransUnit($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j, $contextGroups); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @return mixed |
| 124 | 124 | */ |
| 125 | - abstract protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ); |
|
| 125 | + abstract protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []); |
|
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | 128 | * @param DOMDocument $dom |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return array |
| 132 | 132 | */ |
| 133 | - protected function extractContent( DOMDocument $dom, DOMNode $node ): array { |
|
| 133 | + protected function extractContent(DOMDocument $dom, DOMNode $node): array { |
|
| 134 | 134 | return [ |
| 135 | - 'raw-content' => $this->extractTagContent( $dom, $node ), |
|
| 136 | - 'attr' => $this->extractTagAttributes( $node ) |
|
| 135 | + 'raw-content' => $this->extractTagContent($dom, $node), |
|
| 136 | + 'attr' => $this->extractTagAttributes($node) |
|
| 137 | 137 | ]; |
| 138 | 138 | } |
| 139 | 139 | |
@@ -150,12 +150,12 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return array |
| 152 | 152 | */ |
| 153 | - protected function extractTagAttributes( DOMNode $element ): array { |
|
| 153 | + protected function extractTagAttributes(DOMNode $element): array { |
|
| 154 | 154 | $tagAttributes = []; |
| 155 | 155 | |
| 156 | - if ( $element->hasAttributes() ) { |
|
| 157 | - foreach ( $element->attributes as $attr ) { |
|
| 158 | - $tagAttributes[ $attr->nodeName ] = $attr->nodeValue; |
|
| 156 | + if ($element->hasAttributes()) { |
|
| 157 | + foreach ($element->attributes as $attr) { |
|
| 158 | + $tagAttributes[$attr->nodeName] = $attr->nodeValue; |
|
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | |
@@ -170,17 +170,17 @@ discard block |
||
| 170 | 170 | * |
| 171 | 171 | * @return string |
| 172 | 172 | */ |
| 173 | - protected function extractTagContent( DOMDocument $dom, DOMNode $element ): string { |
|
| 173 | + protected function extractTagContent(DOMDocument $dom, DOMNode $element): string { |
|
| 174 | 174 | $childNodes = $element->hasChildNodes(); |
| 175 | 175 | $extractedContent = ''; |
| 176 | 176 | |
| 177 | - if ( !empty( $childNodes ) ) { |
|
| 178 | - foreach ( $element->childNodes as $node ) { |
|
| 179 | - $extractedContent .= Emoji::toEntity( Strings::fixNonWellFormedXml( $dom->saveXML( $node ) ) ); |
|
| 177 | + if (!empty($childNodes)) { |
|
| 178 | + foreach ($element->childNodes as $node) { |
|
| 179 | + $extractedContent .= Emoji::toEntity(Strings::fixNonWellFormedXml($dom->saveXML($node))); |
|
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - return str_replace( Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent ); |
|
| 183 | + return str_replace(Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /** |
@@ -191,45 +191,45 @@ discard block |
||
| 191 | 191 | * |
| 192 | 192 | * @return array |
| 193 | 193 | */ |
| 194 | - protected function extractContentWithMarksAndExtTags( DOMDocument $dom, DOMElement $childNode ): array { |
|
| 194 | + protected function extractContentWithMarksAndExtTags(DOMDocument $dom, DOMElement $childNode): array { |
|
| 195 | 195 | $source = []; |
| 196 | 196 | |
| 197 | 197 | // example: |
| 198 | 198 | // <g id="1"><mrk mid="0" mtype="seg">An English string with g tags</mrk></g> |
| 199 | - $raw = $this->extractTagContent( $dom, $childNode ); |
|
| 199 | + $raw = $this->extractTagContent($dom, $childNode); |
|
| 200 | 200 | |
| 201 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
| 201 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
| 202 | 202 | |
| 203 | 203 | $mi = 0; |
| 204 | - while ( isset( $markers[ $mi + 1 ] ) ) { |
|
| 205 | - unset( $mid ); |
|
| 204 | + while (isset($markers[$mi + 1])) { |
|
| 205 | + unset($mid); |
|
| 206 | 206 | |
| 207 | - preg_match( '|mid\s?=\s?["\'](.*?)["\']|si', $markers[ $mi + 1 ], $mid ); |
|
| 207 | + preg_match('|mid\s?=\s?["\'](.*?)["\']|si', $markers[$mi + 1], $mid); |
|
| 208 | 208 | |
| 209 | 209 | // if it's a Trados file the trailing spaces after </mrk> are meaningful |
| 210 | 210 | // so we add them to |
| 211 | 211 | $trailingSpaces = ''; |
| 212 | - if ( $this->xliffProprietary === 'trados' ) { |
|
| 213 | - preg_match_all( '/<\/mrk>[\s]+/iu', $markers[ $mi + 1 ], $trailingSpacesMatches ); |
|
| 212 | + if ($this->xliffProprietary === 'trados') { |
|
| 213 | + preg_match_all('/<\/mrk>[\s]+/iu', $markers[$mi + 1], $trailingSpacesMatches); |
|
| 214 | 214 | |
| 215 | - if ( isset( $trailingSpacesMatches[ 0 ] ) && count( $trailingSpacesMatches[ 0 ] ) > 0 ) { |
|
| 216 | - foreach ( $trailingSpacesMatches[ 0 ] as $match ) { |
|
| 217 | - $trailingSpaces = str_replace( '</mrk>', '', $match ); |
|
| 215 | + if (isset($trailingSpacesMatches[0]) && count($trailingSpacesMatches[0]) > 0) { |
|
| 216 | + foreach ($trailingSpacesMatches[0] as $match) { |
|
| 217 | + $trailingSpaces = str_replace('</mrk>', '', $match); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | //re-build the mrk tag after the split |
| 223 | - $originalMark = trim( '<mrk ' . $markers[ $mi + 1 ] ); |
|
| 223 | + $originalMark = trim('<mrk ' . $markers[$mi + 1]); |
|
| 224 | 224 | |
| 225 | - $mark_string = preg_replace( '#^<mrk\s[^>]+>(.*)#', '$1', $originalMark ); // at this point we have: ---> 'Test </mrk> </g>>' |
|
| 226 | - $mark_content = preg_split( '#</mrk>#si', $mark_string ); |
|
| 225 | + $mark_string = preg_replace('#^<mrk\s[^>]+>(.*)#', '$1', $originalMark); // at this point we have: ---> 'Test </mrk> </g>>' |
|
| 226 | + $mark_content = preg_split('#</mrk>#si', $mark_string); |
|
| 227 | 227 | |
| 228 | 228 | $sourceArray = [ |
| 229 | - 'mid' => ( isset( $mid[ 1 ] ) ) ? $mid[ 1 ] : $mi, |
|
| 230 | - 'ext-prec-tags' => ( $mi == 0 ? $markers[ 0 ] : "" ), |
|
| 231 | - 'raw-content' => ( isset( $mark_content[ 0 ] ) ) ? $mark_content[ 0 ] . $trailingSpaces : '', |
|
| 232 | - 'ext-succ-tags' => ( isset( $mark_content[ 1 ] ) ) ? $mark_content[ 1 ] : '', |
|
| 229 | + 'mid' => (isset($mid[1])) ? $mid[1] : $mi, |
|
| 230 | + 'ext-prec-tags' => ($mi == 0 ? $markers[0] : ""), |
|
| 231 | + 'raw-content' => (isset($mark_content[0])) ? $mark_content[0] . $trailingSpaces : '', |
|
| 232 | + 'ext-succ-tags' => (isset($mark_content[1])) ? $mark_content[1] : '', |
|
| 233 | 233 | ]; |
| 234 | 234 | |
| 235 | 235 | $source[] = $sourceArray; |
@@ -245,12 +245,12 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @return array |
| 247 | 247 | */ |
| 248 | - protected function getDataRefMap( array $originalData ): array { |
|
| 248 | + protected function getDataRefMap(array $originalData): array { |
|
| 249 | 249 | // dataRef map |
| 250 | 250 | $dataRefMap = []; |
| 251 | - foreach ( $originalData as $datum ) { |
|
| 252 | - if ( isset( $datum[ 'attr' ][ 'id' ] ) ) { |
|
| 253 | - $dataRefMap[ $datum[ 'attr' ][ 'id' ] ] = $datum[ 'raw-content' ]; |
|
| 251 | + foreach ($originalData as $datum) { |
|
| 252 | + if (isset($datum['attr']['id'])) { |
|
| 253 | + $dataRefMap[$datum['attr']['id']] = $datum['raw-content']; |
|
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | |
@@ -262,10 +262,10 @@ discard block |
||
| 262 | 262 | * |
| 263 | 263 | * @return bool |
| 264 | 264 | */ |
| 265 | - protected function stringContainsMarks( $raw ): bool { |
|
| 266 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
| 265 | + protected function stringContainsMarks($raw): bool { |
|
| 266 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
| 267 | 267 | |
| 268 | - return isset( $markers[ 1 ] ); |
|
| 268 | + return isset($markers[1]); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | * @return array |
| 276 | 276 | * @throws Exception |
| 277 | 277 | */ |
| 278 | - protected function JSONOrRawContentArray( $noteValue, ?bool $escapeStrings = true ): array { |
|
| 278 | + protected function JSONOrRawContentArray($noteValue, ?bool $escapeStrings = true): array { |
|
| 279 | 279 | // |
| 280 | 280 | // convert double escaped entites |
| 281 | 281 | // |
@@ -285,17 +285,17 @@ discard block |
||
| 285 | 285 | // &amp; ---> & |
| 286 | 286 | // &apos ---> ' |
| 287 | 287 | // |
| 288 | - if ( Strings::isADoubleEscapedEntity( $noteValue ) ) { |
|
| 289 | - $noteValue = Strings::htmlspecialchars_decode( $noteValue, true ); |
|
| 288 | + if (Strings::isADoubleEscapedEntity($noteValue)) { |
|
| 289 | + $noteValue = Strings::htmlspecialchars_decode($noteValue, true); |
|
| 290 | 290 | } else { |
| 291 | 291 | // for non escaped entities $escapeStrings is always true for security reasons |
| 292 | 292 | $escapeStrings = true; |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - if ( Strings::isJSON( $noteValue ) ) { |
|
| 296 | - return [ 'json' => Strings::cleanCDATA( $noteValue ) ]; |
|
| 295 | + if (Strings::isJSON($noteValue)) { |
|
| 296 | + return ['json' => Strings::cleanCDATA($noteValue)]; |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - return [ 'raw-content' => Strings::fixNonWellFormedXml( $noteValue, $escapeStrings ) ]; |
|
| 299 | + return ['raw-content' => Strings::fixNonWellFormedXml($noteValue, $escapeStrings)]; |
|
| 300 | 300 | } |
| 301 | 301 | } |
@@ -16,33 +16,33 @@ discard block |
||
| 16 | 16 | * @inheritDoc |
| 17 | 17 | * @throws Exception |
| 18 | 18 | */ |
| 19 | - public function parse( DOMDocument $dom, ?array $output = [] ): array { |
|
| 19 | + public function parse(DOMDocument $dom, ?array $output = []): array { |
|
| 20 | 20 | $i = 1; |
| 21 | 21 | /** @var DOMElement $file */ |
| 22 | - foreach ( $dom->getElementsByTagName( 'file' ) as $file ) { |
|
| 22 | + foreach ($dom->getElementsByTagName('file') as $file) { |
|
| 23 | 23 | |
| 24 | 24 | // metadata |
| 25 | - $output[ 'files' ][ $i ][ 'attr' ] = $this->extractMetadata( $file ); |
|
| 25 | + $output['files'][$i]['attr'] = $this->extractMetadata($file); |
|
| 26 | 26 | |
| 27 | 27 | // reference |
| 28 | - if ( !empty( $this->extractReference( $file ) ) ) { |
|
| 29 | - $output[ 'files' ][ $i ][ 'reference' ] = $this->extractReference( $file ); |
|
| 28 | + if (!empty($this->extractReference($file))) { |
|
| 29 | + $output['files'][$i]['reference'] = $this->extractReference($file); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | // trans-units |
| 33 | 33 | $transUnitIdArrayForUniquenessCheck = []; |
| 34 | 34 | $j = 1; |
| 35 | - foreach ( $file->childNodes as $body ) { |
|
| 36 | - if ( $body->nodeName === 'body' ) { |
|
| 37 | - foreach ( $body->childNodes as $childNode ) { |
|
| 38 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 35 | + foreach ($file->childNodes as $body) { |
|
| 36 | + if ($body->nodeName === 'body') { |
|
| 37 | + foreach ($body->childNodes as $childNode) { |
|
| 38 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | // trans-unit re-count check |
| 42 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
| 43 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
| 44 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
| 45 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
| 42 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
| 43 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
| 44 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
| 45 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $i++; |
@@ -58,50 +58,50 @@ discard block |
||
| 58 | 58 | * |
| 59 | 59 | * @return array |
| 60 | 60 | */ |
| 61 | - private function extractMetadata( DOMElement $file ): array { |
|
| 61 | + private function extractMetadata(DOMElement $file): array { |
|
| 62 | 62 | $metadata = []; |
| 63 | 63 | $customAttr = []; |
| 64 | 64 | |
| 65 | 65 | /** @var DOMAttr $attribute */ |
| 66 | - foreach ( $file->attributes as $attribute ) { |
|
| 67 | - switch ( $attribute->localName ) { |
|
| 66 | + foreach ($file->attributes as $attribute) { |
|
| 67 | + switch ($attribute->localName) { |
|
| 68 | 68 | // original |
| 69 | 69 | case 'original': |
| 70 | - $metadata[ 'original' ] = $attribute->value; |
|
| 70 | + $metadata['original'] = $attribute->value; |
|
| 71 | 71 | break; |
| 72 | 72 | |
| 73 | 73 | // source-language |
| 74 | 74 | case 'source-language': |
| 75 | - $metadata[ 'source-language' ] = $attribute->value; |
|
| 75 | + $metadata['source-language'] = $attribute->value; |
|
| 76 | 76 | break; |
| 77 | 77 | |
| 78 | 78 | // data-type |
| 79 | 79 | case 'datatype': |
| 80 | - $metadata[ 'data-type' ] = $attribute->value; |
|
| 80 | + $metadata['data-type'] = $attribute->value; |
|
| 81 | 81 | break; |
| 82 | 82 | |
| 83 | 83 | // target-language |
| 84 | 84 | case 'target-language': |
| 85 | - $metadata[ 'target-language' ] = $attribute->value; |
|
| 85 | + $metadata['target-language'] = $attribute->value; |
|
| 86 | 86 | break; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // Custom MateCat x-Attribute |
| 90 | - preg_match( '|x-(.*?)|si', $attribute->localName, $temp ); |
|
| 91 | - if ( isset( $temp[ 1 ] ) ) { |
|
| 92 | - $customAttr[ $attribute->localName ] = $attribute->value; |
|
| 90 | + preg_match('|x-(.*?)|si', $attribute->localName, $temp); |
|
| 91 | + if (isset($temp[1])) { |
|
| 92 | + $customAttr[$attribute->localName] = $attribute->value; |
|
| 93 | 93 | } |
| 94 | - unset( $temp ); |
|
| 94 | + unset($temp); |
|
| 95 | 95 | |
| 96 | 96 | // Custom MateCat namespace Attribute mtc: |
| 97 | - preg_match( '|mtc:(.*?)|si', $attribute->nodeName, $temp ); |
|
| 98 | - if ( isset( $temp[ 1 ] ) ) { |
|
| 99 | - $customAttr[ $attribute->nodeName ] = $attribute->value; |
|
| 97 | + preg_match('|mtc:(.*?)|si', $attribute->nodeName, $temp); |
|
| 98 | + if (isset($temp[1])) { |
|
| 99 | + $customAttr[$attribute->nodeName] = $attribute->value; |
|
| 100 | 100 | } |
| 101 | - unset( $temp ); |
|
| 101 | + unset($temp); |
|
| 102 | 102 | |
| 103 | - if ( !empty( $customAttr ) ) { |
|
| 104 | - $metadata[ 'custom' ] = $customAttr; |
|
| 103 | + if (!empty($customAttr)) { |
|
| 104 | + $metadata['custom'] = $customAttr; |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | |
@@ -113,16 +113,16 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return array |
| 115 | 115 | */ |
| 116 | - private function extractReference( DOMElement $file ): array { |
|
| 116 | + private function extractReference(DOMElement $file): array { |
|
| 117 | 117 | $reference = []; |
| 118 | 118 | |
| 119 | 119 | $order = 0; |
| 120 | - foreach ( $file->getElementsByTagName( 'reference' ) as $ref ) { |
|
| 120 | + foreach ($file->getElementsByTagName('reference') as $ref) { |
|
| 121 | 121 | /** @var DOMNode $childNode */ |
| 122 | - foreach ( $ref->childNodes as $childNode ) { |
|
| 123 | - if ( $childNode->nodeName === 'internal-file' ) { |
|
| 124 | - $reference[ $order ][ 'form-type' ] = $childNode->attributes->getNamedItem( 'form' )->nodeValue; |
|
| 125 | - $reference[ $order ][ 'base64' ] = trim( $childNode->nodeValue ); |
|
| 122 | + foreach ($ref->childNodes as $childNode) { |
|
| 123 | + if ($childNode->nodeName === 'internal-file') { |
|
| 124 | + $reference[$order]['form-type'] = $childNode->attributes->getNamedItem('form')->nodeValue; |
|
| 125 | + $reference[$order]['base64'] = trim($childNode->nodeValue); |
|
| 126 | 126 | $order++; |
| 127 | 127 | } |
| 128 | 128 | } |
@@ -144,60 +144,60 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @throws Exception |
| 146 | 146 | */ |
| 147 | - protected function extractTransUnit( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = [] ) { |
|
| 147 | + protected function extractTransUnit(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck, DomDocument $dom, array &$output, int &$i, int &$j, ?array $contextGroups = []) { |
|
| 148 | 148 | // metadata |
| 149 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
| 149 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
| 150 | 150 | |
| 151 | 151 | // notes |
| 152 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $dom, $transUnit ); |
|
| 152 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($dom, $transUnit); |
|
| 153 | 153 | |
| 154 | 154 | // content |
| 155 | 155 | /** @var DOMElement $childNode */ |
| 156 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
| 156 | + foreach ($transUnit->childNodes as $childNode) { |
|
| 157 | 157 | // source |
| 158 | - if ( $childNode->nodeName === 'source' ) { |
|
| 159 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $this->extractContent( $dom, $childNode ); |
|
| 158 | + if ($childNode->nodeName === 'source') { |
|
| 159 | + $output['files'][$i]['trans-units'][$j]['source'] = $this->extractContent($dom, $childNode); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // seg-source |
| 163 | - if ( $childNode->nodeName === 'seg-source' ) { |
|
| 164 | - $rawSegment = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ][ 'raw-content' ]; |
|
| 165 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $rawSegment ); |
|
| 163 | + if ($childNode->nodeName === 'seg-source') { |
|
| 164 | + $rawSegment = $output['files'][$i]['trans-units'][$j]['source']['raw-content']; |
|
| 165 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $this->extractContentWithMarksAndExtTags($dom, $childNode, $rawSegment); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | // target |
| 169 | - if ( $childNode->nodeName === 'target' ) { |
|
| 170 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $this->extractContent( $dom, $childNode ); |
|
| 169 | + if ($childNode->nodeName === 'target') { |
|
| 170 | + $output['files'][$i]['trans-units'][$j]['target'] = $this->extractContent($dom, $childNode); |
|
| 171 | 171 | |
| 172 | 172 | // seg-target |
| 173 | - $targetRawContent = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ][ 'raw-content' ]; |
|
| 174 | - $segSource = @$output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ]; |
|
| 175 | - if ( !empty( $targetRawContent ) && isset( $segSource ) && count( $segSource ) > 0 ) { |
|
| 176 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $this->extractContentWithMarksAndExtTags( $dom, $childNode ); |
|
| 177 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ][ 0 ][ 'attr' ] = $this->extractTagAttributes( $childNode ); |
|
| 173 | + $targetRawContent = @$output['files'][$i]['trans-units'][$j]['target']['raw-content']; |
|
| 174 | + $segSource = @$output['files'][$i]['trans-units'][$j]['seg-source']; |
|
| 175 | + if (!empty($targetRawContent) && isset($segSource) && count($segSource) > 0) { |
|
| 176 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $this->extractContentWithMarksAndExtTags($dom, $childNode); |
|
| 177 | + $output['files'][$i]['trans-units'][$j]['seg-target'][0]['attr'] = $this->extractTagAttributes($childNode); |
|
| 178 | 178 | } |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // locked |
| 182 | - if ( $childNode->nodeName === 'sdl:seg' ) { |
|
| 183 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'locked' ] = $this->extractLocked( $childNode ); |
|
| 182 | + if ($childNode->nodeName === 'sdl:seg') { |
|
| 183 | + $output['files'][$i]['trans-units'][$j]['locked'] = $this->extractLocked($childNode); |
|
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | // context-group |
| 188 | - if ( !empty( $contextGroups ) ) { |
|
| 189 | - foreach ( $contextGroups as $contextGroup ) { |
|
| 190 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
| 188 | + if (!empty($contextGroups)) { |
|
| 189 | + foreach ($contextGroups as $contextGroup) { |
|
| 190 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | - foreach ( $transUnit->getElementsByTagName( 'context-group' ) as $contextGroup ) { |
|
| 195 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'context-group' ][] = $this->extractTransUnitContextGroup( $dom, $contextGroup ); |
|
| 194 | + foreach ($transUnit->getElementsByTagName('context-group') as $contextGroup) { |
|
| 195 | + $output['files'][$i]['trans-units'][$j]['context-group'][] = $this->extractTransUnitContextGroup($dom, $contextGroup); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // alt-trans |
| 199 | - foreach ( $transUnit->getElementsByTagName( 'alt-trans' ) as $altTrans ) { |
|
| 200 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'alt-trans' ][] = $this->extractTransUnitAltTrans( $altTrans ); |
|
| 199 | + foreach ($transUnit->getElementsByTagName('alt-trans') as $altTrans) { |
|
| 200 | + $output['files'][$i]['trans-units'][$j]['alt-trans'][] = $this->extractTransUnitAltTrans($altTrans); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | $j++; |
@@ -210,41 +210,41 @@ discard block |
||
| 210 | 210 | * @return array |
| 211 | 211 | * @throws Exception |
| 212 | 212 | */ |
| 213 | - private function extractTransUnitMetadata( DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck ): array { |
|
| 213 | + private function extractTransUnitMetadata(DOMElement $transUnit, array &$transUnitIdArrayForUniquenessCheck): array { |
|
| 214 | 214 | $metadata = []; |
| 215 | 215 | |
| 216 | 216 | // id MUST NOT be null |
| 217 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
| 218 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
| 217 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
| 218 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
| 222 | 222 | * @var DOMAttr $element |
| 223 | 223 | */ |
| 224 | - foreach ( $transUnit->attributes as $element ) { |
|
| 224 | + foreach ($transUnit->attributes as $element) { |
|
| 225 | 225 | |
| 226 | - if ( $element->nodeName === "id" ) { |
|
| 226 | + if ($element->nodeName === "id") { |
|
| 227 | 227 | |
| 228 | 228 | $id = $element->nodeValue; |
| 229 | 229 | |
| 230 | - if ( strlen( $id ) > 100 ) { |
|
| 231 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
| 230 | + if (strlen($id) > 100) { |
|
| 231 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | $transUnitIdArrayForUniquenessCheck[] = $id; |
| 235 | - $metadata[ 'id' ] = $id; |
|
| 235 | + $metadata['id'] = $id; |
|
| 236 | 236 | |
| 237 | - } elseif ( $element->nodeName === "approved" ) { |
|
| 237 | + } elseif ($element->nodeName === "approved") { |
|
| 238 | 238 | // approved as BOOLEAN |
| 239 | 239 | // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved |
| 240 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_VALIDATE_BOOLEAN ); |
|
| 241 | - } elseif ( $element->nodeName === "maxwidth" ) { |
|
| 240 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_VALIDATE_BOOLEAN); |
|
| 241 | + } elseif ($element->nodeName === "maxwidth") { |
|
| 242 | 242 | // we ignore ( but we get ) the attribute size-unit="char" assuming that a restriction is everytime done by character |
| 243 | 243 | // we duplicate the info to allow Xliff V1 and V2 to work the same |
| 244 | - $metadata[ 'sizeRestriction' ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
| 245 | - $metadata[ $element->nodeName ] = filter_var( $element->nodeValue, FILTER_SANITIZE_NUMBER_INT ); |
|
| 244 | + $metadata['sizeRestriction'] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
| 245 | + $metadata[$element->nodeName] = filter_var($element->nodeValue, FILTER_SANITIZE_NUMBER_INT); |
|
| 246 | 246 | } else { |
| 247 | - $metadata[ $element->nodeName ] = $element->nodeValue; |
|
| 247 | + $metadata[$element->nodeName] = $element->nodeValue; |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | } |
@@ -259,19 +259,19 @@ discard block |
||
| 259 | 259 | * @return array |
| 260 | 260 | * @throws Exception |
| 261 | 261 | */ |
| 262 | - private function extractTransUnitNotes( DOMDocument $dom, DOMElement $transUnit ): array { |
|
| 262 | + private function extractTransUnitNotes(DOMDocument $dom, DOMElement $transUnit): array { |
|
| 263 | 263 | $notes = []; |
| 264 | - foreach ( $transUnit->getElementsByTagName( 'note' ) as $note ) { |
|
| 264 | + foreach ($transUnit->getElementsByTagName('note') as $note) { |
|
| 265 | 265 | |
| 266 | - $noteValue = $this->extractTagContent( $dom, $note ); |
|
| 266 | + $noteValue = $this->extractTagContent($dom, $note); |
|
| 267 | 267 | |
| 268 | - if ( '' !== $noteValue ) { |
|
| 268 | + if ('' !== $noteValue) { |
|
| 269 | 269 | |
| 270 | - $extractedNote = $this->JSONOrRawContentArray( $noteValue ); |
|
| 270 | + $extractedNote = $this->JSONOrRawContentArray($noteValue); |
|
| 271 | 271 | |
| 272 | 272 | // extract all the attributes |
| 273 | - foreach ( $note->attributes as $attribute ) { |
|
| 274 | - $extractedNote[ $attribute->name ] = $attribute->value; |
|
| 273 | + foreach ($note->attributes as $attribute) { |
|
| 274 | + $extractedNote[$attribute->name] = $attribute->value; |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | $notes[] = $extractedNote; |
@@ -287,14 +287,14 @@ discard block |
||
| 287 | 287 | * |
| 288 | 288 | * @return array |
| 289 | 289 | */ |
| 290 | - private function extractTransUnitContextGroup( DOMDocument $dom, DOMElement $contextGroup ): array { |
|
| 290 | + private function extractTransUnitContextGroup(DOMDocument $dom, DOMElement $contextGroup): array { |
|
| 291 | 291 | $cg = []; |
| 292 | - $cg[ 'attr' ] = $this->extractTagAttributes( $contextGroup ); |
|
| 292 | + $cg['attr'] = $this->extractTagAttributes($contextGroup); |
|
| 293 | 293 | |
| 294 | 294 | /** @var DOMNode $context */ |
| 295 | - foreach ( $contextGroup->childNodes as $context ) { |
|
| 296 | - if ( $context->nodeName === 'context' ) { |
|
| 297 | - $cg[ 'contexts' ][] = $this->extractContent( $dom, $context ); |
|
| 295 | + foreach ($contextGroup->childNodes as $context) { |
|
| 296 | + if ($context->nodeName === 'context') { |
|
| 297 | + $cg['contexts'][] = $this->extractContent($dom, $context); |
|
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | |
@@ -306,16 +306,16 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @return array |
| 308 | 308 | */ |
| 309 | - private function extractTransUnitAltTrans( DOMElement $altTrans ) { |
|
| 309 | + private function extractTransUnitAltTrans(DOMElement $altTrans) { |
|
| 310 | 310 | $at = []; |
| 311 | - $at[ 'attr' ] = $this->extractTagAttributes( $altTrans ); |
|
| 311 | + $at['attr'] = $this->extractTagAttributes($altTrans); |
|
| 312 | 312 | |
| 313 | - if ( $altTrans->getElementsByTagName( 'source' )->length > 0 ) { |
|
| 314 | - $at[ 'source' ] = $altTrans->getElementsByTagName( 'source' )->item( 0 )->nodeValue; |
|
| 313 | + if ($altTrans->getElementsByTagName('source')->length > 0) { |
|
| 314 | + $at['source'] = $altTrans->getElementsByTagName('source')->item(0)->nodeValue; |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | - if ( $altTrans->getElementsByTagName( 'target' ) ) { |
|
| 318 | - $at[ 'target' ] = $altTrans->getElementsByTagName( 'target' )->item( 0 )->nodeValue; |
|
| 317 | + if ($altTrans->getElementsByTagName('target')) { |
|
| 318 | + $at['target'] = $altTrans->getElementsByTagName('target')->item(0)->nodeValue; |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | return $at; |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @return bool |
| 328 | 328 | */ |
| 329 | - private function extractLocked( DOMElement $locked ) { |
|
| 330 | - return null !== $locked->getAttribute( 'locked' ); |
|
| 329 | + private function extractLocked(DOMElement $locked) { |
|
| 330 | + return null !== $locked->getAttribute('locked'); |
|
| 331 | 331 | } |
| 332 | 332 | } |
@@ -12,10 +12,10 @@ |
||
| 12 | 12 | * |
| 13 | 13 | * @return AbstractXliffParser |
| 14 | 14 | */ |
| 15 | - public static function getInstance( int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null ): AbstractXliffParser { |
|
| 15 | + public static function getInstance(int $xliffVersion, ?string $xliffProprietary = null, LoggerInterface $logger = null): AbstractXliffParser { |
|
| 16 | 16 | $parserClass = 'Matecat\\XliffParser\\XliffParser\\XliffParserV' . $xliffVersion; |
| 17 | 17 | |
| 18 | 18 | /** @var AbstractXliffParser $parser */ |
| 19 | - return new $parserClass( $xliffVersion, $xliffProprietary, $logger ); |
|
| 19 | + return new $parserClass($xliffVersion, $xliffProprietary, $logger); |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | /** |
| 7 | 7 | * List of allowed Xliff tags |
| 8 | 8 | */ |
| 9 | - public static array $tags = [ 'g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk' ]; |
|
| 9 | + public static array $tags = ['g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk']; |
|
| 10 | 10 | } |
| 11 | 11 | \ No newline at end of file |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @param array|null $tmp |
| 22 | 22 | */ |
| 23 | - public function __construct( ?array $tmp = [] ) { |
|
| 23 | + public function __construct(?array $tmp = []) { |
|
| 24 | 24 | $this->tmp = $tmp; |
| 25 | 25 | $this->steps = []; |
| 26 | 26 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * @param CheckInterface $step |
| 30 | 30 | */ |
| 31 | - public function addCheck( CheckInterface $step ) { |
|
| 31 | + public function addCheck(CheckInterface $step) { |
|
| 32 | 32 | $this->steps[] = $step; |
| 33 | 33 | } |
| 34 | 34 | |
@@ -39,13 +39,13 @@ discard block |
||
| 39 | 39 | $fileType = []; |
| 40 | 40 | |
| 41 | 41 | /** @var CheckInterface $step */ |
| 42 | - foreach ( $this->steps as $step ) { |
|
| 43 | - if ( null !== $step->check( $this->tmp ) ) { |
|
| 44 | - $fileType = $step->check( $this->tmp ); |
|
| 42 | + foreach ($this->steps as $step) { |
|
| 43 | + if (null !== $step->check($this->tmp)) { |
|
| 44 | + $fileType = $step->check($this->tmp); |
|
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - if ( !empty( $fileType ) && $this->isValid( $fileType ) ) { |
|
| 48 | + if (!empty($fileType) && $this->isValid($fileType)) { |
|
| 49 | 49 | return $fileType; |
| 50 | 50 | } |
| 51 | 51 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @return bool |
| 64 | 64 | */ |
| 65 | - private function isValid( $fileType ): bool { |
|
| 65 | + private function isValid($fileType): bool { |
|
| 66 | 66 | $mandatoryKeys = [ |
| 67 | 67 | 'proprietary', |
| 68 | 68 | 'proprietary_name', |
@@ -70,6 +70,6 @@ discard block |
||
| 70 | 70 | 'converter_version', |
| 71 | 71 | ]; |
| 72 | 72 | |
| 73 | - return array_keys( $fileType ) === $mandatoryKeys; |
|
| 73 | + return array_keys($fileType) === $mandatoryKeys; |
|
| 74 | 74 | } |
| 75 | 75 | } |
@@ -8,16 +8,16 @@ |
||
| 8 | 8 | * |
| 9 | 9 | * @return array|null |
| 10 | 10 | */ |
| 11 | - public function check( ?array $tmp = [] ): ?array { |
|
| 11 | + public function check(?array $tmp = []): ?array { |
|
| 12 | 12 | $fileType = []; |
| 13 | 13 | |
| 14 | - if ( isset( $tmp[ 0 ] ) ) { |
|
| 15 | - if ( stripos( $tmp[ 0 ], 'sdl:version' ) !== false ) { |
|
| 14 | + if (isset($tmp[0])) { |
|
| 15 | + if (stripos($tmp[0], 'sdl:version') !== false) { |
|
| 16 | 16 | //little trick, we consider not proprietary Sdlxliff files because we can handle them |
| 17 | - $fileType[ 'proprietary' ] = false; |
|
| 18 | - $fileType[ 'proprietary_name' ] = 'SDL Studio '; |
|
| 19 | - $fileType[ 'proprietary_short_name' ] = 'trados'; |
|
| 20 | - $fileType[ 'converter_version' ] = 'legacy'; |
|
| 17 | + $fileType['proprietary'] = false; |
|
| 18 | + $fileType['proprietary_name'] = 'SDL Studio '; |
|
| 19 | + $fileType['proprietary_short_name'] = 'trados'; |
|
| 20 | + $fileType['converter_version'] = 'legacy'; |
|
| 21 | 21 | |
| 22 | 22 | return $fileType; |
| 23 | 23 | } |
@@ -8,15 +8,15 @@ |
||
| 8 | 8 | * |
| 9 | 9 | * @return array|null |
| 10 | 10 | */ |
| 11 | - public function check( ?array $tmp = [] ): ?array { |
|
| 11 | + public function check(?array $tmp = []): ?array { |
|
| 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,20 +8,20 @@ |
||
| 8 | 8 | * |
| 9 | 9 | * @return array|null |
| 10 | 10 | */ |
| 11 | - public function check( ?array $tmp = [] ): ?array { |
|
| 11 | + public function check(?array $tmp = []): ?array { |
|
| 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; |