Skip to content

Commit

Permalink
Faker improvements (TheSoftwareHouse#7)
Browse files Browse the repository at this point in the history
* Create FakerUnstoredMask

* Maintain empty strings

* Faker locale as configuration option
  • Loading branch information
ajmahoney authored Jun 16, 2020
1 parent 363cdf1 commit ff1f6d0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ parameters:
env(DATABASE_URL): ''
env(CHARSET): 'utf8'
env(COLLATE): 'utf8_general_ci'
env(FAKER_LOCALE): 'en_GB'
charset: '%env(CHARSET)%'
collate: '%env(COLLATE)%'
faker_locale: '%env(FAKER_LOCALE)%'
env(MYSQL_ATTR_INIT_COMMAND): "SET NAMES '%charset%' COLLATE '%collate%'"


Expand Down
7 changes: 6 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ services:
app.fogger.faker_generator:
class: Faker\Factory
factory: ['Faker\Factory', create]
arguments: ['%faker_locale%']

App\Fogger\Mask\FakerMask:
arguments:
- '@app.fogger.faker_generator'


App\Fogger\Mask\FakerUnstoredMask:
arguments:
- '@app.fogger.faker_generator'

App\Fogger\Refine\RefineExecutor:
arguments:
- '@doctrine.dbal.target_connection'
Expand Down
2 changes: 1 addition & 1 deletion src/Fogger/Mask/AbstractCachedMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private function forgeCacheKey(string $value, array $options, bool $substitute =
*/
public function apply(?string $value, array $options = []): ?string
{
if (null === $value) {
if ((null === $value) or ("" === $value)) {
return $value;
}

Expand Down
50 changes: 50 additions & 0 deletions src/Fogger/Mask/FakerUnstoredMask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Fogger\Mask;

use Faker\Generator;

final class FakerUnstoredMask extends AbstractMask
{
const DEFAULT_METHOD = 'email';

private $generator;

public function __construct(Generator $generator)
{
$this->generator = $generator;
}

public function apply(?string $value, array $options = []): ?string
{
if ((null === $value) or ("" === $value )) {
return $value;
}

$method = $options['method'] ?? self::DEFAULT_METHOD;
$arguments = $options['arguments'] ?? [];
$modifier = $options['modifier'] ?? null;
$modifierArguments = $options['modifierArguments'] ?? [];

$generator = $this->generator;

if ('optional' === $modifier) {
$generator = $generator->optional(...$modifierArguments);
}

$result = $generator->$method(...$arguments);

if (is_array($result)) {
$result = implode(' ', $result);
} elseif ($result instanceof \DateTime) {
$result = $result->format("Y-m-d H:i:s");
}

return $result;
}

protected function getMaskName(): string
{
return 'fakerunstored';
}
}

0 comments on commit ff1f6d0

Please sign in to comment.