Skip to content

Commit

Permalink
@@ syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mauretto78 committed Mar 4, 2022
1 parent 5347786 commit cbca188
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/Filters/Snails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Matecat\SubFiltering\Filters;

use Matecat\SubFiltering\Commons\AbstractHandler;
use Matecat\SubFiltering\Commons\Constants;

class Snails extends AbstractHandler
{
/**
* @inheritDoc
*/
public function transform( $segment )
{
preg_match_all( '/@@[^<>\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], '/' ) . '/',
'<ph id="__mtc_' . $this->getPipeline()->getNextId() . '" equiv-text="base64:' . base64_encode( $snail_variable[ 0 ] ) . '"/>',
$segment,
1
);
}
}

return $segment;
}
}
12 changes: 9 additions & 3 deletions src/MyMemoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Matecat\SubFiltering;


use Matecat\SubFiltering\Commons\Pipeline;
use Matecat\SubFiltering\Filters\CtrlCharsPlaceHoldToAscii;
use Matecat\SubFiltering\Filters\EncodeToRawXML;
Expand All @@ -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
Expand All @@ -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 )
*
Expand All @@ -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() );
}

Expand All @@ -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() );
Expand Down
3 changes: 2 additions & 1 deletion tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use Matecat\SubFiltering\Utils\CatUtils;
use PHPUnit\Framework\TestCase;

class MateCatSubFilteringTest extends TestCase {
class MateCatSubFilteringTest extends TestCase
{
/**
* @return \Matecat\SubFiltering\AbstractFilter
* @throws \Exception
Expand Down
60 changes: 55 additions & 5 deletions tests/MyMemorySubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,11 +14,14 @@ class MyMemorySubFilteringTest extends TestCase
*/
private function getFilterInstance()
{
$featureSet = new FeatureSet([new AirbnbFeature()]);

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();
Expand All @@ -28,6 +30,54 @@ public function testVariablesWithHTML()
$segment_from_UI = 'Airbnb account.<ph id="mtc_1" equiv-text="base64:JXtcbn0="/>%{<ph id="mtc_2" equiv-text="base64:Jmx0O2JyJmd0Ow=="/>}<ph id="mtc_3" equiv-text="base64:JXtcbn0="/>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 <ph id="mtc_1" equiv-text="base64:QHRoaXNfaXNfYV92YXJpYWJsZUA="/> 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 <ph id="mtc_1" equiv-text="base64:QEB0aGlzX2lzX2FfdmFyaWFibGVAQA=="/> is valid';

$this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) );
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment, 'skyscanner' ) );
}
}
2 changes: 1 addition & 1 deletion tests/TestHandlersOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit cbca188

Please sign in to comment.