Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registration of custom transformers and pass whole event to get… #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/ColumnTransformer/ColumnTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace machbarmacher\GdprDump\ColumnTransformer;


use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use machbarmacher\GdprDump\ColumnTransformer\Plugins\ClearColumnTransformer;
use machbarmacher\GdprDump\ColumnTransformer\Plugins\FakerColumnTransformer;
use Symfony\Component\EventDispatcher\EventDispatcher;

abstract class ColumnTransformer
{
Expand All @@ -19,18 +18,19 @@ abstract class ColumnTransformer

protected static $dispatcher;

public static function addTransformer(ColumnTransformer $columnTransformer)
{
self::setUp();
self::$dispatcher->addListener(self::COLUMN_TRANSFORM_REQUEST, $columnTransformer);
}

public static function setUp()
{
if (!isset(self::$dispatcher)) {
self::$dispatcher = new EventDispatcher();

self::$dispatcher->addListener(self::COLUMN_TRANSFORM_REQUEST,
new FakerColumnTransformer());
self::$dispatcher->addListener(self::COLUMN_TRANSFORM_REQUEST,
new ClearColumnTransformer());
self::addTransformer(new FakerColumnTransformer());
self::addTransformer(new ClearColumnTransformer());
}

}

public static function replaceValue($tableName, $columnName, $expression)
Expand All @@ -47,13 +47,15 @@ public static function replaceValue($tableName, $columnName, $expression)

public function __invoke(ColumnTransformEvent $event)
{
if (in_array(($event->getExpression())['formatter'],
$this->getSupportedFormatters())) {
$event->setReplacementValue($this->getValue($event->getExpression()));
if (in_array(
($event->getExpression())['formatter'],
$this->getSupportedFormatters()
)) {
$event->setReplacementValue($this->getValue($event));
}
}

abstract public function getValue($expression);
abstract public function getValue(ColumnTransformEvent $event);

abstract protected function getSupportedFormatters();
}
}
5 changes: 3 additions & 2 deletions src/ColumnTransformer/Plugins/ClearColumnTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace machbarmacher\GdprDump\ColumnTransformer\Plugins;

use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformer;
use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformEvent;

class ClearColumnTransformer extends ColumnTransformer
{
Expand All @@ -12,8 +13,8 @@ protected function getSupportedFormatters()
return ['clear'];
}

public function getValue($expression)
public function getValue(ColumnTransformEvent $event)
{
return "";
}
}
}
16 changes: 8 additions & 8 deletions src/ColumnTransformer/Plugins/FakerColumnTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace machbarmacher\GdprDump\ColumnTransformer\Plugins;

use Faker\Factory;
use Faker\Provider\Base;
use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformer;
use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformEvent;

class FakerColumnTransformer extends ColumnTransformer
{
Expand All @@ -30,21 +30,21 @@ public function __construct()
{
if (!isset(self::$generator)) {
self::$generator = Factory::create();
foreach(self::$generator->getProviders() as $provider)
{
foreach (self::$generator->getProviders() as $provider) {
$clazz = new \ReflectionClass($provider);
$methods = $clazz->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach($methods as $m)
{
if(strpos($m->name, '__') === 0) continue;
foreach ($methods as $m) {
if (strpos($m->name, '__') === 0) {
continue;
}
self::$formatterTansformerMap[$m->name] = $m->name;
}
}
}
}

public function getValue($expression)
public function getValue(ColumnTransformEvent $event)
{
return self::$generator->format(self::$formatterTansformerMap[$expression['formatter']]);
return self::$generator->format(self::$formatterTansformerMap[$event->getExpression()['formatter']]);
}
}