Skip to content

Commit a7ddc22

Browse files
committed
feature: prepare a few internal changes for future fakerphp/faker integration
1 parent 782cc63 commit a7ddc22

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"symfony/validator": "^6.3|^7.0"
3636
},
3737
"suggest": {
38+
"db-tools-bundle/pack-faker": "FakerPHP/Faker bridge anonyzer pack",
39+
"db-tools-bundle/pack-fr-fr": "French anonymizers pack",
3840
"symfony/password-hasher": "In order to use the password hash anonymizer"
3941
},
4042
"conflict": {

src/Anonymization/Anonymizer/AbstractAnonymizer.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,41 @@ protected function getSalt(): string
110110
*
111111
* @throws \Exception if any option is invalid.
112112
*/
113-
protected function validateOptions(): void {}
113+
protected function validateOptions(): void
114+
{
115+
if ($this->hasSampleSizeOption()) {
116+
if ($this->options->has('sample_size')) {
117+
$value = $this->options->getInt('sample_size');
118+
if ($value <= 0) {
119+
throw new \InvalidArgumentException("'sample_size' option must be a positive integer.");
120+
}
121+
}
122+
}
123+
}
124+
125+
/**
126+
* Does this anonymizer has a "sample size" option.
127+
*/
128+
protected function hasSampleSizeOption(): bool
129+
{
130+
return false;
131+
}
132+
133+
/**
134+
* Default sample size, goes along the "sample size" option set to true.
135+
*/
136+
protected function getDefaultSampleSize(): int
137+
{
138+
return 500;
139+
}
140+
141+
/**
142+
* Default sample size, goes along the "sample size" option set to true.
143+
*/
144+
protected function getSampleSize(): int
145+
{
146+
return $this->options->getInt('sample_size', $this->getDefaultSampleSize());
147+
}
114148

115149
/**
116150
* Initialize your anonymizer.

src/Anonymization/Anonymizer/AbstractMultipleColumnAnonymizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ protected function getColumnTypes(): array
4949
#[\Override]
5050
protected function validateOptions(): void
5151
{
52+
parent::validateOptions();
53+
5254
if (0 === \count($this->options->all())) {
5355
throw new \InvalidArgumentException("You must provide at least one option.");
5456
}

src/Anonymization/Anonymizer/Core/IbanBicAnonymizer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ protected function validateOptions(): void
2929
{
3030
parent::validateOptions();
3131

32-
if ($this->options->has('sample_size')) {
33-
$value = $this->options->getInt('sample_size');
34-
if ($value <= 0) {
35-
throw new \InvalidArgumentException("'sample_size' option must be a positive integer.");
36-
}
37-
}
3832
if ($this->options->has('country')) {
3933
$value = $this->options->getString('country');
4034
if (!\ctype_alpha($value) || 2 !== \strlen($value)) {
@@ -52,10 +46,16 @@ protected function getColumnNames(): array
5246
];
5347
}
5448

49+
#[\Override]
50+
protected function hasSampleSizeOption(): bool
51+
{
52+
return true;
53+
}
54+
5555
#[\Override]
5656
protected function getSamples(): array
5757
{
58-
$sampleSize = $this->options->getInt('sample_size', 500);
58+
$sampleSize = $this->getSampleSize();
5959
$countryCode = $this->options->getString('country', 'FR');
6060

6161
// @todo, pas d'options, pas de count ni de country, désolé.

src/Attribute/AsAnonymizer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public function __construct(
1414
public string $name,
1515
public string $pack,
1616
public ?string $description = null,
17+
/**
18+
* @var array<string>
19+
* Array of class or interface names required for this anonymizer to work.
20+
* Anonymizers whose requirements are not met will not be usable and raise
21+
* an error in listing.
22+
*/
23+
public array $requires = [],
24+
/**
25+
* @var array<string>
26+
* List of composer dependencies for filling the requirements.
27+
*/
28+
public ?array $dependencies = [],
1729
) {}
1830

1931
public function id(): string
@@ -22,4 +34,14 @@ public function id(): string
2234
// which don't have prefix.
2335
return ('core' !== $this->pack ? $this->pack . '.' : '') . $this->name;
2436
}
37+
38+
public function missingRequirements(): bool
39+
{
40+
foreach ($this->requires as $classOrInterfaceName) {
41+
if (!\class_exists($classOrInterfaceName) && !\interface_exists($classOrInterfaceName)) {
42+
return true;
43+
}
44+
}
45+
return false;
46+
}
2547
}

src/Command/Anonymization/AnonymizerListCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4343
$list[$metadata->pack] = [];
4444
}
4545

46-
$list[$metadata->pack]['<info>' . $metadata->id() . '</info>'] = $metadata->description;
46+
$description = $metadata->description;
47+
if ($metadata->missingRequirements()) {
48+
$description .= "\n" . \sprintf('<error>Dependencies are missing: "%s"</error>', \implode('", "', $metadata->dependencies));
49+
}
50+
51+
$list[$metadata->pack]['<info>' . $metadata->id() . '</info>'] = $description;
4752
}
4853

4954
\array_walk($list, fn (array &$anonymizers) => \ksort($anonymizers, SORT_STRING));

tests/Functional/Anonymizer/Core/AddressAnonymizerTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace DbToolsBundle\PackFrFR\Tests\Functional\Anonymizer;
5+
namespace DbToolsBundle\PackFrFR\Tests\Functional\Anonymizer\Core;
66

7-
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
8-
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizator;
9-
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
10-
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
117
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
8+
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
129
use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase;
1310

1411
class AddressAnonymizerTest extends FunctionalTestCase

0 commit comments

Comments
 (0)