Skip to content

Commit

Permalink
Merge pull request #13 from cazana/pass-params-to-faker-commands
Browse files Browse the repository at this point in the history
Pass params to faker commands
  • Loading branch information
danielsimkus authored May 25, 2021
2 parents d3c4ce6 + 1b7b805 commit 3011976
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 55 deletions.
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Private Dump requires PHP >= 5.6.0
- [Usage](#usage)
- [Configuration File](#configuration-file)
- [Key Value Tables](#key-value-tables)
- [Modifiers](#modifiers)
- [Replacements](#replacements)
- [Text](#text)
- [Dates](#dates)
Expand Down Expand Up @@ -168,26 +167,7 @@ Private Dump supports replacing values in a key-value store, by using an array i



This is a bit more complicated than the standard replacements, but offers a lot of flexibility for anonymising all types of data.



#### Modifiers

You can modify the resulting value (after replacements/transformers) to various ends. The only currently supported modifier is `max` with more coming:

```json
{
"connection": {..},
"databases": {
...
"username_column": "@userName|max:20"
...
}
}
```


This is a bit more complicated than the standard replacements, but offers a lot of flexibility for anonymising all types of data.

# Replacements

Expand All @@ -197,6 +177,7 @@ All replacements below should be prefixed with an `@` as in the [example configu

If you need to use a hardcoded value (active=0, completed=1) you can do this by omitting the `@`: `"active": 0` in the configuration file.

You can pass variables to commands as such `@numberBetween|100,1000`


#### Text
Expand Down
42 changes: 12 additions & 30 deletions src/PrivateDump/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PrivateDump;

use Faker\Generator;
use PhpParser\Node\Param;

class Transformer
{
Expand Down Expand Up @@ -93,8 +94,11 @@ public function transformIso8601Recent($value)
*
* @return mixed
*/
public function transformOriginal($value)
public function transformOriginal($value, ?int $max = null)
{
if ($max) {
return substr($value, 0, $max);
}
return $value;
}

Expand Down Expand Up @@ -126,9 +130,9 @@ public function transform($value, $replacement)
return $replacement;
}

// Transformer has modifiers, let's use them
// Faker Transformer has modifiers, let's use them
if (strpos($replacement, '|') !== false) {
list($replacement, $modifiers) = explode('|', $replacement, 2);
[$replacement, $modifiers] = explode('|', $replacement, 2);
$modifiers = explode(',', $modifiers);
}

Expand All @@ -142,38 +146,16 @@ public function transform($value, $replacement)
$ownMethod = sprintf('transform%s', ucwords(strtolower($replacement)));

try {
$newValue = method_exists($this, $ownMethod) ? $this->$ownMethod($value) : $this->faker->$replacement;
$newValue = method_exists($this, $ownMethod)
? $this->$ownMethod($value, ...$modifiers)
: $this->faker->$replacement(...$modifiers);

} catch (\Exception $e) {
echo sprintf('[error] Transformer not found, please fix and retry: [%s]', $originalReplacement) . PHP_EOL;
exit(9);
}

return $this->modifyValue($newValue, $modifiers);
return $newValue;
}

/**
* @param $value
* @param array $modifiers - Modifiers for the resulting value, currently only supporting 'max'. E.g. min:4,max:40
*
* @return string
*/
private function modifyValue($value, array $modifiers)
{
if (empty($modifiers)) {
return $value;
}

foreach ($modifiers as $modifier) {
list($rule, $modifierValue) = explode(':', $modifier);
switch ($rule) {
case 'max':
return substr($value, 0, $modifierValue);
break;
default:
return $value;
}
}

return $value;
}
}
9 changes: 5 additions & 4 deletions tests/PrivateDump/TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public function fake_car_faker_plugin_works()
{
$vehicleArray = $this->transformer->transform('not required', '@vehicleArray');
$this->assertArrayHasKey('brand', $vehicleArray);
$this->assertArrayHasKey('model', $vehicleArray); }
$this->assertArrayHasKey('model', $vehicleArray);
}

/** @test */
public function max_modifier_works()
public function modifiers_work()
{
$this->assertEquals(8, strlen($this->transformer->transform('test', '@randomString|max:8')));
$this->assertEquals(10, $this->transformer->transform('test', '@numberBetween|10,10'));
}

/** @test */
Expand All @@ -65,6 +66,6 @@ public function static_values_work()
public function original_replacement_works()
{
$this->assertEquals('[email protected]', $this->transformer->transform('[email protected]', '@original'));
$this->assertEquals('admin@exa', $this->transformer->transform('[email protected]', '@original|max:9'));
$this->assertEquals('admin@exa', $this->transformer->transform('[email protected]', '@original|9'));
}
}

0 comments on commit 3011976

Please sign in to comment.