-
Notifications
You must be signed in to change notification settings - Fork 48
/
Faker.php
74 lines (62 loc) · 2.24 KB
/
Faker.php
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
73
74
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Proxy;
use Exception;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
use Smile\GdprDump\Faker\FakerService;
class Faker implements ConverterInterface
{
private object $provider;
private string $method;
private array $arguments;
/**
* @var int[]
*/
private array $placeholders = [];
public function __construct(private FakerService $fakerService)
{
}
/**
* @inheritdoc
*/
public function setParameters(array $parameters): void
{
$input = (new ParameterProcessor())
->addParameter('formatter', Parameter::TYPE_STRING, true)
->addParameter('arguments', Parameter::TYPE_ARRAY, false, [])
->process($parameters);
// Create the formatter now to ensure that errors related to undefined formatters
// are triggered before the start of the dump process
$formatter = $input->get('formatter');
try {
// @phpstan-ignore-next-line getFormatter function always returns an array with 2 items
[$this->provider, $this->method] = $this->fakerService
->getGenerator()
->getFormatter($formatter);
} catch (Exception $e) {
// Wrap the original exception to make it more clear that the error is due to a faker formatter
throw new ValidationException(sprintf('Faker formatter error: %s', $e->getMessage()), $e);
}
$this->arguments = $input->get('arguments') ?? [];
foreach ($this->arguments as $name => $value) {
if ($value === '{{value}}') {
$this->placeholders[] = $name;
}
}
}
/**
* @inheritdoc
*/
public function convert(mixed $value, array $context = []): mixed
{
$arguments = $this->arguments;
// Replace all occurrences of "{{value}}" by $value
foreach ($this->placeholders as $name) {
$arguments[$name] = $value;
}
return $this->provider->{$this->method}(...$arguments);
}
}