Skip to content

Commit

Permalink
Merge pull request #18 from matecat/@@-syntax
Browse files Browse the repository at this point in the history
@@ syntax
  • Loading branch information
mauretto78 authored Mar 4, 2022
2 parents 5347786 + 28b3189 commit 060b893
Showing 6 changed files with 111 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/AbstractFilter.php
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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() );
8 changes: 6 additions & 2 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
@@ -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' );
}

60 changes: 56 additions & 4 deletions tests/MyMemorySubFilteringTest.php
Original file line number Diff line number Diff line change
@@ -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.<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
@@ -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 );

0 comments on commit 060b893

Please sign in to comment.