diff --git a/src/AbstractFilter.php b/src/AbstractFilter.php index 097e0c0..e596976 100644 --- a/src/AbstractFilter.php +++ b/src/AbstractFilter.php @@ -65,6 +65,14 @@ protected function setTarget( $target ) $this->target = $target; } + /** + * Destroy the singleton + */ + public static function destroyInstance() + { + static::$_INSTANCE = null; + } + /** * @param string $source * @param string $target diff --git a/src/Filters/Snails.php b/src/Filters/Snails.php new file mode 100644 index 0000000..edcee94 --- /dev/null +++ b/src/Filters/Snails.php @@ -0,0 +1,31 @@ +\s]+?@@|@[^<>\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) ){ + //replace subsequent elements excluding already encoded + $segment = preg_replace( + '/' . preg_quote( $snail_variable[0], '/' ) . '/', + '', + $segment, + 1 + ); + } + } + + return $segment; + } +} \ No newline at end of file diff --git a/src/MyMemoryFilter.php b/src/MyMemoryFilter.php index 3f6cf9c..ce0ede0 100644 --- a/src/MyMemoryFilter.php +++ b/src/MyMemoryFilter.php @@ -2,6 +2,7 @@ namespace Matecat\SubFiltering; + use Matecat\SubFiltering\Commons\Pipeline; use Matecat\SubFiltering\Filters\CtrlCharsPlaceHoldToAscii; use Matecat\SubFiltering\Filters\EncodeToRawXML; @@ -25,6 +26,8 @@ use Matecat\SubFiltering\Filters\SubFilteredPhToHtml; use Matecat\SubFiltering\Filters\TwigToPh; use Matecat\SubFiltering\Filters\Variables; +use Matecat\SubFiltering\Filters\Snails; +use Matecat\SubFiltering\Filters\DoubleSnail; /** * Class MyMemoryFilter @@ -39,6 +42,7 @@ * @package Matecat\SubFiltering */ class MyMemoryFilter extends AbstractFilter { + /** * Used to transform database raw xml content ( Layer 0 ) to the sub filtered structures, used for server to server ( Ex: TM/MT ) communications ( Layer 1 ) * @@ -47,14 +51,15 @@ class MyMemoryFilter extends AbstractFilter { * * @return mixed */ - public function fromLayer0ToLayer1( $segment, $cid = null ) { + public function fromLayer0ToLayer1( $segment, $cid = null ) + { $channel = new Pipeline( $this->source, $this->target ); $channel->addLast( new StandardPHToMateCatCustomPH() ); $channel->addLast( new PlaceHoldXliffTags() ); $channel->addLast( new LtGtDecode() ); $channel->addLast( new HtmlToPh() ); if ( $cid == 'airbnb' ) { - $channel->addLast( new Variables() ); // SE AIRBNB + $channel->addLast( new Variables() ); $channel->addLast( new SmartCounts() ); } @@ -67,7 +72,8 @@ public function fromLayer0ToLayer1( $segment, $cid = null ) { $channel->remove( new TwigToPh() ); $channel->remove( new SprintfToPH() ); $channel->addAfter( new HtmlToPh(), new RubyOnRailsI18n() ); - $channel->addAfter( new RubyOnRailsI18n(), new Percentages() ); + $channel->addAfter( new RubyOnRailsI18n(), new Snails() ); + $channel->addAfter( new Snails(), new Percentages() ); $channel->addAfter( new Percentages(), new SprintfToPH() ); $channel->addAfter( new SprintfToPH(), new TwigToPh() ); $channel->addAfter( new TwigToPh(), new SingleCurlyBracketsToPh() ); diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index 8aba6ec..3b26b5d 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -12,12 +12,16 @@ use Matecat\SubFiltering\Utils\CatUtils; use PHPUnit\Framework\TestCase; -class MateCatSubFilteringTest extends TestCase { +class MateCatSubFilteringTest extends TestCase +{ /** * @return \Matecat\SubFiltering\AbstractFilter * @throws \Exception */ - private function getFilterInstance() { + private function getFilterInstance() + { + MateCatFilter::destroyInstance(); // for isolation test + return MateCatFilter::getInstance( new FeatureSet(), 'en-US', 'it-IT' ); } diff --git a/tests/MyMemorySubFilteringTest.php b/tests/MyMemorySubFilteringTest.php index 5639c8d..e4c839c 100644 --- a/tests/MyMemorySubFilteringTest.php +++ b/tests/MyMemorySubFilteringTest.php @@ -3,7 +3,6 @@ namespace Matecat\SubFiltering\Tests; use Matecat\SubFiltering\MyMemoryFilter; -use Matecat\SubFiltering\Tests\Mocks\Features\AirbnbFeature; use Matecat\SubFiltering\Tests\Mocks\FeatureSet; use PHPUnit\Framework\TestCase; @@ -15,11 +14,16 @@ class MyMemorySubFilteringTest extends TestCase */ private function getFilterInstance() { - $featureSet = new FeatureSet([new AirbnbFeature()]); + MyMemoryFilter::destroyInstance(); // for isolation test - return MyMemoryFilter::getInstance($featureSet, 'en-US','it-IT', []); + return MyMemoryFilter::getInstance(new FeatureSet(), 'en-US','it-IT', []); } + /** + * Test for Airbnb + * + * @throws \Exception + */ public function testVariablesWithHTML() { $filter = $this->getFilterInstance(); @@ -28,6 +32,54 @@ public function testVariablesWithHTML() $segment_from_UI = 'Airbnb account.%{}1) From '; $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); - $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'airbnb' ) ); + } + + /** + * Test for skyscanner + * + * @throws \Exception + */ + public function testSingleSnailSyntax() + { + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @this is a variable@ is not valid'; + $segment_from_UI = 'This syntax @this is a variable@ is not valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @this_is_a_variable@ is valid'; + $segment_from_UI = 'This syntax is valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + } + + /** + * Test for skyscanner + * + * @throws \Exception + */ + public function testDoubleSnailSyntax() + { + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @@this is a variable@@ is not valid'; + $segment_from_UI = 'This syntax @@this is a variable@@ is not valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); + + $filter = $this->getFilterInstance(); + + $db_segment = 'This syntax @@this_is_a_variable@@ is valid'; + $segment_from_UI = 'This syntax is valid'; + + $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); + $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) ); } } \ No newline at end of file diff --git a/tests/TestHandlersOrder.php b/tests/TestHandlersOrder.php index e5583a4..599bcc5 100644 --- a/tests/TestHandlersOrder.php +++ b/tests/TestHandlersOrder.php @@ -54,7 +54,7 @@ public function testReOrder() { $this->channel->addAfter( new SprintfToPH(), new TwigToPh() ); $this->channel->addAfter( new TwigToPh(), new SingleCurlyBracketsToPh() ); - $reflection = new ReflectionClass( $this->channel ); + $reflection = new \ReflectionClass( $this->channel ); $property = $reflection->getProperty( 'handlers' ); $property->setAccessible( true ); $handlersList = $property->getValue( $this->channel );