diff --git a/src/Commons/Constants.php b/src/Enum/ConstantEnum.php similarity index 90% rename from src/Commons/Constants.php rename to src/Enum/ConstantEnum.php index 7c16c26..c1e5bab 100644 --- a/src/Commons/Constants.php +++ b/src/Enum/ConstantEnum.php @@ -1,9 +1,9 @@ \s]+?\}/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $snail_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $snail_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $snail_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $snail_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/DoubleSquareBrackets.php b/src/Filters/DoubleSquareBrackets.php index c5da671..e47d125 100644 --- a/src/Filters/DoubleSquareBrackets.php +++ b/src/Filters/DoubleSquareBrackets.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class DoubleSquareBrackets extends AbstractHandler { @@ -14,7 +14,7 @@ public function transform( $segment ) { preg_match_all( '/\[\[[^<>\s]+?\]\]/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $snail_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $snail_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $snail_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $snail_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/DoubleUnderscore.php b/src/Filters/DoubleUnderscore.php index f370cb2..2c9d081 100644 --- a/src/Filters/DoubleUnderscore.php +++ b/src/Filters/DoubleUnderscore.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class DoubleUnderscore extends AbstractHandler { @@ -14,7 +14,7 @@ public function transform( $segment ) { preg_match_all( '/__[^<>\s]+?__/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $snail_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $snail_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $snail_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $snail_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/EncodeToRawXML.php b/src/Filters/EncodeToRawXML.php index 4c26eae..73a6d4f 100644 --- a/src/Filters/EncodeToRawXML.php +++ b/src/Filters/EncodeToRawXML.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Utils\CatUtils; class EncodeToRawXML extends AbstractHandler { @@ -26,7 +26,7 @@ public function transform( $segment ) { $segment = preg_replace_callback( '/([\xF0-\xF7]...)/s', [ CatUtils::class, 'htmlentitiesFromUnicode' ], $segment ); // now convert the real   - $segment = str_replace( Constants::nbspPlaceholder, CatUtils::unicode2chr( 0Xa0 ), $segment ); + $segment = str_replace( ConstantEnum::nbspPlaceholder, CatUtils::unicode2chr( 0Xa0 ), $segment ); // handling if ( strpos( $segment, '##_ent_0D_##' ) !== false ) { diff --git a/src/Filters/FromLayer2ToRawXML.php b/src/Filters/FromLayer2ToRawXML.php index 657648b..6f58510 100644 --- a/src/Filters/FromLayer2ToRawXML.php +++ b/src/Filters/FromLayer2ToRawXML.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Utils\CatUtils; /** @@ -29,7 +29,7 @@ public function transform( $segment ) { $segment = $this->placeHoldBrokenHTML( $segment ); $double_encode = false; - if ( strpos( $segment, Constants::LTPLACEHOLDER ) !== false ) { + if ( strpos( $segment, ConstantEnum::LTPLACEHOLDER ) !== false ) { $decXliff = new RestoreXliffTagsContent(); $test_segment = $decXliff->transform( $segment ); @@ -68,7 +68,7 @@ public function transform( $segment ) { $segment = preg_replace_callback( '/([\xF0-\xF7]...)/s', [ CatUtils::class, 'htmlentitiesFromUnicode' ], $segment ); // now convert the real   - $segment = str_replace( Constants::nbspPlaceholder, CatUtils::unicode2chr( 0Xa0 ), $segment ); + $segment = str_replace( ConstantEnum::nbspPlaceholder, CatUtils::unicode2chr( 0Xa0 ), $segment ); // Filters BUG, segmentation on HTML, we should never get this at this level ( Should be fixed, anyway we try to cover ) $segment = $this->resetBrokenHTML( $segment ); diff --git a/src/Filters/HtmlToPh.php b/src/Filters/HtmlToPh.php index d83ac4e..309f235 100644 --- a/src/Filters/HtmlToPh.php +++ b/src/Filters/HtmlToPh.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; use Matecat\SubFiltering\Filters\Html\CallbacksHandler; use Matecat\SubFiltering\Filters\Html\HtmlParser; @@ -117,7 +117,7 @@ protected function _isTagValid( $buffer ) { //EX: //original: <a href=\"\"> // - if ( strpos( $buffer, Constants::LTPLACEHOLDER ) !== false || strpos( $buffer, Constants::GTPLACEHOLDER ) !== false ) { + if ( strpos( $buffer, ConstantEnum::LTPLACEHOLDER ) !== false || strpos( $buffer, ConstantEnum::GTPLACEHOLDER ) !== false ) { return false; } diff --git a/src/Filters/Percentages.php b/src/Filters/Percentages.php index 981abba..313f68e 100644 --- a/src/Filters/Percentages.php +++ b/src/Filters/Percentages.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class Percentages extends AbstractHandler { @@ -31,7 +31,7 @@ public function transform( $segment ) { preg_match_all( '/%%[^<>\s%]+?%%/', $segment, $html, PREG_SET_ORDER ); // removed single percentage support '/(?\s%]+?%/' foreach ( $html as $pos => $percentage_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $percentage_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $percentage_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $percentage_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/PlaceHoldXliffTags.php b/src/Filters/PlaceHoldXliffTags.php index 608626c..12334d5 100644 --- a/src/Filters/PlaceHoldXliffTags.php +++ b/src/Filters/PlaceHoldXliffTags.php @@ -9,9 +9,8 @@ namespace Matecat\SubFiltering\Filters; - use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class PlaceHoldXliffTags extends AbstractHandler { @@ -22,35 +21,35 @@ public function transform( $segment ) { //remove not existent tags $segment = preg_replace( '|()|si', "", $segment ); - //$segment=preg_replace('|<(g\s*.*?)>|si', Constants::LTPLACEHOLDER."$1".Constants::GTPLACEHOLDER,$segment); - $segment = preg_replace( '|<(g\s*id=["\']+.*?["\']+\s*[^<>]*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(g\s*xid=["\']+.*?["\']+\s*[^<>]*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - - $segment = preg_replace( '|<(/g)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - - $segment = preg_replace( '|<(x .*?/?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '#<(bx[ ]{0,}/?|bx .*?/?)>#si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '#<(ex[ ]{0,}/? |ex .*?/?)>#si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(bpt\s*.*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/bpt)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(ept\s*.*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/ept)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(ph .*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/ph)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(ec .*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/ec)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(sc .*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/sc)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(pc .*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/pc)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(it .*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/it)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(mrk\s*.*?)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - $segment = preg_replace( '|<(/mrk)>|si', Constants::LTPLACEHOLDER . "$1" . Constants::GTPLACEHOLDER, $segment ); - - return preg_replace_callback( '/' . Constants::LTPLACEHOLDER . '(.*?)' . Constants::GTPLACEHOLDER . '/u', + //$segment=preg_replace('|<(g\s*.*?)>|si', ConstantEnum::LTPLACEHOLDER."$1".ConstantEnum::GTPLACEHOLDER,$segment); + $segment = preg_replace( '|<(g\s*id=["\']+.*?["\']+\s*[^<>]*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(g\s*xid=["\']+.*?["\']+\s*[^<>]*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + + $segment = preg_replace( '|<(/g)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + + $segment = preg_replace( '|<(x .*?/?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '#<(bx[ ]{0,}/?|bx .*?/?)>#si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '#<(ex[ ]{0,}/? |ex .*?/?)>#si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(bpt\s*.*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/bpt)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(ept\s*.*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/ept)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(ph .*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/ph)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(ec .*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/ec)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(sc .*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/sc)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(pc .*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/pc)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(it .*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/it)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(mrk\s*.*?)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + $segment = preg_replace( '|<(/mrk)>|si', ConstantEnum::LTPLACEHOLDER . "$1" . ConstantEnum::GTPLACEHOLDER, $segment ); + + return preg_replace_callback( '/' . ConstantEnum::LTPLACEHOLDER . '(.*?)' . ConstantEnum::GTPLACEHOLDER . '/u', function ( $matches ) { - return Constants::LTPLACEHOLDER . base64_encode( $matches[ 1 ] ) . Constants::GTPLACEHOLDER; + return ConstantEnum::LTPLACEHOLDER . base64_encode( $matches[ 1 ] ) . ConstantEnum::GTPLACEHOLDER; }, $segment ); //base64 of the tag content to avoid unwanted manipulation } diff --git a/src/Filters/RestoreEquivTextPhToXliffOriginal.php b/src/Filters/RestoreEquivTextPhToXliffOriginal.php index 896b5c1..7ea2d34 100644 --- a/src/Filters/RestoreEquivTextPhToXliffOriginal.php +++ b/src/Filters/RestoreEquivTextPhToXliffOriginal.php @@ -11,7 +11,7 @@ use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreEquivTextPhToXliffOriginal extends AbstractHandler { @@ -23,7 +23,7 @@ class RestoreEquivTextPhToXliffOriginal extends AbstractHandler { public function transform( $segment ) { //pipeline to convert back XLIFF PH to the original ones - preg_match_all( '/' . Constants::LTPLACEHOLDER . 'ph id\s*=\s*[\'"](?!mtc_).*?[\'"] ctype="x-twig" equiv-text\s*=\s*[\'"]base64:([^"\']+?)[\'"]\s*\/' . Constants::GTPLACEHOLDER . '/', $segment, $html, PREG_SET_ORDER ); // Ungreedy + preg_match_all( '/' . ConstantEnum::LTPLACEHOLDER . 'ph id\s*=\s*[\'"](?!mtc_).*?[\'"] ctype="x-twig" equiv-text\s*=\s*[\'"]base64:([^"\']+?)[\'"]\s*\/' . ConstantEnum::GTPLACEHOLDER . '/', $segment, $html, PREG_SET_ORDER ); // Ungreedy foreach ( $html as $tag_attribute ) { $segment = str_replace( "base64:" . $tag_attribute[ 1 ], base64_decode( $tag_attribute[ 1 ] ), $segment ); } diff --git a/src/Filters/RestorePlaceHoldersToXLIFFLtGt.php b/src/Filters/RestorePlaceHoldersToXLIFFLtGt.php index 03cc794..5ced1cf 100644 --- a/src/Filters/RestorePlaceHoldersToXLIFFLtGt.php +++ b/src/Filters/RestorePlaceHoldersToXLIFFLtGt.php @@ -11,13 +11,13 @@ use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestorePlaceHoldersToXLIFFLtGt extends AbstractHandler { public function transform( $segment ) { - $segment = str_replace( Constants::LTPLACEHOLDER, "<", $segment ); - $segment = str_replace( Constants::GTPLACEHOLDER, ">", $segment ); + $segment = str_replace( ConstantEnum::LTPLACEHOLDER, "<", $segment ); + $segment = str_replace( ConstantEnum::GTPLACEHOLDER, ">", $segment ); return $segment; diff --git a/src/Filters/RestoreSubFilteredPhToHtml.php b/src/Filters/RestoreSubFilteredPhToHtml.php index fad7233..11239c8 100644 --- a/src/Filters/RestoreSubFilteredPhToHtml.php +++ b/src/Filters/RestoreSubFilteredPhToHtml.php @@ -10,11 +10,11 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreSubFilteredPhToHtml extends AbstractHandler { - const matchPhRegexp = '|' . Constants::LTPLACEHOLDER . 'ph id\s*=\s*["\']mtc_[0-9]+["\'] equiv-text\s*=\s*["\']base64:([^"\']+)["\']\s*/' . Constants::GTPLACEHOLDER . '|siU'; + const matchPhRegexp = '|' . ConstantEnum::LTPLACEHOLDER . 'ph id\s*=\s*["\']mtc_[0-9]+["\'] equiv-text\s*=\s*["\']base64:([^"\']+)["\']\s*/' . ConstantEnum::GTPLACEHOLDER . '|siU'; /** * @param $segment diff --git a/src/Filters/RestoreTabsPlaceholders.php b/src/Filters/RestoreTabsPlaceholders.php index 929d544..e9ba24b 100644 --- a/src/Filters/RestoreTabsPlaceholders.php +++ b/src/Filters/RestoreTabsPlaceholders.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreTabsPlaceholders extends AbstractHandler { @@ -17,7 +17,7 @@ public function transform( $segment ) { // | Esempio test | Test example | // +-------------------+--------------------+ // - return str_replace( [ " ", " " ], Constants::tabPlaceholder, $segment ); + return str_replace( [ " ", " " ], ConstantEnum::tabPlaceholder, $segment ); } } \ No newline at end of file diff --git a/src/Filters/RestoreXliffTagsContent.php b/src/Filters/RestoreXliffTagsContent.php index 6c1abe1..38e1c79 100644 --- a/src/Filters/RestoreXliffTagsContent.php +++ b/src/Filters/RestoreXliffTagsContent.php @@ -11,17 +11,17 @@ use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreXliffTagsContent extends AbstractHandler { public function transform( $segment ) { - $segment = preg_replace_callback( '/' . Constants::LTPLACEHOLDER . '(.*?)' . Constants::GTPLACEHOLDER . '/u', + $segment = preg_replace_callback( '/' . ConstantEnum::LTPLACEHOLDER . '(.*?)' . ConstantEnum::GTPLACEHOLDER . '/u', function ( $matches ) { $_match = base64_decode( $matches[ 1 ] ); - return Constants::LTPLACEHOLDER . $_match . Constants::GTPLACEHOLDER; + return ConstantEnum::LTPLACEHOLDER . $_match . ConstantEnum::GTPLACEHOLDER; }, $segment ); //base64 decode of the tag content to avoid unwanted manipulation diff --git a/src/Filters/RestoreXliffTagsForView.php b/src/Filters/RestoreXliffTagsForView.php index 6d19b2f..1747c0a 100644 --- a/src/Filters/RestoreXliffTagsForView.php +++ b/src/Filters/RestoreXliffTagsForView.php @@ -9,19 +9,18 @@ namespace Matecat\SubFiltering\Filters; - use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreXliffTagsForView extends AbstractHandler { public function transform( $segment ) { - $segment = preg_replace_callback( '/' . Constants::LTPLACEHOLDER . '(.*?)' . Constants::GTPLACEHOLDER . '/u', + $segment = preg_replace_callback( '/' . ConstantEnum::LTPLACEHOLDER . '(.*?)' . ConstantEnum::GTPLACEHOLDER . '/u', function ( $matches ) { $_match = base64_decode( $matches[ 1 ] ); - return Constants::LTPLACEHOLDER . $_match . Constants::GTPLACEHOLDER; + return ConstantEnum::LTPLACEHOLDER . $_match . ConstantEnum::GTPLACEHOLDER; }, $segment ); //base64 decode of the tag content to avoid unwanted manipulation @@ -32,8 +31,8 @@ function ( $matches ) { $segment = str_replace( $tag_attribute[ 0 ], 'equiv-text="base64:' . base64_encode( $tag_attribute[ 2 ] ) . "\"", $segment ); } - $segment = str_replace( Constants::LTPLACEHOLDER, "<", $segment ); - $segment = str_replace( Constants::GTPLACEHOLDER, ">", $segment ); + $segment = str_replace( ConstantEnum::LTPLACEHOLDER, "<", $segment ); + $segment = str_replace( ConstantEnum::GTPLACEHOLDER, ">", $segment ); return $segment; diff --git a/src/Filters/RestoreXliffTagsInXliff.php b/src/Filters/RestoreXliffTagsInXliff.php index 2ebdda6..2248e32 100644 --- a/src/Filters/RestoreXliffTagsInXliff.php +++ b/src/Filters/RestoreXliffTagsInXliff.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class RestoreXliffTagsInXliff extends AbstractHandler { @@ -11,8 +11,8 @@ class RestoreXliffTagsInXliff extends AbstractHandler { * @inheritDoc */ public function transform( $segment ) { - $segment = str_replace( Constants::xliffInXliffStartPlaceHolder, "<", $segment ); - $segment = str_replace( Constants::xliffInXliffEndPlaceHolder, ">", $segment ); + $segment = str_replace( ConstantEnum::xliffInXliffStartPlaceHolder, "<", $segment ); + $segment = str_replace( ConstantEnum::xliffInXliffEndPlaceHolder, ">", $segment ); return $segment; } diff --git a/src/Filters/RubyOnRailsI18n.php b/src/Filters/RubyOnRailsI18n.php index 3912fe7..2d181d5 100644 --- a/src/Filters/RubyOnRailsI18n.php +++ b/src/Filters/RubyOnRailsI18n.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class RubyOnRailsI18n extends AbstractHandler { @@ -31,7 +31,7 @@ public function transform( $segment ) { preg_match_all( '/%{[^<>\s%]+?}/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $percentage_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $percentage_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $percentage_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $percentage_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/SingleCurlyBracketsToPh.php b/src/Filters/SingleCurlyBracketsToPh.php index a4dcc70..adf2790 100644 --- a/src/Filters/SingleCurlyBracketsToPh.php +++ b/src/Filters/SingleCurlyBracketsToPh.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class SingleCurlyBracketsToPh extends AbstractHandler { @@ -36,7 +36,7 @@ public function transform( $segment ) { preg_match_all( '/{[^<>{} ]+?}/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $twig_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $twig_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $twig_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $twig_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/Snails.php b/src/Filters/Snails.php index 240d29f..02b1f52 100644 --- a/src/Filters/Snails.php +++ b/src/Filters/Snails.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class Snails extends AbstractHandler { @@ -14,7 +14,7 @@ public function transform( $segment ) { preg_match_all( '/@@[^<>\s]+?@@/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $snail_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $snail_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $snail_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $snail_variable[ 0 ], '/' ) . '/', diff --git a/src/Filters/SpacesToNBSPForView.php b/src/Filters/SpacesToNBSPForView.php index d4443d1..fc0bd15 100644 --- a/src/Filters/SpacesToNBSPForView.php +++ b/src/Filters/SpacesToNBSPForView.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Utils\CatUtils; +use Matecat\SubFiltering\Enum\ConstantEnum; class SpacesToNBSPForView extends AbstractHandler { @@ -23,15 +23,16 @@ public function transform( $segment ) { $segment = str_ireplace( [ ' ', ' ', ' ' /* NBSP in ascii value */, - '�A;', '�C;', '&#nbsp;' + '�A;', '�C;', ' ', ' ', ], [ - CatUtils::lfPlaceholder, - CatUtils::crPlaceholder, - CatUtils::nbspPlaceholder, - CatUtils::lfPlaceholder, - CatUtils::crPlaceholder, - CatUtils::nbspPlaceholder + ConstantEnum::lfPlaceholder, + ConstantEnum::crPlaceholder, + ConstantEnum::nbspPlaceholder, + ConstantEnum::lfPlaceholder, + ConstantEnum::crPlaceholder, + ConstantEnum::nbspPlaceholder, + ConstantEnum::nbspPlaceholder ], $segment ); return $segment; diff --git a/src/Filters/SplitPlaceholder.php b/src/Filters/SplitPlaceholder.php index 7f56c05..2a2325e 100644 --- a/src/Filters/SplitPlaceholder.php +++ b/src/Filters/SplitPlaceholder.php @@ -9,11 +9,11 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class SplitPlaceholder extends AbstractHandler { public function transform( $segment ) { - $segment = str_replace( Constants::splitPlaceHolder, "", $segment ); + $segment = str_replace( ConstantEnum::splitPlaceHolder, "", $segment ); return $segment; } diff --git a/src/Filters/SubFilteredPhToHtml.php b/src/Filters/SubFilteredPhToHtml.php index cc4b84d..d419173 100644 --- a/src/Filters/SubFilteredPhToHtml.php +++ b/src/Filters/SubFilteredPhToHtml.php @@ -3,7 +3,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; class SubFilteredPhToHtml extends AbstractHandler { @@ -41,29 +41,29 @@ public function transform( $segment ) { */ private function placeholdXliffTagsInXliff( $value ) { $value = preg_replace( '|(</x>)|si', "", $value ); - $value = preg_replace( '#<(g\s*id=["\']+.*?["\']+\s*(?!<|>)*?)>#si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '#<(g\s*id=["\']+.*?["\']+\s*(?!<|>)*?)>#si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/g)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/g)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(x .*?/?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '#<(bx[ ]{0,}/?|bx .*?/?)>#si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '#<(ex[ ]{0,}/?|ex .*?/?)>#si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(bpt\s*.*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/bpt)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(ept\s*.*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/ept)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(ph .*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/ph)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(ec .*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/ec)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(sc .*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/sc)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(pc .*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/pc)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(it .*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/it)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(mrk\s*.*?)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); - $value = preg_replace( '|<(/mrk)>|si', Constants::xliffInXliffStartPlaceHolder . "$1" . Constants::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(x .*?/?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '#<(bx[ ]{0,}/?|bx .*?/?)>#si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '#<(ex[ ]{0,}/?|ex .*?/?)>#si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(bpt\s*.*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/bpt)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(ept\s*.*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/ept)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(ph .*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/ph)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(ec .*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/ec)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(sc .*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/sc)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(pc .*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/pc)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(it .*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/it)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(mrk\s*.*?)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); + $value = preg_replace( '|<(/mrk)>|si', ConstantEnum::xliffInXliffStartPlaceHolder . "$1" . ConstantEnum::xliffInXliffEndPlaceHolder, $value ); return $value; } diff --git a/src/Filters/TwigToPh.php b/src/Filters/TwigToPh.php index 617a6a4..eb72aeb 100644 --- a/src/Filters/TwigToPh.php +++ b/src/Filters/TwigToPh.php @@ -10,7 +10,7 @@ namespace Matecat\SubFiltering\Filters; use Matecat\SubFiltering\Commons\AbstractHandler; -use Matecat\SubFiltering\Commons\Constants; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; class TwigToPh extends AbstractHandler { @@ -36,7 +36,7 @@ public function transform( $segment ) { preg_match_all( '/{{[^<>]+?}}|{%[^<>]+?%}|{#[^<>]+?#}/', $segment, $html, PREG_SET_ORDER ); foreach ( $html as $pos => $twig_variable ) { //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag - if ( !strstr( $twig_variable[ 0 ], Constants::GTPLACEHOLDER ) ) { + if ( !strstr( $twig_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) { //replace subsequent elements excluding already encoded $segment = preg_replace( '/' . preg_quote( $twig_variable[ 0 ], '/' ) . '/', diff --git a/src/MateCatFilter.php b/src/MateCatFilter.php index c938160..8cf1d01 100644 --- a/src/MateCatFilter.php +++ b/src/MateCatFilter.php @@ -8,7 +8,6 @@ use Matecat\SubFiltering\Filters\DataRefRestore; use Matecat\SubFiltering\Filters\DollarCurlyBrackets; use Matecat\SubFiltering\Filters\DoubleSquareBrackets; -use Matecat\SubFiltering\Filters\DoubleUnderscore; use Matecat\SubFiltering\Filters\EncodeToRawXML; use Matecat\SubFiltering\Filters\FromLayer2ToRawXML; use Matecat\SubFiltering\Filters\FromViewNBSPToSpaces; @@ -20,7 +19,6 @@ use Matecat\SubFiltering\Filters\MateCatCustomPHToStandardPH; use Matecat\SubFiltering\Filters\Percentages; use Matecat\SubFiltering\Filters\PercentNumberSnail; -use Matecat\SubFiltering\Filters\PercentSnail; use Matecat\SubFiltering\Filters\PlaceBreakingSpacesInXliff; use Matecat\SubFiltering\Filters\PlaceHoldXliffTags; use Matecat\SubFiltering\Filters\RemoveCTypeFromPhTags; diff --git a/src/MyMemoryFilter.php b/src/MyMemoryFilter.php index 8103f0a..6f42aa6 100644 --- a/src/MyMemoryFilter.php +++ b/src/MyMemoryFilter.php @@ -8,7 +8,6 @@ use Matecat\SubFiltering\Filters\DollarCurlyBrackets; use Matecat\SubFiltering\Filters\DoubleSnail; use Matecat\SubFiltering\Filters\DoubleSquareBrackets; -use Matecat\SubFiltering\Filters\DoubleUnderscore; use Matecat\SubFiltering\Filters\EncodeToRawXML; use Matecat\SubFiltering\Filters\FromViewNBSPToSpaces; use Matecat\SubFiltering\Filters\HtmlPlainTextDecoder; diff --git a/src/Utils/CatUtils.php b/src/Utils/CatUtils.php index 7c3db50..bd5c6bc 100644 --- a/src/Utils/CatUtils.php +++ b/src/Utils/CatUtils.php @@ -3,9 +3,6 @@ namespace Matecat\SubFiltering\Utils; class CatUtils { - const lfPlaceholder = '##$_0A$##'; - const crPlaceholder = '##$_0D$##'; - const nbspPlaceholder = '##$_A0$##'; /** * Get the char code from a multi byte char diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index 6cc5b81..cfa265f 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -4,6 +4,7 @@ use Exception; use Matecat\SubFiltering\Commons\Pipeline; +use Matecat\SubFiltering\Enum\ConstantEnum; use Matecat\SubFiltering\Enum\CTypeEnum; use Matecat\SubFiltering\Filters\LtGtDecode; use Matecat\SubFiltering\Filters\SprintfToPH; @@ -225,7 +226,7 @@ public function test_3_HandlingNBSP() { $filter = $this->getFilterInstance(); $segment = $expectedL1 = '5 tips for creating a great   guide'; - $segment_to_UI = $string_from_UI = '5 tips for creating a great ' . CatUtils::nbspPlaceholder . ' guide'; + $segment_to_UI = $string_from_UI = '5 tips for creating a great ' . ConstantEnum::nbspPlaceholder . ' guide'; $segmentL1 = $filter->fromLayer0ToLayer1( $segment ); $segmentL2 = $filter->fromLayer0ToLayer2( $segment ); @@ -256,6 +257,22 @@ public function testHTMLFromLayer2() { } + /** + ************************** + * NBSP + ************************** + */ + + public function testNbsp() { + $filter = $this->getFilterInstance(); + + $expected_segment = '   Test'; + $string_from_UI = ConstantEnum::nbspPlaceholder.ConstantEnum::nbspPlaceholder.ConstantEnum::nbspPlaceholder.'Test'; + + $this->assertEquals( $expected_segment, $filter->fromLayer2ToLayer0( $string_from_UI ) ); + $this->assertEquals( $string_from_UI, $filter->fromLayer0ToLayer2( $expected_segment ) ); + } + /** ************************** * Sprintf