@@ -7,5 +7,5 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * List of allowed Xliff tags |
| 9 | 9 | */ |
| 10 | - public static $tags = [ 'g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk' ]; |
|
| 10 | + public static $tags = ['g', 'x', 'bx', 'ex', 'bpt', 'ept', 'ph', 'pc', 'ec', 'sc', 'it', 'mrk']; |
|
| 11 | 11 | } |
| 12 | 12 | \ No newline at end of file |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | self::STATUS_REBUTTED |
| 54 | 54 | ]; |
| 55 | 55 | |
| 56 | - public static function isReviewedStatus( $status ) { |
|
| 57 | - return in_array( $status, TranslationStatus::$REVISION_STATUSES ); |
|
| 56 | + public static function isReviewedStatus($status) { |
|
| 57 | + return in_array($status, TranslationStatus::$REVISION_STATUSES); |
|
| 58 | 58 | } |
| 59 | 59 | } |
@@ -27,65 +27,65 @@ discard block |
||
| 27 | 27 | * @throws InvalidXmlException When parsing of XML with schema or callable produces any errors unrelated to the XML parsing itself |
| 28 | 28 | * @throws RuntimeException When DOM extension is missing |
| 29 | 29 | */ |
| 30 | - public static function parse( $content, $schemaOrCallable = null ) { |
|
| 31 | - if ( !extension_loaded( 'dom' ) ) { |
|
| 32 | - throw new RuntimeException( 'Extension DOM is required.' ); |
|
| 30 | + public static function parse($content, $schemaOrCallable = null) { |
|
| 31 | + if (!extension_loaded('dom')) { |
|
| 32 | + throw new RuntimeException('Extension DOM is required.'); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - $internalErrors = libxml_use_internal_errors( true ); |
|
| 36 | - $disableEntities = libxml_disable_entity_loader( true ); |
|
| 35 | + $internalErrors = libxml_use_internal_errors(true); |
|
| 36 | + $disableEntities = libxml_disable_entity_loader(true); |
|
| 37 | 37 | libxml_clear_errors(); |
| 38 | 38 | |
| 39 | - $dom = new DOMDocument( '1.0', 'UTF-8' ); |
|
| 39 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
| 40 | 40 | $dom->validateOnParse = true; |
| 41 | - if ( !$dom->loadXML( $content, LIBXML_NONET | ( defined( 'LIBXML_COMPACT' ) ? LIBXML_COMPACT : 0 ) ) ) { |
|
| 42 | - libxml_disable_entity_loader( $disableEntities ); |
|
| 41 | + if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { |
|
| 42 | + libxml_disable_entity_loader($disableEntities); |
|
| 43 | 43 | |
| 44 | - throw new XmlParsingException( implode( "\n", static::getXmlErrors( $internalErrors ) ) ); |
|
| 44 | + throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors))); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | $dom->normalizeDocument(); |
| 48 | 48 | |
| 49 | - libxml_use_internal_errors( $internalErrors ); |
|
| 50 | - libxml_disable_entity_loader( $disableEntities ); |
|
| 49 | + libxml_use_internal_errors($internalErrors); |
|
| 50 | + libxml_disable_entity_loader($disableEntities); |
|
| 51 | 51 | |
| 52 | - foreach ( $dom->childNodes as $child ) { |
|
| 53 | - if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) { |
|
| 54 | - throw new XmlParsingException( 'Document types are not allowed.' ); |
|
| 52 | + foreach ($dom->childNodes as $child) { |
|
| 53 | + if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) { |
|
| 54 | + throw new XmlParsingException('Document types are not allowed.'); |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - if ( null !== $schemaOrCallable ) { |
|
| 59 | - $internalErrors = libxml_use_internal_errors( true ); |
|
| 58 | + if (null !== $schemaOrCallable) { |
|
| 59 | + $internalErrors = libxml_use_internal_errors(true); |
|
| 60 | 60 | libxml_clear_errors(); |
| 61 | 61 | |
| 62 | 62 | $e = null; |
| 63 | - if ( is_callable( $schemaOrCallable ) ) { |
|
| 63 | + if (is_callable($schemaOrCallable)) { |
|
| 64 | 64 | try { |
| 65 | - $valid = call_user_func( $schemaOrCallable, $dom, $internalErrors ); |
|
| 66 | - } catch ( Exception $e ) { |
|
| 65 | + $valid = call_user_func($schemaOrCallable, $dom, $internalErrors); |
|
| 66 | + } catch (Exception $e) { |
|
| 67 | 67 | $valid = false; |
| 68 | 68 | } |
| 69 | - } elseif ( !is_array( $schemaOrCallable ) && is_file( (string)$schemaOrCallable ) ) { |
|
| 70 | - $schemaSource = file_get_contents( (string)$schemaOrCallable ); |
|
| 71 | - $valid = @$dom->schemaValidateSource( $schemaSource ); |
|
| 69 | + } elseif (!is_array($schemaOrCallable) && is_file((string)$schemaOrCallable)) { |
|
| 70 | + $schemaSource = file_get_contents((string)$schemaOrCallable); |
|
| 71 | + $valid = @$dom->schemaValidateSource($schemaSource); |
|
| 72 | 72 | } else { |
| 73 | - libxml_use_internal_errors( $internalErrors ); |
|
| 73 | + libxml_use_internal_errors($internalErrors); |
|
| 74 | 74 | |
| 75 | - throw new XmlParsingException( 'The schemaOrCallable argument has to be a valid path to XSD file or callable.' ); |
|
| 75 | + throw new XmlParsingException('The schemaOrCallable argument has to be a valid path to XSD file or callable.'); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if ( !$valid ) { |
|
| 79 | - $messages = static::getXmlErrors( $internalErrors ); |
|
| 80 | - if ( empty( $messages ) ) { |
|
| 81 | - throw new InvalidXmlException( 'The XML is not valid.', 0, $e ); |
|
| 78 | + if (!$valid) { |
|
| 79 | + $messages = static::getXmlErrors($internalErrors); |
|
| 80 | + if (empty($messages)) { |
|
| 81 | + throw new InvalidXmlException('The XML is not valid.', 0, $e); |
|
| 82 | 82 | } |
| 83 | - throw new XmlParsingException( implode( "\n", $messages ), 0, $e ); |
|
| 83 | + throw new XmlParsingException(implode("\n", $messages), 0, $e); |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | libxml_clear_errors(); |
| 88 | - libxml_use_internal_errors( $internalErrors ); |
|
| 88 | + libxml_use_internal_errors($internalErrors); |
|
| 89 | 89 | |
| 90 | 90 | return $dom; |
| 91 | 91 | } |
@@ -95,14 +95,14 @@ discard block |
||
| 95 | 95 | * |
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | - private static function getXmlErrors( $internalErrors ) { |
|
| 98 | + private static function getXmlErrors($internalErrors) { |
|
| 99 | 99 | $errors = []; |
| 100 | - foreach ( libxml_get_errors() as $error ) { |
|
| 100 | + foreach (libxml_get_errors() as $error) { |
|
| 101 | 101 | $errors[] = sprintf( |
| 102 | 102 | '[%s %s] %s (in %s - line %d, column %d)', |
| 103 | 103 | LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', |
| 104 | 104 | $error->code, |
| 105 | - trim( $error->message ), |
|
| 105 | + trim($error->message), |
|
| 106 | 106 | $error->file ?: 'n/a', |
| 107 | 107 | $error->line, |
| 108 | 108 | $error->column |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | libxml_clear_errors(); |
| 113 | - libxml_use_internal_errors( $internalErrors ); |
|
| 113 | + libxml_use_internal_errors($internalErrors); |
|
| 114 | 114 | |
| 115 | 115 | return $errors; |
| 116 | 116 | } |
@@ -8,28 +8,28 @@ discard block |
||
| 8 | 8 | abstract class AbstractXliffReplacer { |
| 9 | 9 | protected $originalFP; |
| 10 | 10 | |
| 11 | - protected $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
| 12 | - protected $inTU = false; // flag to check whether we are in a <trans-unit> |
|
| 13 | - protected $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
| 14 | - protected $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
| 15 | - protected $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
| 16 | - protected $segmentPositionInTu = -1; // the current position of segment in the current <unit> (forXliff v 2.*) |
|
| 17 | - |
|
| 18 | - protected $CDATABuffer = ""; // buffer for special tag |
|
| 19 | - protected $bufferIsActive = false; // buffer for special tag |
|
| 20 | - |
|
| 21 | - protected $offset = 0; // offset for SAX pointer |
|
| 22 | - protected $outputFP; // output stream pointer |
|
| 23 | - protected $currentBuffer; // the current piece of text it's been parsed |
|
| 24 | - protected $len; // length of the currentBuffer |
|
| 25 | - protected $segments; // array of translations |
|
| 11 | + protected $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*) |
|
| 12 | + protected $inTU = false; // flag to check whether we are in a <trans-unit> |
|
| 13 | + protected $inTarget = false; // flag to check whether we are in a <target>, to ignore everything |
|
| 14 | + protected $isEmpty = false; // flag to check whether we are in an empty tag (<tag/>) |
|
| 15 | + protected $targetWasWritten = false; // flag to check is <target> was written in the current unit |
|
| 16 | + protected $segmentPositionInTu = -1; // the current position of segment in the current <unit> (forXliff v 2.*) |
|
| 17 | + |
|
| 18 | + protected $CDATABuffer = ""; // buffer for special tag |
|
| 19 | + protected $bufferIsActive = false; // buffer for special tag |
|
| 20 | + |
|
| 21 | + protected $offset = 0; // offset for SAX pointer |
|
| 22 | + protected $outputFP; // output stream pointer |
|
| 23 | + protected $currentBuffer; // the current piece of text it's been parsed |
|
| 24 | + protected $len; // length of the currentBuffer |
|
| 25 | + protected $segments; // array of translations |
|
| 26 | 26 | protected $lastTransUnit = []; |
| 27 | - protected $currentTransUnitId; // id of current <trans-unit> |
|
| 27 | + protected $currentTransUnitId; // id of current <trans-unit> |
|
| 28 | 28 | protected $currentTransUnitTranslate; // 'translate' attribute of current <trans-unit> |
| 29 | - protected $currentSegmentArray = []; // id of current <segment> (forXliff v 2.*) |
|
| 30 | - protected $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
| 31 | - protected $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
| 32 | - protected $sourceAttributes = []; // current <source> attributes (needed when handling xliff files without <target>) |
|
| 29 | + protected $currentSegmentArray = []; // id of current <segment> (forXliff v 2.*) |
|
| 30 | + protected $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
|
| 31 | + protected $hasWrittenCounts = false; // check if <unit> already wrote segment counts (forXliff v 2.*) |
|
| 32 | + protected $sourceAttributes = []; // current <source> attributes (needed when handling xliff files without <target>) |
|
| 33 | 33 | |
| 34 | 34 | protected $targetLang; |
| 35 | 35 | |
@@ -76,8 +76,8 @@ 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 | 82 | $this->setTuTagName(); |
| 83 | 83 | $this->segments = $segments; |
@@ -95,19 +95,19 @@ discard block |
||
| 95 | 95 | return "§" . |
| 96 | 96 | substr( |
| 97 | 97 | str_replace( |
| 98 | - [ '+', '/' ], |
|
| 98 | + ['+', '/'], |
|
| 99 | 99 | '', |
| 100 | - base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) ) |
|
| 100 | + base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong)) |
|
| 101 | 101 | ), |
| 102 | 102 | 0, |
| 103 | 103 | 4 |
| 104 | 104 | ); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - private function createOutputFileIfDoesNotExist( $outputFilePath ) { |
|
| 107 | + private function createOutputFileIfDoesNotExist($outputFilePath) { |
|
| 108 | 108 | // create output file |
| 109 | - if ( !file_exists( $outputFilePath ) ) { |
|
| 110 | - touch( $outputFilePath ); |
|
| 109 | + if (!file_exists($outputFilePath)) { |
|
| 110 | + touch($outputFilePath); |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
@@ -115,13 +115,13 @@ discard block |
||
| 115 | 115 | * @param $originalXliffPath |
| 116 | 116 | * @param $outputFilePath |
| 117 | 117 | */ |
| 118 | - private function setFileDescriptors( $originalXliffPath, $outputFilePath ) { |
|
| 119 | - $this->outputFP = fopen( $outputFilePath, 'w+' ); |
|
| 118 | + private function setFileDescriptors($originalXliffPath, $outputFilePath) { |
|
| 119 | + $this->outputFP = fopen($outputFilePath, 'w+'); |
|
| 120 | 120 | |
| 121 | 121 | $streamArgs = null; |
| 122 | 122 | |
| 123 | - if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) { |
|
| 124 | - throw new RuntimeException( "could not open XML input" ); |
|
| 123 | + if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) { |
|
| 124 | + throw new RuntimeException("could not open XML input"); |
|
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * <trans-unit> (xliff v1.*) || <unit> (xliff v2.*) |
| 131 | 131 | */ |
| 132 | 132 | private function setTuTagName() { |
| 133 | - $this->tuTagName = ( $this->xliffVersion === 2 ) ? 'unit' : 'trans-unit'; |
|
| 133 | + $this->tuTagName = ($this->xliffVersion === 2) ? 'unit' : 'trans-unit'; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -139,8 +139,8 @@ discard block |
||
| 139 | 139 | public function __destruct() { |
| 140 | 140 | //this stream can be closed outside the class |
| 141 | 141 | //to permit multiple concurrent downloads, so suppress warnings |
| 142 | - @fclose( $this->originalFP ); |
|
| 143 | - fclose( $this->outputFP ); |
|
| 142 | + @fclose($this->originalFP); |
|
| 143 | + fclose($this->outputFP); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | abstract public function replaceTranslation(); |
@@ -151,11 +151,11 @@ discard block |
||
| 151 | 151 | * @return resource |
| 152 | 152 | */ |
| 153 | 153 | protected function initSaxParser() { |
| 154 | - $xmlSaxParser = xml_parser_create( 'UTF-8' ); |
|
| 155 | - xml_set_object( $xmlSaxParser, $this ); |
|
| 156 | - xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false ); |
|
| 157 | - xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' ); |
|
| 158 | - xml_set_character_data_handler( $xmlSaxParser, 'characterData' ); |
|
| 154 | + $xmlSaxParser = xml_parser_create('UTF-8'); |
|
| 155 | + xml_set_object($xmlSaxParser, $this); |
|
| 156 | + xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false); |
|
| 157 | + xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose'); |
|
| 158 | + xml_set_character_data_handler($xmlSaxParser, 'characterData'); |
|
| 159 | 159 | |
| 160 | 160 | return $xmlSaxParser; |
| 161 | 161 | } |
@@ -163,8 +163,8 @@ discard block |
||
| 163 | 163 | /** |
| 164 | 164 | * @param resource $xmlSaxParser |
| 165 | 165 | */ |
| 166 | - protected function closeSaxParser( $xmlSaxParser ) { |
|
| 167 | - xml_parser_free( $xmlSaxParser ); |
|
| 166 | + protected function closeSaxParser($xmlSaxParser) { |
|
| 167 | + xml_parser_free($xmlSaxParser); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return mixed |
| 176 | 176 | */ |
| 177 | - abstract protected function tagOpen( $parser, $name, $attr ); |
|
| 177 | + abstract protected function tagOpen($parser, $name, $attr); |
|
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | 180 | * @param $parser |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * |
| 183 | 183 | * @return mixed |
| 184 | 184 | */ |
| 185 | - abstract protected function tagClose( $parser, $name ); |
|
| 185 | + abstract protected function tagClose($parser, $name); |
|
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | 188 | * @param $parser |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @return mixed |
| 192 | 192 | */ |
| 193 | - abstract protected function characterData( $parser, $data ); |
|
| 193 | + abstract protected function characterData($parser, $data); |
|
| 194 | 194 | |
| 195 | 195 | /** |
| 196 | 196 | * postprocess escaped data and write to disk |
@@ -199,18 +199,18 @@ discard block |
||
| 199 | 199 | * @param string $data |
| 200 | 200 | * @param bool $treatAsCDATA |
| 201 | 201 | */ |
| 202 | - protected function postProcAndFlush( $fp, $data, $treatAsCDATA = false ) { |
|
| 202 | + protected function postProcAndFlush($fp, $data, $treatAsCDATA = false) { |
|
| 203 | 203 | //postprocess string |
| 204 | - $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data ); |
|
| 205 | - $data = str_replace( ' ', ' ', $data ); |
|
| 206 | - if ( !$treatAsCDATA ) { |
|
| 204 | + $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data); |
|
| 205 | + $data = str_replace(' ', ' ', $data); |
|
| 206 | + if (!$treatAsCDATA) { |
|
| 207 | 207 | //unix2dos |
| 208 | - $data = str_replace( "\r\n", "\r", $data ); |
|
| 209 | - $data = str_replace( "\n", "\r", $data ); |
|
| 210 | - $data = str_replace( "\r", "\r\n", $data ); |
|
| 208 | + $data = str_replace("\r\n", "\r", $data); |
|
| 209 | + $data = str_replace("\n", "\r", $data); |
|
| 210 | + $data = str_replace("\r", "\r\n", $data); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | //flush to disk |
| 214 | - fwrite( $fp, $data ); |
|
| 214 | + fwrite($fp, $data); |
|
| 215 | 215 | } |
| 216 | 216 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * @param string|null $xliffProprietary |
| 35 | 35 | * @param LoggerInterface|null $logger |
| 36 | 36 | */ |
| 37 | - public function __construct( $xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null ) { |
|
| 37 | + public function __construct($xliffVersion, $xliffProprietary = null, LoggerInterface $logger = null) { |
|
| 38 | 38 | $this->xliffVersion = $xliffVersion; |
| 39 | 39 | $this->logger = $logger; |
| 40 | 40 | $this->xliffProprietary = $xliffProprietary; |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * @return string |
| 45 | 45 | */ |
| 46 | 46 | protected function getTuTagName() { |
| 47 | - return ( $this->xliffVersion === 1 ) ? 'trans-unit' : 'unit'; |
|
| 47 | + return ($this->xliffVersion === 1) ? 'trans-unit' : 'unit'; |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - abstract public function parse( DOMDocument $dom, $output = [] ); |
|
| 55 | + abstract public function parse(DOMDocument $dom, $output = []); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * Extract trans-unit content from the current node |
@@ -64,17 +64,17 @@ discard block |
||
| 64 | 64 | * @param $i |
| 65 | 65 | * @param $j |
| 66 | 66 | */ |
| 67 | - protected function extractTuFromNode( $childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j ) { |
|
| 68 | - if ( $childNode->nodeName === 'group' ) { |
|
| 69 | - foreach ( $childNode->childNodes as $nestedChildNode ) { |
|
| 70 | - if ( $nestedChildNode->nodeName === 'group' ) { |
|
| 71 | - $this->extractTuFromNode( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 72 | - } elseif ( $nestedChildNode->nodeName === $this->getTuTagName() ) { |
|
| 73 | - $this->extractTransUnit( $nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 67 | + protected function extractTuFromNode($childNode, &$transUnitIdArrayForUniquenessCheck, DOMDocument $dom, &$output, &$i, &$j) { |
|
| 68 | + if ($childNode->nodeName === 'group') { |
|
| 69 | + foreach ($childNode->childNodes as $nestedChildNode) { |
|
| 70 | + if ($nestedChildNode->nodeName === 'group') { |
|
| 71 | + $this->extractTuFromNode($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 72 | + } elseif ($nestedChildNode->nodeName === $this->getTuTagName()) { |
|
| 73 | + $this->extractTransUnit($nestedChildNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | - } elseif ( $childNode->nodeName === $this->getTuTagName() ) { |
|
| 77 | - $this->extractTransUnit( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 76 | + } elseif ($childNode->nodeName === $this->getTuTagName()) { |
|
| 77 | + $this->extractTransUnit($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return mixed |
| 92 | 92 | */ |
| 93 | - abstract protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ); |
|
| 93 | + abstract protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j); |
|
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | 96 | * @param DOMDocument $dom |
@@ -98,10 +98,10 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return array |
| 100 | 100 | */ |
| 101 | - protected function extractContent( DOMDocument $dom, DOMNode $node ) { |
|
| 101 | + protected function extractContent(DOMDocument $dom, DOMNode $node) { |
|
| 102 | 102 | return [ |
| 103 | - 'raw-content' => $this->extractTagContent( $dom, $node ), |
|
| 104 | - 'attr' => $this->extractTagAttributes( $node ) |
|
| 103 | + 'raw-content' => $this->extractTagContent($dom, $node), |
|
| 104 | + 'attr' => $this->extractTagAttributes($node) |
|
| 105 | 105 | ]; |
| 106 | 106 | } |
| 107 | 107 | |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @return array |
| 120 | 120 | */ |
| 121 | - protected function extractTagAttributes( DOMNode $element ) { |
|
| 121 | + protected function extractTagAttributes(DOMNode $element) { |
|
| 122 | 122 | $tagAttributes = []; |
| 123 | 123 | |
| 124 | - if ( $element->hasAttributes() ) { |
|
| 125 | - foreach ( $element->attributes as $attr ) { |
|
| 126 | - $tagAttributes[ $attr->nodeName ] = $attr->nodeValue; |
|
| 124 | + if ($element->hasAttributes()) { |
|
| 125 | + foreach ($element->attributes as $attr) { |
|
| 126 | + $tagAttributes[$attr->nodeName] = $attr->nodeValue; |
|
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
@@ -138,17 +138,17 @@ discard block |
||
| 138 | 138 | * |
| 139 | 139 | * @return string |
| 140 | 140 | */ |
| 141 | - protected function extractTagContent( DOMDocument $dom, DOMNode $element ) { |
|
| 141 | + protected function extractTagContent(DOMDocument $dom, DOMNode $element) { |
|
| 142 | 142 | $childNodes = $element->hasChildNodes(); |
| 143 | 143 | $extractedContent = ''; |
| 144 | 144 | |
| 145 | - if ( !empty( $childNodes ) ) { |
|
| 146 | - foreach ( $element->childNodes as $node ) { |
|
| 147 | - $extractedContent .= Emoji::toEntity( Strings::fixNonWellFormedXml( $dom->saveXML( $node ) ) ); |
|
| 145 | + if (!empty($childNodes)) { |
|
| 146 | + foreach ($element->childNodes as $node) { |
|
| 147 | + $extractedContent .= Emoji::toEntity(Strings::fixNonWellFormedXml($dom->saveXML($node))); |
|
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - return str_replace( Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent ); |
|
| 151 | + return str_replace(Placeholder::EMPTY_TAG_PLACEHOLDER, '', $extractedContent); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -161,50 +161,50 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * @return array |
| 163 | 163 | */ |
| 164 | - protected function extractContentWithMarksAndExtTags( DOMDocument $dom, DOMElement $childNode, $originalRawContent, array $originalData = [] ) { |
|
| 164 | + protected function extractContentWithMarksAndExtTags(DOMDocument $dom, DOMElement $childNode, $originalRawContent, array $originalData = []) { |
|
| 165 | 165 | $source = []; |
| 166 | 166 | |
| 167 | 167 | // example: |
| 168 | 168 | // <g id="1"><mrk mid="0" mtype="seg">An English string with g tags</mrk></g> |
| 169 | - $raw = $this->extractTagContent( $dom, $childNode ); |
|
| 169 | + $raw = $this->extractTagContent($dom, $childNode); |
|
| 170 | 170 | |
| 171 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
| 171 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
| 172 | 172 | |
| 173 | 173 | $mi = 0; |
| 174 | - while ( isset( $markers[ $mi + 1 ] ) ) { |
|
| 175 | - unset( $mid ); |
|
| 174 | + while (isset($markers[$mi + 1])) { |
|
| 175 | + unset($mid); |
|
| 176 | 176 | |
| 177 | - preg_match( '|mid\s?=\s?["\'](.*?)["\']|si', $markers[ $mi + 1 ], $mid ); |
|
| 177 | + preg_match('|mid\s?=\s?["\'](.*?)["\']|si', $markers[$mi + 1], $mid); |
|
| 178 | 178 | |
| 179 | 179 | // if it's a Trados file the trailing spaces after </mrk> are meaningful |
| 180 | 180 | // so we add them to |
| 181 | 181 | $trailingSpaces = ''; |
| 182 | - if ( $this->xliffProprietary === 'trados' ) { |
|
| 183 | - preg_match_all( '/<\/mrk>[\s]+/iu', $markers[ $mi + 1 ], $trailingSpacesMatches ); |
|
| 182 | + if ($this->xliffProprietary === 'trados') { |
|
| 183 | + preg_match_all('/<\/mrk>[\s]+/iu', $markers[$mi + 1], $trailingSpacesMatches); |
|
| 184 | 184 | |
| 185 | - if ( isset( $trailingSpacesMatches[ 0 ] ) && count( $trailingSpacesMatches[ 0 ] ) > 0 ) { |
|
| 186 | - foreach ( $trailingSpacesMatches[ 0 ] as $match ) { |
|
| 187 | - $trailingSpaces = str_replace( '</mrk>', '', $match ); |
|
| 185 | + if (isset($trailingSpacesMatches[0]) && count($trailingSpacesMatches[0]) > 0) { |
|
| 186 | + foreach ($trailingSpacesMatches[0] as $match) { |
|
| 187 | + $trailingSpaces = str_replace('</mrk>', '', $match); |
|
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | //re-build the mrk tag after the split |
| 193 | - $originalMark = trim( '<mrk ' . $markers[ $mi + 1 ] ); |
|
| 193 | + $originalMark = trim('<mrk ' . $markers[$mi + 1]); |
|
| 194 | 194 | |
| 195 | - $mark_string = preg_replace( '#^<mrk\s[^>]+>(.*)#', '$1', $originalMark ); // at this point we have: ---> 'Test </mrk> </g>>' |
|
| 196 | - $mark_content = preg_split( '#</mrk>#si', $mark_string ); |
|
| 195 | + $mark_string = preg_replace('#^<mrk\s[^>]+>(.*)#', '$1', $originalMark); // at this point we have: ---> 'Test </mrk> </g>>' |
|
| 196 | + $mark_content = preg_split('#</mrk>#si', $mark_string); |
|
| 197 | 197 | |
| 198 | 198 | $sourceArray = [ |
| 199 | - 'mid' => ( isset( $mid[ 1 ] ) ) ? $mid[ 1 ] : $mi, |
|
| 200 | - 'ext-prec-tags' => ( $mi == 0 ? $markers[ 0 ] : "" ), |
|
| 201 | - 'raw-content' => ( isset( $mark_content[ 0 ] ) ) ? $mark_content[ 0 ] . $trailingSpaces : '', |
|
| 202 | - 'ext-succ-tags' => ( isset( $mark_content[ 1 ] ) ) ? $mark_content[ 1 ] : '', |
|
| 199 | + 'mid' => (isset($mid[1])) ? $mid[1] : $mi, |
|
| 200 | + 'ext-prec-tags' => ($mi == 0 ? $markers[0] : ""), |
|
| 201 | + 'raw-content' => (isset($mark_content[0])) ? $mark_content[0] . $trailingSpaces : '', |
|
| 202 | + 'ext-succ-tags' => (isset($mark_content[1])) ? $mark_content[1] : '', |
|
| 203 | 203 | ]; |
| 204 | 204 | |
| 205 | - if ( !empty( $originalData ) ) { |
|
| 206 | - $dataRefMap = $this->getDataRefMap( $originalData ); |
|
| 207 | - $sourceArray[ 'replaced-content' ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $mark_content[ 0 ] ); |
|
| 205 | + if (!empty($originalData)) { |
|
| 206 | + $dataRefMap = $this->getDataRefMap($originalData); |
|
| 207 | + $sourceArray['replaced-content'] = (new DataRefReplacer($dataRefMap))->replace($mark_content[0]); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | $source[] = $sourceArray; |
@@ -220,12 +220,12 @@ discard block |
||
| 220 | 220 | * |
| 221 | 221 | * @return array |
| 222 | 222 | */ |
| 223 | - protected function getDataRefMap( $originalData ) { |
|
| 223 | + protected function getDataRefMap($originalData) { |
|
| 224 | 224 | // dataRef map |
| 225 | 225 | $dataRefMap = []; |
| 226 | - foreach ( $originalData as $datum ) { |
|
| 227 | - if ( isset( $datum[ 'attr' ][ 'id' ] ) ) { |
|
| 228 | - $dataRefMap[ $datum[ 'attr' ][ 'id' ] ] = $datum[ 'raw-content' ]; |
|
| 226 | + foreach ($originalData as $datum) { |
|
| 227 | + if (isset($datum['attr']['id'])) { |
|
| 228 | + $dataRefMap[$datum['attr']['id']] = $datum['raw-content']; |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | |
@@ -237,10 +237,10 @@ discard block |
||
| 237 | 237 | * |
| 238 | 238 | * @return bool |
| 239 | 239 | */ |
| 240 | - protected function stringContainsMarks( $raw ) { |
|
| 241 | - $markers = preg_split( '#<mrk\s#si', $raw, -1 ); |
|
| 240 | + protected function stringContainsMarks($raw) { |
|
| 241 | + $markers = preg_split('#<mrk\s#si', $raw, -1); |
|
| 242 | 242 | |
| 243 | - return isset( $markers[ 1 ] ); |
|
| 243 | + return isset($markers[1]); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | * @return array |
| 251 | 251 | * @throws \Exception |
| 252 | 252 | */ |
| 253 | - protected function JSONOrRawContentArray( $noteValue, $escapeStrings = true ) { |
|
| 253 | + protected function JSONOrRawContentArray($noteValue, $escapeStrings = true) { |
|
| 254 | 254 | // |
| 255 | 255 | // convert double escaped entites |
| 256 | 256 | // |
@@ -260,17 +260,17 @@ discard block |
||
| 260 | 260 | // &amp; ---> & |
| 261 | 261 | // &apos ---> ' |
| 262 | 262 | // |
| 263 | - if ( Strings::isADoubleEscapedEntity( $noteValue ) ) { |
|
| 264 | - $noteValue = Strings::htmlspecialchars_decode( $noteValue, true ); |
|
| 263 | + if (Strings::isADoubleEscapedEntity($noteValue)) { |
|
| 264 | + $noteValue = Strings::htmlspecialchars_decode($noteValue, true); |
|
| 265 | 265 | } else { |
| 266 | 266 | // for non escaped entities $escapeStrings is always true for security reasons |
| 267 | 267 | $escapeStrings = true; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - if ( Strings::isJSON( $noteValue ) ) { |
|
| 271 | - return [ 'json' => Strings::cleanCDATA( $noteValue ) ]; |
|
| 270 | + if (Strings::isJSON($noteValue)) { |
|
| 271 | + return ['json' => Strings::cleanCDATA($noteValue)]; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - return [ 'raw-content' => Strings::fixNonWellFormedXml( $noteValue, $escapeStrings ) ]; |
|
| 274 | + return ['raw-content' => Strings::fixNonWellFormedXml($noteValue, $escapeStrings)]; |
|
| 275 | 275 | } |
| 276 | 276 | } |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return array|mixed |
| 20 | 20 | */ |
| 21 | - public static function pathInfo( $path, $options = 15 ) { |
|
| 22 | - $rawPath = explode( DIRECTORY_SEPARATOR, $path ); |
|
| 21 | + public static function pathInfo($path, $options = 15) { |
|
| 22 | + $rawPath = explode(DIRECTORY_SEPARATOR, $path); |
|
| 23 | 23 | |
| 24 | - $basename = array_pop( $rawPath ); |
|
| 25 | - $dirname = implode( DIRECTORY_SEPARATOR, $rawPath ); |
|
| 24 | + $basename = array_pop($rawPath); |
|
| 25 | + $dirname = implode(DIRECTORY_SEPARATOR, $rawPath); |
|
| 26 | 26 | |
| 27 | - $explodedFileName = explode( ".", $basename ); |
|
| 28 | - $extension = strtolower( array_pop( $explodedFileName ) ); |
|
| 29 | - $filename = implode( ".", $explodedFileName ); |
|
| 27 | + $explodedFileName = explode(".", $basename); |
|
| 28 | + $extension = strtolower(array_pop($explodedFileName)); |
|
| 29 | + $filename = implode(".", $explodedFileName); |
|
| 30 | 30 | |
| 31 | 31 | $returnArray = []; |
| 32 | 32 | |
@@ -39,17 +39,17 @@ discard block |
||
| 39 | 39 | |
| 40 | 40 | // foreach flag, add in $return_array the corresponding field, |
| 41 | 41 | // obtained by variable name correspondence |
| 42 | - foreach ( $flagMap as $field => $i ) { |
|
| 42 | + foreach ($flagMap as $field => $i) { |
|
| 43 | 43 | //binary AND |
| 44 | - if ( ( $options & $i ) > 0 ) { |
|
| 44 | + if (($options & $i) > 0) { |
|
| 45 | 45 | //variable substitution: $field can be one between 'dirname', 'basename', 'extension', 'filename' |
| 46 | 46 | // $$field gets the value of the variable named $field |
| 47 | - $returnArray[ $field ] = $$field; |
|
| 47 | + $returnArray[$field] = $$field; |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if ( count( $returnArray ) == 1 ) { |
|
| 52 | - $returnArray = array_pop( $returnArray ); |
|
| 51 | + if (count($returnArray) == 1) { |
|
| 52 | + $returnArray = array_pop($returnArray); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | return $returnArray; |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @return false|string |
| 62 | 62 | */ |
| 63 | - public static function getExtension( $path ) { |
|
| 64 | - $pathInfo = self::pathInfo( $path ); |
|
| 63 | + public static function getExtension($path) { |
|
| 64 | + $pathInfo = self::pathInfo($path); |
|
| 65 | 65 | |
| 66 | - if ( empty( $pathInfo ) ) { |
|
| 66 | + if (empty($pathInfo)) { |
|
| 67 | 67 | return false; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - return strtolower( $pathInfo[ 'extension' ] ); |
|
| 70 | + return strtolower($pathInfo['extension']); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -75,14 +75,14 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return bool |
| 77 | 77 | */ |
| 78 | - public static function isXliff( $path ) { |
|
| 79 | - $extension = self::getExtension( $path ); |
|
| 78 | + public static function isXliff($path) { |
|
| 79 | + $extension = self::getExtension($path); |
|
| 80 | 80 | |
| 81 | - if ( !$extension ) { |
|
| 81 | + if (!$extension) { |
|
| 82 | 82 | return false; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - switch ( $extension ) { |
|
| 85 | + switch ($extension) { |
|
| 86 | 86 | case 'xliff': |
| 87 | 87 | case 'sdlxliff': |
| 88 | 88 | case 'tmx': |
@@ -98,14 +98,14 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool|string |
| 100 | 100 | */ |
| 101 | - public static function getMemoryFileType( $path ) { |
|
| 102 | - $pathInfo = self::pathInfo( $path ); |
|
| 101 | + public static function getMemoryFileType($path) { |
|
| 102 | + $pathInfo = self::pathInfo($path); |
|
| 103 | 103 | |
| 104 | - if ( empty( $pathInfo ) ) { |
|
| 104 | + if (empty($pathInfo)) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - switch ( strtolower( $pathInfo[ 'extension' ] ) ) { |
|
| 108 | + switch (strtolower($pathInfo['extension'])) { |
|
| 109 | 109 | case 'tmx': |
| 110 | 110 | return 'tmx'; |
| 111 | 111 | case 'g': |
@@ -120,8 +120,8 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return bool |
| 122 | 122 | */ |
| 123 | - public static function isTMXFile( $path ) { |
|
| 124 | - return self::getMemoryFileType( $path ) === 'tmx'; |
|
| 123 | + public static function isTMXFile($path) { |
|
| 124 | + return self::getMemoryFileType($path) === 'tmx'; |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return bool |
| 131 | 131 | */ |
| 132 | - public static function isGlossaryFile( $path ) { |
|
| 133 | - return self::getMemoryFileType( $path ) === 'glossary'; |
|
| 132 | + public static function isGlossaryFile($path) { |
|
| 133 | + return self::getMemoryFileType($path) === 'glossary'; |
|
| 134 | 134 | } |
| 135 | 135 | } |
@@ -16,30 +16,30 @@ discard block |
||
| 16 | 16 | * @inheritDoc |
| 17 | 17 | * @throws \Exception |
| 18 | 18 | */ |
| 19 | - public function parse( DOMDocument $dom, $output = [] ) { |
|
| 19 | + public function parse(DOMDocument $dom, $output = []) { |
|
| 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( $dom ); |
|
| 25 | + $output['files'][$i]['attr'] = $this->extractMetadata($dom); |
|
| 26 | 26 | |
| 27 | 27 | // notes |
| 28 | - $output[ 'files' ][ $i ][ 'notes' ] = $this->extractNotes( $file ); |
|
| 28 | + $output['files'][$i]['notes'] = $this->extractNotes($file); |
|
| 29 | 29 | |
| 30 | 30 | // trans-units |
| 31 | 31 | $transUnitIdArrayForUniquenessCheck = []; |
| 32 | 32 | $j = 1; |
| 33 | 33 | /** @var DOMElement $transUnit */ |
| 34 | - foreach ( $file->childNodes as $childNode ) { |
|
| 35 | - $this->extractTuFromNode( $childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j ); |
|
| 34 | + foreach ($file->childNodes as $childNode) { |
|
| 35 | + $this->extractTuFromNode($childNode, $transUnitIdArrayForUniquenessCheck, $dom, $output, $i, $j); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | // trans-unit re-count check |
| 39 | - $totalTransUnitsId = count( $transUnitIdArrayForUniquenessCheck ); |
|
| 40 | - $transUnitsUniqueId = count( array_unique( $transUnitIdArrayForUniquenessCheck ) ); |
|
| 41 | - if ( $totalTransUnitsId != $transUnitsUniqueId ) { |
|
| 42 | - throw new DuplicateTransUnitIdInXliff( "Invalid trans-unit id, duplicate found.", 400 ); |
|
| 39 | + $totalTransUnitsId = count($transUnitIdArrayForUniquenessCheck); |
|
| 40 | + $transUnitsUniqueId = count(array_unique($transUnitIdArrayForUniquenessCheck)); |
|
| 41 | + if ($totalTransUnitsId != $transUnitsUniqueId) { |
|
| 42 | + throw new DuplicateTransUnitIdInXliff("Invalid trans-unit id, duplicate found.", 400); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $i++; |
@@ -53,23 +53,23 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return array |
| 55 | 55 | */ |
| 56 | - private function extractMetadata( DOMDocument $dom ) { |
|
| 56 | + private function extractMetadata(DOMDocument $dom) { |
|
| 57 | 57 | $metadata = []; |
| 58 | 58 | |
| 59 | - $xliffNode = $dom->getElementsByTagName( 'xliff' )->item( 0 ); |
|
| 60 | - $fileNode = $dom->getElementsByTagName( 'file' )->item( 0 ); |
|
| 59 | + $xliffNode = $dom->getElementsByTagName('xliff')->item(0); |
|
| 60 | + $fileNode = $dom->getElementsByTagName('file')->item(0); |
|
| 61 | 61 | |
| 62 | 62 | // original |
| 63 | - $metadata[ 'original' ] = ( null !== $fileNode->attributes->getNamedItem( 'original' ) ) ? $fileNode->attributes->getNamedItem( 'original' )->nodeValue : 'no-name'; |
|
| 63 | + $metadata['original'] = (null !== $fileNode->attributes->getNamedItem('original')) ? $fileNode->attributes->getNamedItem('original')->nodeValue : 'no-name'; |
|
| 64 | 64 | |
| 65 | 65 | // source-language |
| 66 | - $metadata[ 'source-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'srcLang' ) ) ? $xliffNode->attributes->getNamedItem( 'srcLang' )->nodeValue : 'en-US'; |
|
| 66 | + $metadata['source-language'] = (null !== $xliffNode->attributes->getNamedItem('srcLang')) ? $xliffNode->attributes->getNamedItem('srcLang')->nodeValue : 'en-US'; |
|
| 67 | 67 | |
| 68 | 68 | // datatype |
| 69 | 69 | // @TODO to be implemented |
| 70 | 70 | |
| 71 | 71 | // target-language |
| 72 | - $metadata[ 'target-language' ] = ( null !== $xliffNode->attributes->getNamedItem( 'trgLang' ) ) ? $xliffNode->attributes->getNamedItem( 'trgLang' )->nodeValue : 'en-US'; |
|
| 72 | + $metadata['target-language'] = (null !== $xliffNode->attributes->getNamedItem('trgLang')) ? $xliffNode->attributes->getNamedItem('trgLang')->nodeValue : 'en-US'; |
|
| 73 | 73 | |
| 74 | 74 | // custom MateCat x-attribute |
| 75 | 75 | // @TODO to be implemented |
@@ -83,16 +83,16 @@ discard block |
||
| 83 | 83 | * @return array |
| 84 | 84 | * @throws \Exception |
| 85 | 85 | */ |
| 86 | - private function extractNotes( DOMElement $file ) { |
|
| 86 | + private function extractNotes(DOMElement $file) { |
|
| 87 | 87 | $notes = []; |
| 88 | 88 | |
| 89 | 89 | // loop <notes> to get nested <note> tag |
| 90 | - foreach ( $file->childNodes as $childNode ) { |
|
| 91 | - if ( $childNode->nodeName === 'notes' ) { |
|
| 92 | - foreach ( $childNode->childNodes as $note ) { |
|
| 93 | - $noteValue = trim( $note->nodeValue ); |
|
| 94 | - if ( '' !== $noteValue ) { |
|
| 95 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
| 90 | + foreach ($file->childNodes as $childNode) { |
|
| 91 | + if ($childNode->nodeName === 'notes') { |
|
| 92 | + foreach ($childNode->childNodes as $note) { |
|
| 93 | + $noteValue = trim($note->nodeValue); |
|
| 94 | + if ('' !== $noteValue) { |
|
| 95 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | } |
@@ -113,34 +113,34 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @throws \Exception |
| 115 | 115 | */ |
| 116 | - protected function extractTransUnit( $transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j ) { |
|
| 116 | + protected function extractTransUnit($transUnit, &$transUnitIdArrayForUniquenessCheck, $dom, &$output, &$i, &$j) { |
|
| 117 | 117 | // metadata |
| 118 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ] = $this->extractTransUnitMetadata( $transUnit, $transUnitIdArrayForUniquenessCheck ); |
|
| 118 | + $output['files'][$i]['trans-units'][$j]['attr'] = $this->extractTransUnitMetadata($transUnit, $transUnitIdArrayForUniquenessCheck); |
|
| 119 | 119 | |
| 120 | 120 | // notes |
| 121 | 121 | // merge <notes> with key and key-note contained in metadata <mda:metaGroup> |
| 122 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] = $this->extractTransUnitNotes( $transUnit ); |
|
| 122 | + $output['files'][$i]['trans-units'][$j]['notes'] = $this->extractTransUnitNotes($transUnit); |
|
| 123 | 123 | |
| 124 | 124 | // uuid |
| 125 | - foreach ( $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'notes' ] as $note ) { |
|
| 126 | - if ( isset( $note[ 'raw-content' ] ) && Strings::isAValidUuid( $note[ 'raw-content' ] ) ) { |
|
| 127 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ][ 'uuid' ] = $note[ 'raw-content' ]; |
|
| 125 | + foreach ($output['files'][$i]['trans-units'][$j]['notes'] as $note) { |
|
| 126 | + if (isset($note['raw-content']) && Strings::isAValidUuid($note['raw-content'])) { |
|
| 127 | + $output['files'][$i]['trans-units'][$j]['attr']['uuid'] = $note['raw-content']; |
|
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // original-data (exclusive for V2) |
| 132 | 132 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html#originaldata |
| 133 | - $originalData = $this->extractTransUnitOriginalData( $transUnit ); |
|
| 133 | + $originalData = $this->extractTransUnitOriginalData($transUnit); |
|
| 134 | 134 | $dataRefMap = null; |
| 135 | - if ( !empty( $originalData ) ) { |
|
| 136 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'original-data' ] = $originalData; |
|
| 137 | - $dataRefMap = $this->getDataRefMap( $originalData ); |
|
| 135 | + if (!empty($originalData)) { |
|
| 136 | + $output['files'][$i]['trans-units'][$j]['original-data'] = $originalData; |
|
| 137 | + $dataRefMap = $this->getDataRefMap($originalData); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // additionalTagData (exclusive for V2) |
| 141 | - $additionalTagData = $this->extractTransUnitAdditionalTagData( $transUnit ); |
|
| 142 | - if ( !empty( $additionalTagData ) ) { |
|
| 143 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'additional-tag-data' ] = $additionalTagData; |
|
| 141 | + $additionalTagData = $this->extractTransUnitAdditionalTagData($transUnit); |
|
| 142 | + if (!empty($additionalTagData)) { |
|
| 143 | + $output['files'][$i]['trans-units'][$j]['additional-tag-data'] = $additionalTagData; |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // content |
@@ -160,62 +160,62 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | /** @var DOMElement $segment */ |
| 162 | 162 | $c = 0; |
| 163 | - foreach ( $transUnit->childNodes as $segment ) { |
|
| 164 | - if ( $segment->nodeName === 'segment' ) { |
|
| 163 | + foreach ($transUnit->childNodes as $segment) { |
|
| 164 | + if ($segment->nodeName === 'segment') { |
|
| 165 | 165 | |
| 166 | 166 | // check segment id consistency |
| 167 | - $attr = $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'attr' ]; |
|
| 168 | - $this->checkSegmentIdConsistency( $segment, $attr ); |
|
| 167 | + $attr = $output['files'][$i]['trans-units'][$j]['attr']; |
|
| 168 | + $this->checkSegmentIdConsistency($segment, $attr); |
|
| 169 | 169 | |
| 170 | 170 | // loop <segment> to get nested <source> and <target> tag |
| 171 | - foreach ( $segment->childNodes as $childNode ) { |
|
| 172 | - if ( $childNode->nodeName === 'source' ) { |
|
| 173 | - $extractedSource = $this->extractContent( $dom, $childNode ); |
|
| 174 | - $source[ 'raw-content' ][ $c ] = $extractedSource[ 'raw-content' ]; |
|
| 171 | + foreach ($segment->childNodes as $childNode) { |
|
| 172 | + if ($childNode->nodeName === 'source') { |
|
| 173 | + $extractedSource = $this->extractContent($dom, $childNode); |
|
| 174 | + $source['raw-content'][$c] = $extractedSource['raw-content']; |
|
| 175 | 175 | |
| 176 | - if ( !empty( $originalData ) ) { |
|
| 177 | - $source[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $source[ 'raw-content' ][ $c ] ); |
|
| 176 | + if (!empty($originalData)) { |
|
| 177 | + $source['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($source['raw-content'][$c]); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - if ( !empty( $extractedSource[ 'attr' ] ) ) { |
|
| 181 | - $source[ 'attr' ][ $c ] = $extractedSource[ 'attr' ]; |
|
| 180 | + if (!empty($extractedSource['attr'])) { |
|
| 181 | + $source['attr'][$c] = $extractedSource['attr']; |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // append value to 'seg-source' |
| 185 | - if ( $this->stringContainsMarks( $extractedSource[ 'raw-content' ] ) ) { |
|
| 186 | - $segSource = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedSource[ 'raw-content' ], $originalData ); |
|
| 185 | + if ($this->stringContainsMarks($extractedSource['raw-content'])) { |
|
| 186 | + $segSource = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedSource['raw-content'], $originalData); |
|
| 187 | 187 | } else { |
| 188 | 188 | $segSource[] = [ |
| 189 | - 'mid' => count( $segSource ) > 0 ? count( $segSource ) : 0, |
|
| 189 | + 'mid' => count($segSource) > 0 ? count($segSource) : 0, |
|
| 190 | 190 | 'ext-prec-tags' => '', |
| 191 | - 'raw-content' => $extractedSource[ 'raw-content' ], |
|
| 192 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedSource[ 'raw-content' ] ) : null, |
|
| 191 | + 'raw-content' => $extractedSource['raw-content'], |
|
| 192 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedSource['raw-content']) : null, |
|
| 193 | 193 | 'ext-succ-tags' => '', |
| 194 | 194 | ]; |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - if ( $childNode->nodeName === 'target' ) { |
|
| 199 | - $extractedTarget = $this->extractContent( $dom, $childNode ); |
|
| 200 | - $target[ 'raw-content' ][ $c ] = $extractedTarget[ 'raw-content' ]; |
|
| 198 | + if ($childNode->nodeName === 'target') { |
|
| 199 | + $extractedTarget = $this->extractContent($dom, $childNode); |
|
| 200 | + $target['raw-content'][$c] = $extractedTarget['raw-content']; |
|
| 201 | 201 | |
| 202 | - if ( !empty( $originalData ) ) { |
|
| 203 | - $target[ 'replaced-content' ][ $c ] = ( new DataRefReplacer( $dataRefMap ) )->replace( $target[ 'raw-content' ][ $c ] ); |
|
| 202 | + if (!empty($originalData)) { |
|
| 203 | + $target['replaced-content'][$c] = (new DataRefReplacer($dataRefMap))->replace($target['raw-content'][$c]); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - if ( !empty( $extractedTarget[ 'attr' ] ) ) { |
|
| 207 | - $target[ 'attr' ][ $c ] = $extractedTarget[ 'attr' ]; |
|
| 206 | + if (!empty($extractedTarget['attr'])) { |
|
| 207 | + $target['attr'][$c] = $extractedTarget['attr']; |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | // append value to 'seg-target' |
| 211 | - if ( $this->stringContainsMarks( $extractedTarget[ 'raw-content' ] ) ) { |
|
| 212 | - $segTarget = $this->extractContentWithMarksAndExtTags( $dom, $childNode, $extractedTarget[ 'raw-content' ], $originalData ); |
|
| 211 | + if ($this->stringContainsMarks($extractedTarget['raw-content'])) { |
|
| 212 | + $segTarget = $this->extractContentWithMarksAndExtTags($dom, $childNode, $extractedTarget['raw-content'], $originalData); |
|
| 213 | 213 | } else { |
| 214 | 214 | $segTarget[] = [ |
| 215 | - 'mid' => count( $segTarget ) > 0 ? count( $segTarget ) : 0, |
|
| 215 | + 'mid' => count($segTarget) > 0 ? count($segTarget) : 0, |
|
| 216 | 216 | 'ext-prec-tags' => '', |
| 217 | - 'raw-content' => $extractedTarget[ 'raw-content' ], |
|
| 218 | - 'replaced-content' => ( !empty( $originalData ) ) ? ( new DataRefReplacer( $dataRefMap ) )->replace( $extractedTarget[ 'raw-content' ] ) : null, |
|
| 217 | + 'raw-content' => $extractedTarget['raw-content'], |
|
| 218 | + 'replaced-content' => (!empty($originalData)) ? (new DataRefReplacer($dataRefMap))->replace($extractedTarget['raw-content']) : null, |
|
| 219 | 219 | 'ext-succ-tags' => '', |
| 220 | 220 | ]; |
| 221 | 221 | } |
@@ -226,10 +226,10 @@ discard block |
||
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'source' ] = $source; |
|
| 230 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'target' ] = $target; |
|
| 231 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-source' ] = $segSource; |
|
| 232 | - $output[ 'files' ][ $i ][ 'trans-units' ][ $j ][ 'seg-target' ] = $segTarget; |
|
| 229 | + $output['files'][$i]['trans-units'][$j]['source'] = $source; |
|
| 230 | + $output['files'][$i]['trans-units'][$j]['target'] = $target; |
|
| 231 | + $output['files'][$i]['trans-units'][$j]['seg-source'] = $segSource; |
|
| 232 | + $output['files'][$i]['trans-units'][$j]['seg-target'] = $segTarget; |
|
| 233 | 233 | |
| 234 | 234 | $j++; |
| 235 | 235 | } |
@@ -240,41 +240,41 @@ discard block |
||
| 240 | 240 | * |
| 241 | 241 | * @return array |
| 242 | 242 | */ |
| 243 | - private function extractTransUnitMetadata( DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck ) { |
|
| 243 | + private function extractTransUnitMetadata(DOMElement $transUnit, &$transUnitIdArrayForUniquenessCheck) { |
|
| 244 | 244 | $metadata = []; |
| 245 | 245 | |
| 246 | 246 | // id |
| 247 | - if ( null === $transUnit->attributes->getNamedItem( 'id' ) ) { |
|
| 248 | - throw new NotFoundIdInTransUnit( 'Invalid trans-unit id found. EMPTY value', 400 ); |
|
| 247 | + if (null === $transUnit->attributes->getNamedItem('id')) { |
|
| 248 | + throw new NotFoundIdInTransUnit('Invalid trans-unit id found. EMPTY value', 400); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | - $id = $transUnit->attributes->getNamedItem( 'id' )->nodeValue; |
|
| 251 | + $id = $transUnit->attributes->getNamedItem('id')->nodeValue; |
|
| 252 | 252 | |
| 253 | - if ( strlen( $id ) > 100 ) { |
|
| 254 | - throw new SegmentIdTooLongException( 'Segment-id too long. Max 100 characters allowed', 400 ); |
|
| 253 | + if (strlen($id) > 100) { |
|
| 254 | + throw new SegmentIdTooLongException('Segment-id too long. Max 100 characters allowed', 400); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | $transUnitIdArrayForUniquenessCheck[] = $id; |
| 258 | - $metadata[ 'id' ] = $id; |
|
| 258 | + $metadata['id'] = $id; |
|
| 259 | 259 | |
| 260 | 260 | // translate |
| 261 | - if ( null !== $transUnit->attributes->getNamedItem( 'translate' ) ) { |
|
| 262 | - $metadata[ 'translate' ] = $transUnit->attributes->getNamedItem( 'translate' )->nodeValue; |
|
| 261 | + if (null !== $transUnit->attributes->getNamedItem('translate')) { |
|
| 262 | + $metadata['translate'] = $transUnit->attributes->getNamedItem('translate')->nodeValue; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | // tGroupBegin |
| 266 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupBegin' ) ) { |
|
| 267 | - $metadata[ 'tGroupBegin' ] = $transUnit->attributes->getNamedItem( 'tGroupBegin' )->nodeValue; |
|
| 266 | + if (null !== $transUnit->attributes->getNamedItem('tGroupBegin')) { |
|
| 267 | + $metadata['tGroupBegin'] = $transUnit->attributes->getNamedItem('tGroupBegin')->nodeValue; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | // tGroupEnd |
| 271 | - if ( null !== $transUnit->attributes->getNamedItem( 'tGroupEnd' ) ) { |
|
| 272 | - $metadata[ 'tGroupEnd' ] = $transUnit->attributes->getNamedItem( 'tGroupEnd' )->nodeValue; |
|
| 271 | + if (null !== $transUnit->attributes->getNamedItem('tGroupEnd')) { |
|
| 272 | + $metadata['tGroupEnd'] = $transUnit->attributes->getNamedItem('tGroupEnd')->nodeValue; |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | // sizeRestriction |
| 276 | - if ( null !== $transUnit->attributes->getNamedItem( 'sizeRestriction' ) && '' !== $transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue ) { |
|
| 277 | - $metadata[ 'sizeRestriction' ] = (int)$transUnit->attributes->getNamedItem( 'sizeRestriction' )->nodeValue; |
|
| 276 | + if (null !== $transUnit->attributes->getNamedItem('sizeRestriction') && '' !== $transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue) { |
|
| 277 | + $metadata['sizeRestriction'] = (int)$transUnit->attributes->getNamedItem('sizeRestriction')->nodeValue; |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | return $metadata; |
@@ -286,31 +286,31 @@ discard block |
||
| 286 | 286 | * @return array |
| 287 | 287 | * @throws \Exception |
| 288 | 288 | */ |
| 289 | - private function extractTransUnitOriginalData( DOMElement $transUnit ) { |
|
| 289 | + private function extractTransUnitOriginalData(DOMElement $transUnit) { |
|
| 290 | 290 | $originalData = []; |
| 291 | 291 | |
| 292 | 292 | // loop <originalData> to get nested content |
| 293 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
| 294 | - if ( $childNode->nodeName === 'originalData' ) { |
|
| 295 | - foreach ( $childNode->childNodes as $data ) { |
|
| 296 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
| 297 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
| 293 | + foreach ($transUnit->childNodes as $childNode) { |
|
| 294 | + if ($childNode->nodeName === 'originalData') { |
|
| 295 | + foreach ($childNode->childNodes as $data) { |
|
| 296 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
| 297 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
| 298 | 298 | |
| 299 | - $dataValue = str_replace( Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue ); |
|
| 300 | - $dataValue = str_replace( Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue ); |
|
| 301 | - $dataValue = str_replace( Placeholder::TAB_PLACEHOLDER, '\t', $dataValue ); |
|
| 299 | + $dataValue = str_replace(Placeholder::WHITE_SPACE_PLACEHOLDER, ' ', $data->nodeValue); |
|
| 300 | + $dataValue = str_replace(Placeholder::NEW_LINE_PLACEHOLDER, '\n', $dataValue); |
|
| 301 | + $dataValue = str_replace(Placeholder::TAB_PLACEHOLDER, '\t', $dataValue); |
|
| 302 | 302 | |
| 303 | - if ( '' !== $dataValue ) { |
|
| 303 | + if ('' !== $dataValue) { |
|
| 304 | 304 | |
| 305 | - $jsonOrRawContentArray = $this->JSONOrRawContentArray( $dataValue, false ); |
|
| 305 | + $jsonOrRawContentArray = $this->JSONOrRawContentArray($dataValue, false); |
|
| 306 | 306 | |
| 307 | 307 | // restore xliff tags |
| 308 | - if ( isset( $jsonOrRawContentArray[ 'json' ] ) ) { |
|
| 309 | - $jsonOrRawContentArray[ 'json' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'json' ] ); |
|
| 308 | + if (isset($jsonOrRawContentArray['json'])) { |
|
| 309 | + $jsonOrRawContentArray['json'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['json']); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - if ( isset( $jsonOrRawContentArray[ 'raw-content' ] ) ) { |
|
| 313 | - $jsonOrRawContentArray[ 'raw-content' ] = str_replace( [ Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER ], [ '<', '>' ], $jsonOrRawContentArray[ 'raw-content' ] ); |
|
| 312 | + if (isset($jsonOrRawContentArray['raw-content'])) { |
|
| 313 | + $jsonOrRawContentArray['raw-content'] = str_replace([Placeholder::LT_PLACEHOLDER, Placeholder::GT_PLACEHOLDER], ['<', '>'], $jsonOrRawContentArray['raw-content']); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | $originalData[] = array_merge( |
@@ -335,41 +335,41 @@ discard block |
||
| 335 | 335 | * |
| 336 | 336 | * @return array |
| 337 | 337 | */ |
| 338 | - private function extractTransUnitAdditionalTagData( DOMElement $transUnit ) { |
|
| 338 | + private function extractTransUnitAdditionalTagData(DOMElement $transUnit) { |
|
| 339 | 339 | $additionalTagData = []; |
| 340 | 340 | |
| 341 | 341 | // loop <originalData> to get nested content |
| 342 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
| 343 | - if ( $childNode->nodeName === 'memsource:additionalTagData' ) { |
|
| 344 | - foreach ( $childNode->childNodes as $data ) { |
|
| 342 | + foreach ($transUnit->childNodes as $childNode) { |
|
| 343 | + if ($childNode->nodeName === 'memsource:additionalTagData') { |
|
| 344 | + foreach ($childNode->childNodes as $data) { |
|
| 345 | 345 | $dataArray = []; |
| 346 | 346 | |
| 347 | 347 | // id |
| 348 | - if ( $data->nodeName === 'memsource:tag' ) { |
|
| 349 | - if ( null !== $data->attributes && null !== $data->attributes->getNamedItem( 'id' ) ) { |
|
| 350 | - $dataId = $data->attributes->getNamedItem( 'id' )->nodeValue; |
|
| 351 | - $dataArray[ 'attr' ][ 'id' ] = $dataId; |
|
| 348 | + if ($data->nodeName === 'memsource:tag') { |
|
| 349 | + if (null !== $data->attributes && null !== $data->attributes->getNamedItem('id')) { |
|
| 350 | + $dataId = $data->attributes->getNamedItem('id')->nodeValue; |
|
| 351 | + $dataArray['attr']['id'] = $dataId; |
|
| 352 | 352 | } |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | // in PHP 7.4 $data->childNodes is an empty DomNodeList, it is iterable with size 0 |
| 356 | 356 | // PHP 5.6 check: in php 5.6 $data->childNodes can be null |
| 357 | - if ( $data->childNodes != null ) { |
|
| 357 | + if ($data->childNodes != null) { |
|
| 358 | 358 | |
| 359 | 359 | // content |
| 360 | - foreach ( $data->childNodes as $datum ) { |
|
| 361 | - if ( $datum->nodeName === 'memsource:tagId' ) { |
|
| 362 | - $dataArray[ 'raw-content' ][ 'tagId' ] = $datum->nodeValue; |
|
| 360 | + foreach ($data->childNodes as $datum) { |
|
| 361 | + if ($datum->nodeName === 'memsource:tagId') { |
|
| 362 | + $dataArray['raw-content']['tagId'] = $datum->nodeValue; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | - if ( $datum->nodeName === 'memsource:type' ) { |
|
| 366 | - $dataArray[ 'raw-content' ][ 'type' ] = $datum->nodeValue; |
|
| 365 | + if ($datum->nodeName === 'memsource:type') { |
|
| 366 | + $dataArray['raw-content']['type'] = $datum->nodeValue; |
|
| 367 | 367 | } |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | } |
| 371 | 371 | |
| 372 | - if ( !empty( $dataArray ) ) { |
|
| 372 | + if (!empty($dataArray)) { |
|
| 373 | 373 | $additionalTagData[] = $dataArray; |
| 374 | 374 | } |
| 375 | 375 | } |
@@ -385,15 +385,15 @@ discard block |
||
| 385 | 385 | * @param DOMElement $segment |
| 386 | 386 | * @param array $attr |
| 387 | 387 | */ |
| 388 | - private function checkSegmentIdConsistency( DOMElement $segment, array $attr ) { |
|
| 389 | - if ( isset( $attr[ 'tGroupBegin' ] ) && isset( $attr[ 'tGroupEnd' ] ) && $segment->attributes->getNamedItem( 'id' ) ) { |
|
| 390 | - $id = $segment->attributes->getNamedItem( 'id' )->nodeValue; |
|
| 391 | - $min = (int)$attr[ 'tGroupBegin' ]; |
|
| 392 | - $max = (int)$attr[ 'tGroupEnd' ]; |
|
| 393 | - |
|
| 394 | - if ( false === ( ( $min <= $id ) && ( $id <= $max ) ) ) { |
|
| 395 | - if ( $this->logger ) { |
|
| 396 | - $this->logger->warning( 'Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd' ); |
|
| 388 | + private function checkSegmentIdConsistency(DOMElement $segment, array $attr) { |
|
| 389 | + if (isset($attr['tGroupBegin']) && isset($attr['tGroupEnd']) && $segment->attributes->getNamedItem('id')) { |
|
| 390 | + $id = $segment->attributes->getNamedItem('id')->nodeValue; |
|
| 391 | + $min = (int)$attr['tGroupBegin']; |
|
| 392 | + $max = (int)$attr['tGroupEnd']; |
|
| 393 | + |
|
| 394 | + if (false === (($min <= $id) && ($id <= $max))) { |
|
| 395 | + if ($this->logger) { |
|
| 396 | + $this->logger->warning('Segment #' . $id . ' is not included within tGroupBegin and tGroupEnd'); |
|
| 397 | 397 | } |
| 398 | 398 | } |
| 399 | 399 | } |
@@ -405,31 +405,31 @@ discard block |
||
| 405 | 405 | * @return array |
| 406 | 406 | * @throws \Exception |
| 407 | 407 | */ |
| 408 | - private function extractTransUnitNotes( DOMElement $transUnit ) { |
|
| 408 | + private function extractTransUnitNotes(DOMElement $transUnit) { |
|
| 409 | 409 | $notes = []; |
| 410 | 410 | |
| 411 | 411 | // loop <notes> to get nested <note> tag |
| 412 | - foreach ( $transUnit->childNodes as $childNode ) { |
|
| 413 | - if ( $childNode->nodeName == 'notes' ) { |
|
| 414 | - foreach ( $childNode->childNodes as $note ) { |
|
| 415 | - $noteValue = trim( $note->nodeValue ); |
|
| 416 | - if ( '' !== $noteValue ) { |
|
| 417 | - $notes[] = $this->JSONOrRawContentArray( $noteValue ); |
|
| 412 | + foreach ($transUnit->childNodes as $childNode) { |
|
| 413 | + if ($childNode->nodeName == 'notes') { |
|
| 414 | + foreach ($childNode->childNodes as $note) { |
|
| 415 | + $noteValue = trim($note->nodeValue); |
|
| 416 | + if ('' !== $noteValue) { |
|
| 417 | + $notes[] = $this->JSONOrRawContentArray($noteValue); |
|
| 418 | 418 | } |
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | |
| 422 | - if ( $childNode->nodeName === 'mda:metadata' ) { |
|
| 423 | - foreach ( $childNode->childNodes as $metadata ) { |
|
| 424 | - if ( $metadata->nodeName === 'mda:metaGroup' ) { |
|
| 425 | - foreach ( $metadata->childNodes as $meta ) { |
|
| 426 | - if ( null !== $meta->attributes && null !== $meta->attributes->getNamedItem( 'type' ) ) { |
|
| 427 | - $type = $meta->attributes->getNamedItem( 'type' )->nodeValue; |
|
| 428 | - $metaValue = trim( $meta->nodeValue ); |
|
| 422 | + if ($childNode->nodeName === 'mda:metadata') { |
|
| 423 | + foreach ($childNode->childNodes as $metadata) { |
|
| 424 | + if ($metadata->nodeName === 'mda:metaGroup') { |
|
| 425 | + foreach ($metadata->childNodes as $meta) { |
|
| 426 | + if (null !== $meta->attributes && null !== $meta->attributes->getNamedItem('type')) { |
|
| 427 | + $type = $meta->attributes->getNamedItem('type')->nodeValue; |
|
| 428 | + $metaValue = trim($meta->nodeValue); |
|
| 429 | 429 | |
| 430 | - if ( '' !== $metaValue ) { |
|
| 430 | + if ('' !== $metaValue) { |
|
| 431 | 431 | $notes[] = array_merge( |
| 432 | - $this->JSONOrRawContentArray( $metaValue ), |
|
| 432 | + $this->JSONOrRawContentArray($metaValue), |
|
| 433 | 433 | [ |
| 434 | 434 | 'attr' => [ |
| 435 | 435 | 'type' => $type |
@@ -15,17 +15,17 @@ discard block |
||
| 15 | 15 | * |
| 16 | 16 | * @return array |
| 17 | 17 | */ |
| 18 | - public static function parse( $html ) { |
|
| 19 | - $toBeEscaped = Strings::isAnEscapedHTML( $html ); |
|
| 18 | + public static function parse($html) { |
|
| 19 | + $toBeEscaped = Strings::isAnEscapedHTML($html); |
|
| 20 | 20 | |
| 21 | - if ( $toBeEscaped ) { |
|
| 22 | - $html = Strings::htmlspecialchars_decode( $html ); |
|
| 21 | + if ($toBeEscaped) { |
|
| 22 | + $html = Strings::htmlspecialchars_decode($html); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - $html = self::protectNotClosedHtmlTags( $html ); |
|
| 26 | - $html = self::protectNotHtmlLessThanSymbols( $html ); |
|
| 25 | + $html = self::protectNotClosedHtmlTags($html); |
|
| 26 | + $html = self::protectNotHtmlLessThanSymbols($html); |
|
| 27 | 27 | |
| 28 | - return self::extractHtmlNode( $html, $toBeEscaped ); |
|
| 28 | + return self::extractHtmlNode($html, $toBeEscaped); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
@@ -43,41 +43,41 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @return string |
| 45 | 45 | */ |
| 46 | - private static function protectNotHtmlLessThanSymbols( $html ) { |
|
| 47 | - preg_match_all( '/<|>/iu', $html, $matches, PREG_OFFSET_CAPTURE ); |
|
| 46 | + private static function protectNotHtmlLessThanSymbols($html) { |
|
| 47 | + preg_match_all('/<|>/iu', $html, $matches, PREG_OFFSET_CAPTURE); |
|
| 48 | 48 | |
| 49 | 49 | $delta = 0; |
| 50 | 50 | $realNextOffset = 0; |
| 51 | 51 | $next = null; |
| 52 | 52 | |
| 53 | - foreach ( $matches[ 0 ] as $key => $match ) { |
|
| 53 | + foreach ($matches[0] as $key => $match) { |
|
| 54 | 54 | |
| 55 | - $current = $matches[ 0 ][ $key ][ 0 ]; |
|
| 55 | + $current = $matches[0][$key][0]; |
|
| 56 | 56 | |
| 57 | - if ( isset( $matches[ 0 ][ $key + 1 ][ 0 ] ) ) { |
|
| 58 | - $next = $matches[ 0 ][ $key + 1 ][ 0 ]; |
|
| 59 | - $nextOffset = $matches[ 0 ][ $key + 1 ][ 1 ]; |
|
| 60 | - $realNextOffset = ( $delta === 0 ) ? $nextOffset : ( $nextOffset + $delta ); |
|
| 57 | + if (isset($matches[0][$key + 1][0])) { |
|
| 58 | + $next = $matches[0][$key + 1][0]; |
|
| 59 | + $nextOffset = $matches[0][$key + 1][1]; |
|
| 60 | + $realNextOffset = ($delta === 0) ? $nextOffset : ($nextOffset + $delta); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $length = strlen( $match[ 0 ] ); |
|
| 64 | - $offset = $matches[ 0 ][ $key ][ 1 ]; |
|
| 65 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
| 63 | + $length = strlen($match[0]); |
|
| 64 | + $offset = $matches[0][$key][1]; |
|
| 65 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
| 66 | 66 | |
| 67 | - if ( $current === '<' && isset( $next ) ) { |
|
| 67 | + if ($current === '<' && isset($next)) { |
|
| 68 | 68 | |
| 69 | 69 | // 1. if next is > or |
| 70 | 70 | // 2. next is < and is not html tag (like < >) |
| 71 | - $insideAngularTags = substr( $html, $realOffset, ( $realNextOffset - $realOffset + 1 ) ); |
|
| 71 | + $insideAngularTags = substr($html, $realOffset, ($realNextOffset - $realOffset + 1)); |
|
| 72 | 72 | |
| 73 | - if ( $next !== '>' || !Strings::isHtmlString( $insideAngularTags ) ) { |
|
| 74 | - $html = substr_replace( $html, self::LT_PLACEHOLDER, $realOffset, $length ); |
|
| 75 | - $delta = $delta + strlen( self::LT_PLACEHOLDER ) - $length; |
|
| 73 | + if ($next !== '>' || !Strings::isHtmlString($insideAngularTags)) { |
|
| 74 | + $html = substr_replace($html, self::LT_PLACEHOLDER, $realOffset, $length); |
|
| 75 | + $delta = $delta + strlen(self::LT_PLACEHOLDER) - $length; |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - return !is_array( $html ) ? $html : implode( $html ); |
|
| 80 | + return !is_array($html) ? $html : implode($html); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
@@ -95,29 +95,29 @@ discard block |
||
| 95 | 95 | * |
| 96 | 96 | * @return string |
| 97 | 97 | */ |
| 98 | - private static function protectNotClosedHtmlTags( $html ) { |
|
| 99 | - preg_match_all( '/<|>/iu', $html, $matches, PREG_OFFSET_CAPTURE ); |
|
| 98 | + private static function protectNotClosedHtmlTags($html) { |
|
| 99 | + preg_match_all('/<|>/iu', $html, $matches, PREG_OFFSET_CAPTURE); |
|
| 100 | 100 | |
| 101 | 101 | $tags = []; |
| 102 | 102 | $offsets = []; |
| 103 | 103 | $originalLengths = []; |
| 104 | 104 | |
| 105 | 105 | // 1. Map all tags |
| 106 | - foreach ( $matches[ 0 ] as $key => $match ) { |
|
| 107 | - $current = $matches[ 0 ][ $key ][ 0 ]; |
|
| 108 | - $currentOffset = $matches[ 0 ][ $key ][ 1 ]; |
|
| 106 | + foreach ($matches[0] as $key => $match) { |
|
| 107 | + $current = $matches[0][$key][0]; |
|
| 108 | + $currentOffset = $matches[0][$key][1]; |
|
| 109 | 109 | |
| 110 | 110 | // check every string inside angular brackets (< and >) |
| 111 | - if ( $current === '<' && isset( $matches[ 0 ][ $key + 1 ][ 0 ] ) && $matches[ 0 ][ $key + 1 ][ 0 ] === '>' ) { |
|
| 112 | - $nextOffset = $matches[ 0 ][ $key + 1 ][ 1 ]; |
|
| 113 | - $tag = substr( $html, ( $currentOffset + 1 ), ( $nextOffset - $currentOffset - 1 ) ); |
|
| 114 | - $trimmedTag = trim( $tag ); |
|
| 111 | + if ($current === '<' && isset($matches[0][$key + 1][0]) && $matches[0][$key + 1][0] === '>') { |
|
| 112 | + $nextOffset = $matches[0][$key + 1][1]; |
|
| 113 | + $tag = substr($html, ($currentOffset + 1), ($nextOffset - $currentOffset - 1)); |
|
| 114 | + $trimmedTag = trim($tag); |
|
| 115 | 115 | |
| 116 | 116 | // if the tag is self closed do nothing |
| 117 | - if ( Strings::lastChar( $tag ) !== '/' ) { |
|
| 117 | + if (Strings::lastChar($tag) !== '/') { |
|
| 118 | 118 | $tags[] = $trimmedTag; |
| 119 | 119 | $offsets[] = $currentOffset; |
| 120 | - $originalLengths[] = strlen( $tag ) + 2; // add 2 to length because there are < and > |
|
| 120 | + $originalLengths[] = strlen($tag) + 2; // add 2 to length because there are < and > |
|
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -125,47 +125,47 @@ discard block |
||
| 125 | 125 | // 2. Removing closed tags |
| 126 | 126 | $indexes = []; |
| 127 | 127 | |
| 128 | - if ( count( $tags ) > 0 ) { |
|
| 129 | - foreach ( $tags as $index => $tag ) { |
|
| 128 | + if (count($tags) > 0) { |
|
| 129 | + foreach ($tags as $index => $tag) { |
|
| 130 | 130 | |
| 131 | - if ( Strings::contains( '/', $tag ) ) { |
|
| 131 | + if (Strings::contains('/', $tag)) { |
|
| 132 | 132 | $complementaryTag = $tag; |
| 133 | 133 | } else { |
| 134 | - $complementaryTag = '/' . explode( ' ', $tag )[ 0 ]; |
|
| 134 | + $complementaryTag = '/' . explode(' ', $tag)[0]; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - $complementaryTagIndex = array_search( $complementaryTag, $tags ); |
|
| 137 | + $complementaryTagIndex = array_search($complementaryTag, $tags); |
|
| 138 | 138 | |
| 139 | - if ( false !== $complementaryTagIndex ) { |
|
| 139 | + if (false !== $complementaryTagIndex) { |
|
| 140 | 140 | $indexes[] = $index; |
| 141 | 141 | $indexes[] = $complementaryTagIndex; |
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - $indexes = array_unique( $indexes ); |
|
| 147 | - foreach ( $indexes as $index ) { |
|
| 148 | - unset( $tags[ $index ] ); |
|
| 146 | + $indexes = array_unique($indexes); |
|
| 147 | + foreach ($indexes as $index) { |
|
| 148 | + unset($tags[$index]); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // 3. Loop not closed tags |
| 152 | 152 | $delta = 0; |
| 153 | 153 | |
| 154 | - if ( count( $tags ) ) { |
|
| 155 | - foreach ( $tags as $index => $tag ) { |
|
| 154 | + if (count($tags)) { |
|
| 155 | + foreach ($tags as $index => $tag) { |
|
| 156 | 156 | |
| 157 | - $length = $originalLengths[ $index ]; |
|
| 158 | - $offset = $offsets[ $index ]; |
|
| 159 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
| 157 | + $length = $originalLengths[$index]; |
|
| 158 | + $offset = $offsets[$index]; |
|
| 159 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
| 160 | 160 | |
| 161 | 161 | $replacement = self::LT_PLACEHOLDER . $tag . self::GT_PLACEHOLDER; |
| 162 | 162 | |
| 163 | - $html = substr_replace( $html, $replacement, $realOffset, $length ); |
|
| 164 | - $delta = $delta + strlen( $replacement ) - $length; |
|
| 163 | + $html = substr_replace($html, $replacement, $realOffset, $length); |
|
| 164 | + $delta = $delta + strlen($replacement) - $length; |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - return !is_array( $html ) ? $html : implode( $html ); |
|
| 168 | + return !is_array($html) ? $html : implode($html); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -174,55 +174,55 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return array |
| 176 | 176 | */ |
| 177 | - private static function extractHtmlNode( $html, $toBeEscaped = false ) { |
|
| 177 | + private static function extractHtmlNode($html, $toBeEscaped = false) { |
|
| 178 | 178 | $pattern = "/<([a-zA-Z0-9._-]+)([^>]|[^<]*?)(([\s]*\/>)|" . |
| 179 | 179 | "(>((([^<]*?|<\!\-\-.*?\-\->)|(?R))*)<\/\\1[\s]*>))/sm"; |
| 180 | - preg_match_all( $pattern, $html, $matches, PREG_OFFSET_CAPTURE ); |
|
| 180 | + preg_match_all($pattern, $html, $matches, PREG_OFFSET_CAPTURE); |
|
| 181 | 181 | |
| 182 | 182 | $elements = []; |
| 183 | 183 | |
| 184 | - foreach ( $matches[ 0 ] as $key => $match ) { |
|
| 184 | + foreach ($matches[0] as $key => $match) { |
|
| 185 | 185 | |
| 186 | - $attributes = isset( $matches[ 2 ][ $key ][ 0 ] ) ? self::getAttributes( $matches[ 2 ][ $key ][ 0 ] ) : []; |
|
| 187 | - $base64Decoded = ( isset( $attributes[ 'equiv-text' ] ) ) ? base64_decode( str_replace( "base64:", "", $attributes[ 'equiv-text' ] ) ) : null; |
|
| 188 | - $tagName = $matches[ 1 ][ $key ][ 0 ]; |
|
| 189 | - $text = !empty( $matches[ 6 ][ $key ][ 0 ] ) ? $matches[ 6 ][ $key ][ 0 ] : ""; |
|
| 186 | + $attributes = isset($matches[2][$key][0]) ? self::getAttributes($matches[2][$key][0]) : []; |
|
| 187 | + $base64Decoded = (isset($attributes['equiv-text'])) ? base64_decode(str_replace("base64:", "", $attributes['equiv-text'])) : null; |
|
| 188 | + $tagName = $matches[1][$key][0]; |
|
| 189 | + $text = !empty($matches[6][$key][0]) ? $matches[6][$key][0] : ""; |
|
| 190 | 190 | $originalText = $text; |
| 191 | - $strippedText = strip_tags( $text ); |
|
| 191 | + $strippedText = strip_tags($text); |
|
| 192 | 192 | |
| 193 | 193 | // get start and end tags |
| 194 | - $explodedNode = explode( self::ORIGINAL_TEXT_PLACEHOLDER, str_replace( $originalText, self::ORIGINAL_TEXT_PLACEHOLDER, $match[ 0 ] ) ); |
|
| 194 | + $explodedNode = explode(self::ORIGINAL_TEXT_PLACEHOLDER, str_replace($originalText, self::ORIGINAL_TEXT_PLACEHOLDER, $match[0])); |
|
| 195 | 195 | |
| 196 | - $start = ( isset( $explodedNode[ 0 ] ) ) ? $explodedNode[ 0 ] : ""; |
|
| 197 | - $end = ( isset( $explodedNode[ 1 ] ) ) ? $explodedNode[ 1 ] : ""; |
|
| 196 | + $start = (isset($explodedNode[0])) ? $explodedNode[0] : ""; |
|
| 197 | + $end = (isset($explodedNode[1])) ? $explodedNode[1] : ""; |
|
| 198 | 198 | |
| 199 | 199 | // inner_html |
| 200 | - $inner_html = self::getInnerHtml( $matches, $key, $toBeEscaped ); |
|
| 200 | + $inner_html = self::getInnerHtml($matches, $key, $toBeEscaped); |
|
| 201 | 201 | |
| 202 | 202 | // node |
| 203 | - $node = self::rebuildNode( $originalText, $toBeEscaped, $start, $end ); |
|
| 203 | + $node = self::rebuildNode($originalText, $toBeEscaped, $start, $end); |
|
| 204 | 204 | |
| 205 | 205 | // terminator |
| 206 | - $terminator = ( $toBeEscaped ) ? '>' : '>'; |
|
| 206 | + $terminator = ($toBeEscaped) ? '>' : '>'; |
|
| 207 | 207 | |
| 208 | 208 | // self closed |
| 209 | - $selfClosed = Strings::contains( '/>', trim( $start ) ); |
|
| 209 | + $selfClosed = Strings::contains('/>', trim($start)); |
|
| 210 | 210 | |
| 211 | 211 | $elements[] = (object)[ |
| 212 | - 'node' => self::restoreLessThanAndGreaterThanSymbols( $node ), |
|
| 213 | - 'start' => self::restoreLessThanAndGreaterThanSymbols( $start ), |
|
| 214 | - 'end' => self::restoreLessThanAndGreaterThanSymbols( $end ), |
|
| 212 | + 'node' => self::restoreLessThanAndGreaterThanSymbols($node), |
|
| 213 | + 'start' => self::restoreLessThanAndGreaterThanSymbols($start), |
|
| 214 | + 'end' => self::restoreLessThanAndGreaterThanSymbols($end), |
|
| 215 | 215 | 'terminator' => $terminator, |
| 216 | - 'offset' => $match[ 1 ], |
|
| 216 | + 'offset' => $match[1], |
|
| 217 | 217 | 'tagname' => $tagName, |
| 218 | 218 | 'attributes' => $attributes, |
| 219 | 219 | 'base64_decoded' => $base64Decoded, |
| 220 | 220 | 'self_closed' => $selfClosed, |
| 221 | - 'omittag' => ( $matches[ 4 ][ $key ][ 1 ] > -1 ), // boolean |
|
| 221 | + 'omittag' => ($matches[4][$key][1] > -1), // boolean |
|
| 222 | 222 | 'inner_html' => $inner_html, |
| 223 | - 'has_children' => is_array( $inner_html ), |
|
| 224 | - 'original_text' => ( $toBeEscaped ) ? self::restoreLessThanAndGreaterThanSymbols( Strings::escapeOnlyHTMLTags( $originalText ) ) : self::restoreLessThanAndGreaterThanSymbols( $originalText ), |
|
| 225 | - 'stripped_text' => self::restoreLessThanAndGreaterThanSymbols( $strippedText ), |
|
| 223 | + 'has_children' => is_array($inner_html), |
|
| 224 | + 'original_text' => ($toBeEscaped) ? self::restoreLessThanAndGreaterThanSymbols(Strings::escapeOnlyHTMLTags($originalText)) : self::restoreLessThanAndGreaterThanSymbols($originalText), |
|
| 225 | + 'stripped_text' => self::restoreLessThanAndGreaterThanSymbols($strippedText), |
|
| 226 | 226 | ]; |
| 227 | 227 | } |
| 228 | 228 | |
@@ -234,8 +234,8 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return string|string[] |
| 236 | 236 | */ |
| 237 | - private static function restoreLessThanAndGreaterThanSymbols( $text ) { |
|
| 238 | - return str_replace( [ self::LT_PLACEHOLDER, self::GT_PLACEHOLDER ], [ '<', '>' ], $text ); |
|
| 237 | + private static function restoreLessThanAndGreaterThanSymbols($text) { |
|
| 238 | + return str_replace([self::LT_PLACEHOLDER, self::GT_PLACEHOLDER], ['<', '>'], $text); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
@@ -246,17 +246,17 @@ discard block |
||
| 246 | 246 | * |
| 247 | 247 | * @return string |
| 248 | 248 | */ |
| 249 | - private static function rebuildNode( $originalText, $toBeEscaped, $start = null, $end = null ) { |
|
| 249 | + private static function rebuildNode($originalText, $toBeEscaped, $start = null, $end = null) { |
|
| 250 | 250 | $node = ''; |
| 251 | 251 | |
| 252 | - if ( !empty( $start ) ) { |
|
| 253 | - $node .= ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $start ) : $start; |
|
| 252 | + if (!empty($start)) { |
|
| 253 | + $node .= ($toBeEscaped) ? Strings::escapeOnlyHTMLTags($start) : $start; |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - $node .= ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $originalText ) : $originalText; |
|
| 256 | + $node .= ($toBeEscaped) ? Strings::escapeOnlyHTMLTags($originalText) : $originalText; |
|
| 257 | 257 | |
| 258 | - if ( !empty( $end ) ) { |
|
| 259 | - $node .= ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $end ) : $end; |
|
| 258 | + if (!empty($end)) { |
|
| 259 | + $node .= ($toBeEscaped) ? Strings::escapeOnlyHTMLTags($end) : $end; |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | return $node; |
@@ -267,16 +267,16 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return mixed |
| 269 | 269 | */ |
| 270 | - public static function getAttributes( $content ) { |
|
| 270 | + public static function getAttributes($content) { |
|
| 271 | 271 | $pattern = '/(.*?)=("|\'|\\\")(.*?)("|\'|\\\"|\\\')/'; |
| 272 | 272 | |
| 273 | - preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ); |
|
| 273 | + preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE); |
|
| 274 | 274 | |
| 275 | 275 | $attributes = []; |
| 276 | 276 | |
| 277 | - if ( isset( $matches[ 1 ] ) && count( $matches[ 1 ] ) > 0 ) { |
|
| 278 | - foreach ( $matches[ 1 ] as $key => $match ) { |
|
| 279 | - $attributes[ trim( $match[ 0 ] ) ] = $matches[ 3 ][ $key ][ 0 ]; |
|
| 277 | + if (isset($matches[1]) && count($matches[1]) > 0) { |
|
| 278 | + foreach ($matches[1] as $key => $match) { |
|
| 279 | + $attributes[trim($match[0])] = $matches[3][$key][0]; |
|
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
@@ -291,11 +291,11 @@ discard block |
||
| 291 | 291 | * |
| 292 | 292 | * @return array|mixed|string |
| 293 | 293 | */ |
| 294 | - private static function getInnerHtml( $matches, $key, $toBeEscaped = false ) { |
|
| 295 | - if ( isset( $matches[ 6 ][ $key ][ 0 ] ) ) { |
|
| 296 | - $node = self::extractHtmlNode( $matches[ 6 ][ $key ][ 0 ], $toBeEscaped ); |
|
| 294 | + private static function getInnerHtml($matches, $key, $toBeEscaped = false) { |
|
| 295 | + if (isset($matches[6][$key][0])) { |
|
| 296 | + $node = self::extractHtmlNode($matches[6][$key][0], $toBeEscaped); |
|
| 297 | 297 | |
| 298 | - return ( !empty( $node ) ) ? $node : $matches[ 6 ][ $key ][ 0 ]; |
|
| 298 | + return (!empty($node)) ? $node : $matches[6][$key][0]; |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | return null; |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * |
| 17 | 17 | * @param array $map |
| 18 | 18 | */ |
| 19 | - public function __construct( array $map = null ) { |
|
| 19 | + public function __construct(array $map = null) { |
|
| 20 | 20 | $this->map = $map; |
| 21 | 21 | } |
| 22 | 22 | |
@@ -31,38 +31,38 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @return string |
| 33 | 33 | */ |
| 34 | - public function replace( $string ) { |
|
| 34 | + public function replace($string) { |
|
| 35 | 35 | // if map is empty |
| 36 | 36 | // or the string has not a dataRef attribute |
| 37 | 37 | // return string as is |
| 38 | - if ( empty( $this->map ) || !$this->hasAnyDataRefAttribute( $string ) ) { |
|
| 38 | + if (empty($this->map) || !$this->hasAnyDataRefAttribute($string)) { |
|
| 39 | 39 | return $string; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | // (recursively) clean string from equiv-text eventually present |
| 43 | - $string = $this->cleanFromEquivText( $string ); |
|
| 43 | + $string = $this->cleanFromEquivText($string); |
|
| 44 | 44 | |
| 45 | - $html = HtmlParser::parse( $string ); |
|
| 45 | + $html = HtmlParser::parse($string); |
|
| 46 | 46 | |
| 47 | 47 | // 1. Replace <ph>|<sc>|<ec> tags |
| 48 | - foreach ( $html as $node ) { |
|
| 49 | - $string = $this->recursiveAddEquivTextToPhTag( $node, $string ); |
|
| 48 | + foreach ($html as $node) { |
|
| 49 | + $string = $this->recursiveAddEquivTextToPhTag($node, $string); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // 2. Replace <pc> tags |
| 53 | - $toBeEscaped = Strings::isAnEscapedHTML( $string ); |
|
| 53 | + $toBeEscaped = Strings::isAnEscapedHTML($string); |
|
| 54 | 54 | |
| 55 | - if ( $this->stringContainsPcTags( $string, $toBeEscaped ) ) { |
|
| 55 | + if ($this->stringContainsPcTags($string, $toBeEscaped)) { |
|
| 56 | 56 | |
| 57 | 57 | // replace self-closed <pc /> |
| 58 | - $string = $this->replaceSelfClosedPcTags( $string, $toBeEscaped ); |
|
| 58 | + $string = $this->replaceSelfClosedPcTags($string, $toBeEscaped); |
|
| 59 | 59 | |
| 60 | 60 | // create a dataRefEnd map |
| 61 | 61 | // (needed for correct handling of </pc> closing tags) |
| 62 | - $dataRefEndMap = $this->buildDataRefEndMap( $html ); |
|
| 63 | - $string = $this->replaceOpeningPcTags( $string, $toBeEscaped ); |
|
| 64 | - $string = $this->replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap ); |
|
| 65 | - $string = ( $toBeEscaped ) ? Strings::escapeOnlyHTMLTags( $string ) : $string; |
|
| 62 | + $dataRefEndMap = $this->buildDataRefEndMap($html); |
|
| 63 | + $string = $this->replaceOpeningPcTags($string, $toBeEscaped); |
|
| 64 | + $string = $this->replaceClosingPcTags($string, $toBeEscaped, $dataRefEndMap); |
|
| 65 | + $string = ($toBeEscaped) ? Strings::escapeOnlyHTMLTags($string) : $string; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | return $string; |
@@ -73,17 +73,17 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return bool |
| 75 | 75 | */ |
| 76 | - private function hasAnyDataRefAttribute( $string ) { |
|
| 76 | + private function hasAnyDataRefAttribute($string) { |
|
| 77 | 77 | $dataRefTags = [ |
| 78 | 78 | 'dataRef', |
| 79 | 79 | 'dataRefStart', |
| 80 | 80 | 'dataRefEnd', |
| 81 | 81 | ]; |
| 82 | 82 | |
| 83 | - foreach ( $dataRefTags as $tag ) { |
|
| 84 | - preg_match( '/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches ); |
|
| 83 | + foreach ($dataRefTags as $tag) { |
|
| 84 | + preg_match('/ ' . $tag . '=[\\\\"](.*?)[\\\\"]/', $string, $matches); |
|
| 85 | 85 | |
| 86 | - if ( count( $matches ) > 0 ) { |
|
| 86 | + if (count($matches) > 0) { |
|
| 87 | 87 | return true; |
| 88 | 88 | } |
| 89 | 89 | } |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | * |
| 95 | 95 | * @return string |
| 96 | 96 | */ |
| 97 | - private function cleanFromEquivText( $string ) { |
|
| 98 | - $html = HtmlParser::parse( $string ); |
|
| 97 | + private function cleanFromEquivText($string) { |
|
| 98 | + $html = HtmlParser::parse($string); |
|
| 99 | 99 | |
| 100 | - foreach ( $html as $node ) { |
|
| 101 | - $string = $this->recursiveCleanFromEquivText( $node, $string ); |
|
| 100 | + foreach ($html as $node) { |
|
| 101 | + $string = $this->recursiveCleanFromEquivText($node, $string); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | return $string; |
@@ -117,57 +117,57 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @return string |
| 119 | 119 | */ |
| 120 | - private function recursiveAddEquivTextToPhTag( $node, $string ) { |
|
| 121 | - if ( $node->has_children ) { |
|
| 122 | - foreach ( $node->inner_html as $childNode ) { |
|
| 123 | - $string = $this->recursiveAddEquivTextToPhTag( $childNode, $string ); |
|
| 120 | + private function recursiveAddEquivTextToPhTag($node, $string) { |
|
| 121 | + if ($node->has_children) { |
|
| 122 | + foreach ($node->inner_html as $childNode) { |
|
| 123 | + $string = $this->recursiveAddEquivTextToPhTag($childNode, $string); |
|
| 124 | 124 | } |
| 125 | 125 | } else { |
| 126 | - if ( $node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec' ) { |
|
| 127 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
| 126 | + if ($node->tagname === 'ph' || $node->tagname === 'sc' || $node->tagname === 'ec') { |
|
| 127 | + if (!isset($node->attributes['dataRef'])) { |
|
| 128 | 128 | return $string; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
| 132 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
| 131 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
| 132 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | // if isset a value in the map calculate base64 encoded value |
| 136 | 136 | // otherwise skip |
| 137 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
| 137 | + if (!in_array($b, array_keys($this->map))) { |
|
| 138 | 138 | return $string; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // check if is null, in this case convert it to NULL string |
| 142 | - if ( is_null( $this->map[ $b ] ) ) { |
|
| 143 | - $this->map[ $b ] = 'NULL'; |
|
| 142 | + if (is_null($this->map[$b])) { |
|
| 143 | + $this->map[$b] = 'NULL'; |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - $value = $this->map[ $b ]; |
|
| 147 | - $base64EncodedValue = base64_encode( $value ); |
|
| 146 | + $value = $this->map[$b]; |
|
| 147 | + $base64EncodedValue = base64_encode($value); |
|
| 148 | 148 | |
| 149 | - if ( empty( $base64EncodedValue ) || $base64EncodedValue === '' ) { |
|
| 149 | + if (empty($base64EncodedValue) || $base64EncodedValue === '') { |
|
| 150 | 150 | return $string; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | // if there is no id copy it from dataRef |
| 154 | - $id = ( !isset( $node->attributes[ 'id' ] ) ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
| 154 | + $id = (!isset($node->attributes['id'])) ? ' id="' . $b . '" removeId="true"' : ''; |
|
| 155 | 155 | |
| 156 | 156 | // introduce dataType for <ec>/<sc> tag handling |
| 157 | - $dataType = ( $this->isAEcOrScTag( $node ) ) ? ' dataType="' . $node->tagname . '"' : ''; |
|
| 157 | + $dataType = ($this->isAEcOrScTag($node)) ? ' dataType="' . $node->tagname . '"' : ''; |
|
| 158 | 158 | |
| 159 | 159 | // replacement |
| 160 | - $d = str_replace( '/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a ); |
|
| 161 | - $a = str_replace( [ '<', '>', '>', '<' ], '', $a ); |
|
| 162 | - $d = str_replace( [ '<', '>', '>', '<' ], '', $d ); |
|
| 160 | + $d = str_replace('/', $id . $dataType . ' equiv-text="base64:' . $base64EncodedValue . '"/', $a); |
|
| 161 | + $a = str_replace(['<', '>', '>', '<'], '', $a); |
|
| 162 | + $d = str_replace(['<', '>', '>', '<'], '', $d); |
|
| 163 | 163 | |
| 164 | 164 | // convert <ec>/<sc> into <ph> |
| 165 | - if ( $this->isAEcOrScTag( $node ) ) { |
|
| 166 | - $d = 'ph' . substr( $d, 2 ); |
|
| 167 | - $d = trim( $d ); |
|
| 165 | + if ($this->isAEcOrScTag($node)) { |
|
| 166 | + $d = 'ph' . substr($d, 2); |
|
| 167 | + $d = trim($d); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - return str_replace( $a, $d, $string ); |
|
| 170 | + return str_replace($a, $d, $string); |
|
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | * |
| 181 | 181 | * @return bool |
| 182 | 182 | */ |
| 183 | - private function stringContainsPcTags( $string, $toBeEscaped ) { |
|
| 184 | - $regex = ( $toBeEscaped ) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
| 185 | - preg_match_all( $regex, $string, $openingPcMatches ); |
|
| 183 | + private function stringContainsPcTags($string, $toBeEscaped) { |
|
| 184 | + $regex = ($toBeEscaped) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
| 185 | + preg_match_all($regex, $string, $openingPcMatches); |
|
| 186 | 186 | |
| 187 | - return ( isset( $openingPcMatches[ 0 ] ) && count( $openingPcMatches[ 0 ] ) > 0 ); |
|
| 187 | + return (isset($openingPcMatches[0]) && count($openingPcMatches[0]) > 0); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -193,28 +193,28 @@ discard block |
||
| 193 | 193 | * |
| 194 | 194 | * @return mixed |
| 195 | 195 | */ |
| 196 | - private function replaceSelfClosedPcTags( $string, $toBeEscaped ) { |
|
| 197 | - if ( $toBeEscaped ) { |
|
| 198 | - $string = str_replace( [ '<', '>' ], [ '<', '>' ], $string ); |
|
| 196 | + private function replaceSelfClosedPcTags($string, $toBeEscaped) { |
|
| 197 | + if ($toBeEscaped) { |
|
| 198 | + $string = str_replace(['<', '>'], ['<', '>'], $string); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | $regex = '/<pc[^>]+?\/>/iu'; |
| 202 | - preg_match_all( $regex, $string, $selfClosedPcMatches ); |
|
| 202 | + preg_match_all($regex, $string, $selfClosedPcMatches); |
|
| 203 | 203 | |
| 204 | - foreach ( $selfClosedPcMatches[ 0 ] as $match ) { |
|
| 204 | + foreach ($selfClosedPcMatches[0] as $match) { |
|
| 205 | 205 | |
| 206 | - $html = HtmlParser::parse( $match ); |
|
| 207 | - $node = $html[ 0 ]; |
|
| 206 | + $html = HtmlParser::parse($match); |
|
| 207 | + $node = $html[0]; |
|
| 208 | 208 | $attributes = $node->attributes; |
| 209 | 209 | |
| 210 | - if ( isset( $attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
|
| 211 | - $replacement = '<ph id="' . $attributes[ 'id' ] . '" dataType="pcSelf" originalData="' . base64_encode( $match ) . '" dataRef="' . $attributes[ 'dataRefStart' ] . '" equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"/>'; |
|
| 212 | - $string = str_replace( $match, $replacement, $string ); |
|
| 210 | + if (isset($attributes['dataRefStart']) && array_key_exists($node->attributes['dataRefStart'], $this->map)) { |
|
| 211 | + $replacement = '<ph id="' . $attributes['id'] . '" dataType="pcSelf" originalData="' . base64_encode($match) . '" dataRef="' . $attributes['dataRefStart'] . '" equiv-text="base64:' . base64_encode($this->map[$node->attributes['dataRefStart']]) . '"/>'; |
|
| 212 | + $string = str_replace($match, $replacement, $string); |
|
| 213 | 213 | } |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - if ( $toBeEscaped ) { |
|
| 217 | - $string = str_replace( [ '<', '>' ], [ '<', '>' ], $string ); |
|
| 216 | + if ($toBeEscaped) { |
|
| 217 | + $string = str_replace(['<', '>'], ['<', '>'], $string); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | return $string; |
@@ -228,12 +228,12 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @return array |
| 230 | 230 | */ |
| 231 | - private function buildDataRefEndMap( $html ) { |
|
| 231 | + private function buildDataRefEndMap($html) { |
|
| 232 | 232 | $dataRefEndMap = []; |
| 233 | 233 | |
| 234 | - foreach ( $html as $index => $node ) { |
|
| 235 | - if ( $node->tagname === 'pc' ) { |
|
| 236 | - $this->extractDataRefMapRecursively( $node, $dataRefEndMap ); |
|
| 234 | + foreach ($html as $index => $node) { |
|
| 235 | + if ($node->tagname === 'pc') { |
|
| 236 | + $this->extractDataRefMapRecursively($node, $dataRefEndMap); |
|
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | |
@@ -246,25 +246,25 @@ discard block |
||
| 246 | 246 | * @param object $node |
| 247 | 247 | * @param $dataRefEndMap |
| 248 | 248 | */ |
| 249 | - private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
|
| 250 | - if ( $this->nodeContainsNestedPcTags( $node ) ) { |
|
| 251 | - foreach ( $node->inner_html as $nestedNode ) { |
|
| 252 | - $this->extractDataRefMapRecursively( $nestedNode, $dataRefEndMap ); |
|
| 249 | + private function extractDataRefMapRecursively($node, &$dataRefEndMap) { |
|
| 250 | + if ($this->nodeContainsNestedPcTags($node)) { |
|
| 251 | + foreach ($node->inner_html as $nestedNode) { |
|
| 252 | + $this->extractDataRefMapRecursively($nestedNode, $dataRefEndMap); |
|
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | // EXCLUDE self closed <pc/> |
| 257 | - if ( $node->tagname === 'pc' && $node->self_closed === false ) { |
|
| 258 | - if ( isset( $node->attributes[ 'dataRefEnd' ] ) ) { |
|
| 259 | - $dataRefEnd = $node->attributes[ 'dataRefEnd' ]; |
|
| 260 | - } elseif ( isset( $node->attributes[ 'dataRefStart' ] ) ) { |
|
| 261 | - $dataRefEnd = $node->attributes[ 'dataRefStart' ]; |
|
| 257 | + if ($node->tagname === 'pc' && $node->self_closed === false) { |
|
| 258 | + if (isset($node->attributes['dataRefEnd'])) { |
|
| 259 | + $dataRefEnd = $node->attributes['dataRefEnd']; |
|
| 260 | + } elseif (isset($node->attributes['dataRefStart'])) { |
|
| 261 | + $dataRefEnd = $node->attributes['dataRefStart']; |
|
| 262 | 262 | } else { |
| 263 | 263 | $dataRefEnd = null; |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | $dataRefEndMap[] = [ |
| 267 | - 'id' => isset( $node->attributes[ 'id' ] ) ? $node->attributes[ 'id' ] : null, |
|
| 267 | + 'id' => isset($node->attributes['id']) ? $node->attributes['id'] : null, |
|
| 268 | 268 | 'dataRefEnd' => $dataRefEnd, |
| 269 | 269 | ]; |
| 270 | 270 | } |
@@ -276,15 +276,15 @@ discard block |
||
| 276 | 276 | * |
| 277 | 277 | * @return string|string[] |
| 278 | 278 | */ |
| 279 | - private function recursiveCleanFromEquivText( $node, $string ) { |
|
| 280 | - if ( $node->has_children ) { |
|
| 281 | - foreach ( $node->inner_html as $childNode ) { |
|
| 282 | - $string = $this->recursiveCleanFromEquivText( $childNode, $string ); |
|
| 279 | + private function recursiveCleanFromEquivText($node, $string) { |
|
| 280 | + if ($node->has_children) { |
|
| 281 | + foreach ($node->inner_html as $childNode) { |
|
| 282 | + $string = $this->recursiveCleanFromEquivText($childNode, $string); |
|
| 283 | 283 | } |
| 284 | 284 | } else { |
| 285 | - if ( isset( $node->attributes[ 'dataRef' ] ) && array_key_exists( $node->attributes[ 'dataRef' ], $this->map ) ) { |
|
| 286 | - $cleaned = preg_replace( '/ equiv-text="(.*?)"/', '', $node->node ); |
|
| 287 | - $string = str_replace( $node->node, $cleaned, $string ); |
|
| 285 | + if (isset($node->attributes['dataRef']) && array_key_exists($node->attributes['dataRef'], $this->map)) { |
|
| 286 | + $cleaned = preg_replace('/ equiv-text="(.*?)"/', '', $node->node); |
|
| 287 | + $string = str_replace($node->node, $cleaned, $string); |
|
| 288 | 288 | } |
| 289 | 289 | } |
| 290 | 290 | |
@@ -299,35 +299,35 @@ discard block |
||
| 299 | 299 | * |
| 300 | 300 | * @return string |
| 301 | 301 | */ |
| 302 | - private function replaceOpeningPcTags( $string, $toBeEscaped ) { |
|
| 303 | - $regex = ( $toBeEscaped ) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
| 304 | - preg_match_all( $regex, $string, $openingPcMatches ); |
|
| 302 | + private function replaceOpeningPcTags($string, $toBeEscaped) { |
|
| 303 | + $regex = ($toBeEscaped) ? '/<pc (.*?)>/iu' : '/<pc (.*?)>/iu'; |
|
| 304 | + preg_match_all($regex, $string, $openingPcMatches); |
|
| 305 | 305 | |
| 306 | - foreach ( $openingPcMatches[ 0 ] as $index => $match ) { |
|
| 307 | - $attr = HtmlParser::getAttributes( $openingPcMatches[ 1 ][ $index ] ); |
|
| 306 | + foreach ($openingPcMatches[0] as $index => $match) { |
|
| 307 | + $attr = HtmlParser::getAttributes($openingPcMatches[1][$index]); |
|
| 308 | 308 | |
| 309 | 309 | // CASE 1 - Missing `dataRefStart` |
| 310 | - if ( isset( $attr[ 'dataRefEnd' ] ) && !isset( $attr[ 'dataRefStart' ] ) ) { |
|
| 311 | - $attr[ 'dataRefStart' ] = $attr[ 'dataRefEnd' ]; |
|
| 310 | + if (isset($attr['dataRefEnd']) && !isset($attr['dataRefStart'])) { |
|
| 311 | + $attr['dataRefStart'] = $attr['dataRefEnd']; |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | // CASE 2 - Missing `dataRefEnd` |
| 315 | - if ( isset( $attr[ 'dataRefStart' ] ) && !isset( $attr[ 'dataRefEnd' ] ) ) { |
|
| 316 | - $attr[ 'dataRefEnd' ] = $attr[ 'dataRefStart' ]; |
|
| 315 | + if (isset($attr['dataRefStart']) && !isset($attr['dataRefEnd'])) { |
|
| 316 | + $attr['dataRefEnd'] = $attr['dataRefStart']; |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - if ( isset( $attr[ 'dataRefStart' ] ) ) { |
|
| 319 | + if (isset($attr['dataRefStart'])) { |
|
| 320 | 320 | $startOriginalData = $match; // opening <pc> |
| 321 | - $startValue = $this->map[ $attr[ 'dataRefStart' ] ] ? $this->map[ $attr[ 'dataRefStart' ] ] : 'NULL'; //handling null values in original data map |
|
| 322 | - $base64EncodedStartValue = base64_encode( $startValue ); |
|
| 323 | - $base64StartOriginalData = base64_encode( $startOriginalData ); |
|
| 321 | + $startValue = $this->map[$attr['dataRefStart']] ? $this->map[$attr['dataRefStart']] : 'NULL'; //handling null values in original data map |
|
| 322 | + $base64EncodedStartValue = base64_encode($startValue); |
|
| 323 | + $base64StartOriginalData = base64_encode($startOriginalData); |
|
| 324 | 324 | |
| 325 | 325 | // conversion for opening <pc> tag |
| 326 | - $openingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_1"' : '' ) . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
| 327 | - . $attr[ 'dataRefStart' ] . '" equiv-text="base64:' |
|
| 326 | + $openingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_1"' : '') . ' dataType="pcStart" originalData="' . $base64StartOriginalData . '" dataRef="' |
|
| 327 | + . $attr['dataRefStart'] . '" equiv-text="base64:' |
|
| 328 | 328 | . $base64EncodedStartValue . '"/>'; |
| 329 | 329 | |
| 330 | - $string = str_replace( $startOriginalData, $openingPcConverted, $string ); |
|
| 330 | + $string = str_replace($startOriginalData, $openingPcConverted, $string); |
|
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | |
@@ -344,34 +344,34 @@ discard block |
||
| 344 | 344 | * |
| 345 | 345 | * @return string |
| 346 | 346 | */ |
| 347 | - private function replaceClosingPcTags( $string, $toBeEscaped, $dataRefEndMap = [] ) { |
|
| 348 | - $regex = ( $toBeEscaped ) ? '/<\/pc>/iu' : '/<\/pc>/iu'; |
|
| 349 | - preg_match_all( $regex, $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
|
| 347 | + private function replaceClosingPcTags($string, $toBeEscaped, $dataRefEndMap = []) { |
|
| 348 | + $regex = ($toBeEscaped) ? '/<\/pc>/iu' : '/<\/pc>/iu'; |
|
| 349 | + preg_match_all($regex, $string, $closingPcMatches, PREG_OFFSET_CAPTURE); |
|
| 350 | 350 | $delta = 0; |
| 351 | 351 | |
| 352 | - foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
|
| 353 | - $offset = $match[ 1 ]; |
|
| 354 | - $length = strlen( $match[ 0 ] ); |
|
| 355 | - $attr = $dataRefEndMap[ $index ]; |
|
| 352 | + foreach ($closingPcMatches[0] as $index => $match) { |
|
| 353 | + $offset = $match[1]; |
|
| 354 | + $length = strlen($match[0]); |
|
| 355 | + $attr = $dataRefEndMap[$index]; |
|
| 356 | 356 | |
| 357 | - if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
|
| 358 | - $endOriginalData = $match[ 0 ]; // </pc> |
|
| 359 | - $endValue = $this->map[ $attr[ 'dataRefEnd' ] ] ?: 'NULL'; |
|
| 360 | - $base64EncodedEndValue = base64_encode( $endValue ); |
|
| 361 | - $base64EndOriginalData = base64_encode( $endOriginalData ); |
|
| 357 | + if (!empty($attr) && isset($attr['dataRefEnd'])) { |
|
| 358 | + $endOriginalData = $match[0]; // </pc> |
|
| 359 | + $endValue = $this->map[$attr['dataRefEnd']] ?: 'NULL'; |
|
| 360 | + $base64EncodedEndValue = base64_encode($endValue); |
|
| 361 | + $base64EndOriginalData = base64_encode($endOriginalData); |
|
| 362 | 362 | |
| 363 | 363 | // conversion for closing <pc> tag |
| 364 | - $closingPcConverted = '<ph ' . ( ( isset( $attr[ 'id' ] ) ) ? 'id="' . $attr[ 'id' ] . '_2"' : '' ) . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
| 365 | - . $attr[ 'dataRefEnd' ] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
| 364 | + $closingPcConverted = '<ph ' . ((isset($attr['id'])) ? 'id="' . $attr['id'] . '_2"' : '') . ' dataType="pcEnd" originalData="' . $base64EndOriginalData . '" dataRef="' |
|
| 365 | + . $attr['dataRefEnd'] . '" equiv-text="base64:' . $base64EncodedEndValue . '"/>'; |
|
| 366 | 366 | |
| 367 | - $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
|
| 367 | + $realOffset = ($delta === 0) ? $offset : ($offset + $delta); |
|
| 368 | 368 | |
| 369 | - $string = substr_replace( $string, $closingPcConverted, $realOffset, $length ); |
|
| 370 | - $delta = $delta + strlen( $closingPcConverted ) - $length; |
|
| 369 | + $string = substr_replace($string, $closingPcConverted, $realOffset, $length); |
|
| 370 | + $delta = $delta + strlen($closingPcConverted) - $length; |
|
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | - return !is_array( $string ) ? $string : implode( $string ); |
|
| 374 | + return !is_array($string) ? $string : implode($string); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -379,13 +379,13 @@ discard block |
||
| 379 | 379 | * |
| 380 | 380 | * @return bool |
| 381 | 381 | */ |
| 382 | - private function nodeContainsNestedPcTags( $node ) { |
|
| 383 | - if ( !$node->has_children ) { |
|
| 382 | + private function nodeContainsNestedPcTags($node) { |
|
| 383 | + if (!$node->has_children) { |
|
| 384 | 384 | return false; |
| 385 | 385 | } |
| 386 | 386 | |
| 387 | - foreach ( $node->inner_html as $nestedNode ) { |
|
| 388 | - if ( $nestedNode->tagname === 'pc' && ( isset( $node->attributes[ 'dataRefEnd' ] ) || isset( $node->attributes[ 'dataRefStart' ] ) ) ) { |
|
| 387 | + foreach ($node->inner_html as $nestedNode) { |
|
| 388 | + if ($nestedNode->tagname === 'pc' && (isset($node->attributes['dataRefEnd']) || isset($node->attributes['dataRefStart']))) { |
|
| 389 | 389 | return true; |
| 390 | 390 | } |
| 391 | 391 | } |
@@ -398,18 +398,18 @@ discard block |
||
| 398 | 398 | * |
| 399 | 399 | * @return string |
| 400 | 400 | */ |
| 401 | - public function restore( $string ) { |
|
| 401 | + public function restore($string) { |
|
| 402 | 402 | // if map is empty return string as is |
| 403 | - if ( empty( $this->map ) ) { |
|
| 403 | + if (empty($this->map)) { |
|
| 404 | 404 | return $string; |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | // replace eventual empty equiv-text="" |
| 408 | - $string = str_replace( ' equiv-text=""', '', $string ); |
|
| 409 | - $html = HtmlParser::parse( $string ); |
|
| 408 | + $string = str_replace(' equiv-text=""', '', $string); |
|
| 409 | + $html = HtmlParser::parse($string); |
|
| 410 | 410 | |
| 411 | - foreach ( $html as $node ) { |
|
| 412 | - $string = $this->recursiveRemoveOriginalData( $node, $string ); |
|
| 411 | + foreach ($html as $node) { |
|
| 412 | + $string = $this->recursiveRemoveOriginalData($node, $string); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | return $string; |
@@ -421,76 +421,76 @@ discard block |
||
| 421 | 421 | * |
| 422 | 422 | * @return string|string[] |
| 423 | 423 | */ |
| 424 | - private function recursiveRemoveOriginalData( $node, $string ) { |
|
| 425 | - if ( $node->has_children ) { |
|
| 426 | - foreach ( $node->inner_html as $childNode ) { |
|
| 427 | - $string = $this->recursiveRemoveOriginalData( $childNode, $string ); |
|
| 424 | + private function recursiveRemoveOriginalData($node, $string) { |
|
| 425 | + if ($node->has_children) { |
|
| 426 | + foreach ($node->inner_html as $childNode) { |
|
| 427 | + $string = $this->recursiveRemoveOriginalData($childNode, $string); |
|
| 428 | 428 | } |
| 429 | 429 | } else { |
| 430 | 430 | |
| 431 | - if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
|
| 431 | + if (!isset($node->attributes['dataRef'])) { |
|
| 432 | 432 | return $string; |
| 433 | 433 | } |
| 434 | 434 | |
| 435 | - $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
| 436 | - $b = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
|
| 437 | - $c = $node->terminator; // terminator: Eg: > |
|
| 435 | + $a = $node->node; // complete match. Eg: <ph id="source1" dataRef="source1"/> |
|
| 436 | + $b = $node->attributes['dataRef']; // map identifier. Eg: source1 |
|
| 437 | + $c = $node->terminator; // terminator: Eg: > |
|
| 438 | 438 | |
| 439 | 439 | // if isset a value in the map calculate base64 encoded value |
| 440 | 440 | // or it is an empty string |
| 441 | 441 | // otherwise skip |
| 442 | - if ( !in_array( $b, array_keys( $this->map ) ) ) { |
|
| 442 | + if (!in_array($b, array_keys($this->map))) { |
|
| 443 | 443 | return $string; |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | // check if is null, in this case convert it to NULL string |
| 447 | - if ( is_null( $this->map[ $b ] ) ) { |
|
| 448 | - $this->map[ $b ] = 'NULL'; |
|
| 447 | + if (is_null($this->map[$b])) { |
|
| 448 | + $this->map[$b] = 'NULL'; |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | // remove id? |
| 452 | - $removeId = ( isset( $node->attributes[ 'removeId' ] ) && $node->attributes[ 'removeId' ] === "true" ) ? ' id="' . $b . '" removeId="true"' : ''; |
|
| 452 | + $removeId = (isset($node->attributes['removeId']) && $node->attributes['removeId'] === "true") ? ' id="' . $b . '" removeId="true"' : ''; |
|
| 453 | 453 | |
| 454 | 454 | // grab dataType attribute for <ec>/<sc> tag handling |
| 455 | - $dataType = ( $this->wasAEcOrScTag( $node ) ) ? ' dataType="' . $node->attributes[ 'dataType' ] . '"' : ''; |
|
| 455 | + $dataType = ($this->wasAEcOrScTag($node)) ? ' dataType="' . $node->attributes['dataType'] . '"' : ''; |
|
| 456 | 456 | |
| 457 | - $d = str_replace( $removeId . $dataType . ' equiv-text="base64:' . base64_encode( $this->map[ $b ] ) . '"/' . $c, '/' . $c, $a ); |
|
| 457 | + $d = str_replace($removeId . $dataType . ' equiv-text="base64:' . base64_encode($this->map[$b]) . '"/' . $c, '/' . $c, $a); |
|
| 458 | 458 | |
| 459 | 459 | // replace original <ec>/<sc> tag |
| 460 | - if ( $this->wasAEcOrScTag( $node ) ) { |
|
| 461 | - $d = $node->attributes[ 'dataType' ] . substr( $d, 3 ); |
|
| 462 | - $d = trim( $d ); |
|
| 460 | + if ($this->wasAEcOrScTag($node)) { |
|
| 461 | + $d = $node->attributes['dataType'] . substr($d, 3); |
|
| 462 | + $d = trim($d); |
|
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | // replace only content tag, no matter if the string is encoded or not |
| 466 | 466 | // in this way we can handle string with mixed tags (encoded and not-encoded) |
| 467 | 467 | // in the same string |
| 468 | - $a = $this->purgeTags( $a ); |
|
| 469 | - $d = $this->purgeTags( $d ); |
|
| 468 | + $a = $this->purgeTags($a); |
|
| 469 | + $d = $this->purgeTags($d); |
|
| 470 | 470 | |
| 471 | - $string = str_replace( $a, $d, $string ); |
|
| 471 | + $string = str_replace($a, $d, $string); |
|
| 472 | 472 | |
| 473 | 473 | // restoring <pc/> self-closed here |
| 474 | - if ( Strings::contains( 'dataType="pcSelf"', $d ) ) { |
|
| 475 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
| 474 | + if (Strings::contains('dataType="pcSelf"', $d)) { |
|
| 475 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
| 476 | 476 | |
| 477 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
| 478 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
| 479 | - $originalData = $this->purgeTags( $originalData ); |
|
| 480 | - $string = str_replace( $d, $originalData, $string ); |
|
| 477 | + if (isset($originalDataMatches[1])) { |
|
| 478 | + $originalData = base64_decode($originalDataMatches[1]); |
|
| 479 | + $originalData = $this->purgeTags($originalData); |
|
| 480 | + $string = str_replace($d, $originalData, $string); |
|
| 481 | 481 | } |
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | // restoring <pc> tags here |
| 485 | 485 | // if <ph> tag has originalData and originalType is pcStart or pcEnd, |
| 486 | 486 | // replace with original data |
| 487 | - if ( Strings::contains( 'dataType="pcStart"', $d ) || Strings::contains( 'dataType="pcEnd"', $d ) ) { |
|
| 488 | - preg_match( '/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches ); |
|
| 487 | + if (Strings::contains('dataType="pcStart"', $d) || Strings::contains('dataType="pcEnd"', $d)) { |
|
| 488 | + preg_match('/\s?originalData="(.*?)"\s?/', $d, $originalDataMatches); |
|
| 489 | 489 | |
| 490 | - if ( isset( $originalDataMatches[ 1 ] ) ) { |
|
| 491 | - $originalData = base64_decode( $originalDataMatches[ 1 ] ); |
|
| 492 | - $originalData = $this->purgeTags( $originalData ); |
|
| 493 | - $string = str_replace( $d, $originalData, $string ); |
|
| 490 | + if (isset($originalDataMatches[1])) { |
|
| 491 | + $originalData = base64_decode($originalDataMatches[1]); |
|
| 492 | + $originalData = $this->purgeTags($originalData); |
|
| 493 | + $string = str_replace($d, $originalData, $string); |
|
| 494 | 494 | } |
| 495 | 495 | } |
| 496 | 496 | } |
@@ -503,8 +503,8 @@ discard block |
||
| 503 | 503 | * |
| 504 | 504 | * @return string |
| 505 | 505 | */ |
| 506 | - private function purgeTags( $string ) { |
|
| 507 | - return str_replace( [ '<', '>', '<', '>' ], '', $string ); |
|
| 506 | + private function purgeTags($string) { |
|
| 507 | + return str_replace(['<', '>', '<', '>'], '', $string); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | /** |
@@ -514,8 +514,8 @@ discard block |
||
| 514 | 514 | * |
| 515 | 515 | * @return bool |
| 516 | 516 | */ |
| 517 | - private function isAEcOrScTag( $node ) { |
|
| 518 | - return ( $node->tagname === 'ec' || $node->tagname === 'sc' ); |
|
| 517 | + private function isAEcOrScTag($node) { |
|
| 518 | + return ($node->tagname === 'ec' || $node->tagname === 'sc'); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | /** |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | * |
| 527 | 527 | * @return bool |
| 528 | 528 | */ |
| 529 | - private function wasAEcOrScTag( $node ) { |
|
| 530 | - return ( isset( $node->attributes[ 'dataType' ] ) && ( $node->attributes[ 'dataType' ] === 'ec' || $node->attributes[ 'dataType' ] === 'sc' ) ); |
|
| 529 | + private function wasAEcOrScTag($node) { |
|
| 530 | + return (isset($node->attributes['dataType']) && ($node->attributes['dataType'] === 'ec' || $node->attributes['dataType'] === 'sc')); |
|
| 531 | 531 | } |
| 532 | 532 | } |