Skip to content

Commit

Permalink
Docu
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed May 26, 2021
1 parent b4649ee commit a439c3f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 65 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Matecat Subfiltering

Subfiltering is a [Matecat](https://matecat.com) component used for string conversion from database to UI layer and viceversa.
Subfiltering is a component used by [Matecat](https://matecat.com) and [MyMemory](https://mymemory.translated.net/)) for string conversion from database to UI layer and viceversa.

## How to use

Expand All @@ -9,7 +9,7 @@ There are two filters available (both are implementation of `AbstractFilter`):
- `MateCatFilter`
- `MyMemoryFilter`

There is a slight difference between the two classes. To instantiate `MateCatFilter` class do the following:
Use `getInstance` method to instantiate these classes:

```php

Expand All @@ -18,16 +18,7 @@ use Matecat\SubFiltering\MateCatFilter;
$filter = MateCatFilter::getInstance(new FeatureSet(), 'it-IT', 'en-EN', []);
```

Instead to use `MyMemoryFilter` (please note that ):

```php

use Matecat\SubFiltering\MyMemoryFilter;

$filter = MyMemoryFilter::getInstance(new FeatureSet(), 'it-IT', 'en-EN');
```

The only required argument is a concrete implementation of `Matecat\SubFiltering\Contracts\FeatureSetInterface`.
The first argument MUST be concrete implementation of `Matecat\SubFiltering\Contracts\FeatureSetInterface`.

There are three more arguments you can pass:

Expand All @@ -37,7 +28,9 @@ There are three more arguments you can pass:

## Basic Usage

Once `Filter` class is instantiated you can use the following methods to convert strings from one layer to another one:
Once an intance of `AbstractFilter` class is instantiated you can use several methods to convert strings from one layer to another one.

### MateCatFilter methods

- `fromLayer0ToLayer2`
- `fromLayer1ToLayer2`
Expand All @@ -48,7 +41,26 @@ Once `Filter` class is instantiated you can use the following methods to convert
- `fromRawXliffToLayer0`
- `fromLayer0ToRawXliff`

Where `Layer0` is the DB layer, `Layer1` is the intermediate layer (used by [MyMemory](https://mymemory.translated.net/)) and `Layer2` is the UI layer.
### MyMemoryFilter methods

- `fromLayer0ToLayer1`
- `fromLayer1ToLayer0`

Where `Layer0` is the DB layer, `Layer1` is the intermediate layer and `Layer2` is the MateCat's UI layer.

## Examples

In the `tests` folder there is an fully working example of a concrete implementation of `FeatureSetInterface` with a custom filter.

```
// tests/Mocks
.
├── Features
│   ├── AirbnbFeature.php
│   └── BaseFeature.php
└── FeatureSet.php
```

## Support

Expand Down
23 changes: 23 additions & 0 deletions src/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ protected function setTarget( $target )
$this->target = $target;
}

/**
* @param string $source
* @param string $target
* @param FeatureSetInterface $featureSet
* @param array $dataRefMap
*
* @return AbstractFilter
* @throws \Exception
*/
public static function getInstance( FeatureSetInterface $featureSet, $source = null, $target = null, array $dataRefMap = [] )
{
if ( static::$_INSTANCE === null ) {
static::$_INSTANCE = new static();
}

static::$_INSTANCE->setSource($source);
static::$_INSTANCE->setTarget($target);
static::$_INSTANCE->setDataRefMap($dataRefMap);
static::$_INSTANCE->setFeatureSet( $featureSet );

return static::$_INSTANCE;
}

/**
* 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 Down
24 changes: 0 additions & 24 deletions src/MateCatFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Matecat\SubFiltering;

use Matecat\SubFiltering\Commons\Pipeline;
use Matecat\SubFiltering\Contracts\FeatureSetInterface;
use Matecat\SubFiltering\Filters\CtrlCharsPlaceHoldToAscii;
use Matecat\SubFiltering\Filters\DataRefReplace;
use Matecat\SubFiltering\Filters\DataRefRestore;
Expand Down Expand Up @@ -56,29 +55,6 @@
*/
class MateCatFilter extends AbstractFilter
{
/**
* @param string $source
* @param string $target
* @param FeatureSetInterface $featureSet
* @param array $dataRefMap
*
* @return AbstractFilter
* @throws \Exception
*/
public static function getInstance( FeatureSetInterface $featureSet, $source = null, $target = null, array $dataRefMap = [] )
{
if ( static::$_INSTANCE === null or !(static::$_INSTANCE instanceof MateCatFilter) ) {
static::$_INSTANCE = new MateCatFilter();
}

static::$_INSTANCE->setSource($source);
static::$_INSTANCE->setTarget($target);
static::$_INSTANCE->setDataRefMap($dataRefMap);
static::$_INSTANCE->setFeatureSet( $featureSet );

return static::$_INSTANCE;
}

/**
* Used to transform database raw xml content ( Layer 0 ) to the UI structures ( Layer 2 )
*
Expand Down
22 changes: 0 additions & 22 deletions src/MyMemoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Matecat\SubFiltering;

use Matecat\SubFiltering\Commons\Pipeline;
use Matecat\SubFiltering\Contracts\FeatureSetInterface;
use Matecat\SubFiltering\Filters\CtrlCharsPlaceHoldToAscii;
use Matecat\SubFiltering\Filters\EncodeToRawXML;
use Matecat\SubFiltering\Filters\FromViewNBSPToSpaces;
Expand Down Expand Up @@ -36,27 +35,6 @@
*/
class MyMemoryFilter extends AbstractFilter
{
/**
* @param string $source
* @param string $target
* @param FeatureSetInterface $featureSet
*
* @return AbstractFilter
* @throws \Exception
*/
public static function getInstance( FeatureSetInterface $featureSet, $source = null, $target = null )
{
if ( static::$_INSTANCE === null or !(static::$_INSTANCE instanceof MyMemoryFilter) ) {
static::$_INSTANCE = new MyMemoryFilter();
}

static::$_INSTANCE->setSource($source);
static::$_INSTANCE->setTarget($target);
static::$_INSTANCE->setFeatureSet( $featureSet );

return static::$_INSTANCE;
}

/**
* 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 Down
4 changes: 2 additions & 2 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace Matecat\SubFiltering\Tests;

use Matecat\SubFiltering\Commons\Pipeline;
use Matecat\SubFiltering\MateCatFilter;
use Matecat\SubFiltering\Filters\LtGtDecode;
use Matecat\SubFiltering\Filters\SprintfToPH;
use Matecat\SubFiltering\Filters\TwigToPh;
use Matecat\SubFiltering\MateCatFilter;
use Matecat\SubFiltering\Tests\Mocks\FeatureSet;
use Matecat\SubFiltering\Utils\CatUtils;
use PHPUnit\Framework\TestCase;

class MateCatSubFilteringTest extends TestCase
{
/**
* @return MateCatFilter
* @return \Matecat\SubFiltering\AbstractFilter
* @throws \Exception
*/
private function getFilterInstance()
Expand Down
5 changes: 2 additions & 3 deletions tests/MyMemorySubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Matecat\SubFiltering\Tests;

use Matecat\SubFiltering\MateCatFilter;
use Matecat\SubFiltering\MyMemoryFilter;
use Matecat\SubFiltering\Tests\Mocks\Features\AirbnbFeature;
use Matecat\SubFiltering\Tests\Mocks\FeatureSet;
Expand All @@ -11,14 +10,14 @@
class MyMemorySubFilteringTest extends TestCase
{
/**
* @return MateCatFilter
* @return \Matecat\SubFiltering\AbstractFilter
* @throws \Exception
*/
private function getFilterInstance()
{
$featureSet = new FeatureSet([new AirbnbFeature()]);

return MyMemoryFilter::getInstance($featureSet, 'en-US','it-IT');
return MyMemoryFilter::getInstance($featureSet, 'en-US','it-IT', []);
}

public function testVariablesWithHTML()
Expand Down

0 comments on commit a439c3f

Please sign in to comment.