-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathArrayLoader.php
More file actions
72 lines (63 loc) · 2.33 KB
/
ArrayLoader.php
File metadata and controls
72 lines (63 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Anonymization\Config\Loader;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ArrayLoader implements LoaderInterface
{
public function __construct(
private array $data,
private string $connectionName,
/**
* Root directory from which the configuration was loaded. It allows
* later file loading (for example, when sources are CSV or TXT files).
*/
private readonly string $basePath,
) {}
#[\Override]
public function load(AnonymizationConfig $config): void
{
if ($this->connectionName !== $config->connectionName) {
return;
}
$resolver = (new OptionsResolver())
->setRequired('anonymizer')
->setAllowedTypes('anonymizer', 'string')
->setDefault('options', [])
->setAllowedTypes('options', 'array')
;
foreach ($this->getData() as $table => $tableConfigs) {
foreach ($tableConfigs as $target => $targetConfig) {
try {
$targetConfig = \is_array($targetConfig) ? $targetConfig : ['anonymizer' => $targetConfig];
$targetConfig = $resolver->resolve($targetConfig);
} catch (ExceptionInterface $e) {
$message = $e->getMessage();
throw new \InvalidArgumentException(
<<<TXT
Error while validating configuration for table '{$table}', key '{$target}':
{$message}
TXT
);
}
$config->add(new AnonymizerConfig(
$table,
$target,
$targetConfig['anonymizer'],
new Options($targetConfig['options']),
$this->basePath,
));
}
}
}
/**
* Please override.
*/
protected function getData(): array
{
return $this->data;
}
}