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

Add new parameter --gdpr-replacements-locale #51

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
10 changes: 5 additions & 5 deletions src/ColumnTransformer/ColumnTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ abstract class ColumnTransformer
protected static $dispatcher;


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

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

}

public static function replaceValue($tableName, $columnName, $expression)
public static function replaceValue($tableName, $columnName, $expression, $locale)
{
self::setUp();
self::setUp($locale);
$event = new ColumnTransformEvent($tableName, $columnName, $expression);
self::$dispatcher->dispatch(self::COLUMN_TRANSFORM_REQUEST, $event);
if ($event->isReplacementSet()) {
Expand All @@ -56,4 +56,4 @@ public function __invoke(ColumnTransformEvent $event)
abstract public function getValue($expression);

abstract protected function getSupportedFormatters();
}
}
4 changes: 2 additions & 2 deletions src/ColumnTransformer/Plugins/FakerColumnTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ protected function getSupportedFormatters()
return array_keys(self::$formatterTansformerMap);
}

public function __construct()
public function __construct($locale)
{
if (!isset(self::$generator)) {
self::$generator = Factory::create();
self::$generator = Factory::create($locale);
foreach(self::$generator->getProviders() as $provider)
{
$clazz = new \ReflectionClass($provider);
Expand Down
3 changes: 3 additions & 0 deletions src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ protected function configure()
'A json of gdpr replacement values keyed by table and column.')
->addOption('gdpr-replacements-file', null, InputOption::VALUE_OPTIONAL,
'File that contains a json of gdpr replacement values keyed by table and column.')
->addOption('gdpr-replacements-locale', null, InputOption::VALUE_OPTIONAL,
'Locale used for creating the fake data.')
->addOption('debug-sql', null, InputOption::VALUE_NONE,
'Add a comment with the dump sql.')
// This seems NOT to work as documented.
Expand Down Expand Up @@ -371,6 +373,7 @@ protected function getDumpSettingsDefault()
] + [
'gdpr-expressions' => null,
'gdpr-replacements' => null,
'gdpr-replacements-locale' => 'en_EN',
'debug-sql' => false,
];
}
Expand Down
10 changes: 9 additions & 1 deletion src/MysqldumpGdpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class MysqldumpGdpr extends Mysqldump
/** @var [string][string]string */
protected $gdprReplacements;

/** @var string */
protected $gdprReplacementsLocale;

/** @var bool */
protected $debugSql;

Expand All @@ -35,6 +38,11 @@ public function __construct(
unset($dumpSettings['gdpr-replacements']);
}

if (array_key_exists('gdpr-replacements-locale', $dumpSettings)) {
$this->gdprReplacementsLocale = $dumpSettings['gdpr-replacements-locale'];
unset($dumpSettings['gdpr-replacements-locale']);
}

if (array_key_exists('debug-sql', $dumpSettings)) {
$this->debugSql = $dumpSettings['debug-sql'];
unset($dumpSettings['debug-sql']);
Expand Down Expand Up @@ -64,7 +72,7 @@ public function getColumnStmt($tableName)
protected function hookTransformColumnValue($tableName, $colName, $colValue)
{
if (!empty($this->gdprReplacements[$tableName][$colName])) {
$replacement = ColumnTransformer::replaceValue($tableName, $colName, $this->gdprReplacements[$tableName][$colName]);
$replacement = ColumnTransformer::replaceValue($tableName, $colName, $this->gdprReplacements[$tableName][$colName], $this->gdprReplacementsLocale);
if($replacement !== FALSE) {
return $replacement;
}
Expand Down