|
1
|
|
|
<?php |
|
2
|
|
|
namespace DBFaker\Generators; |
|
3
|
|
|
|
|
4
|
|
|
use DBFaker\Generators\Conditions\CheckTypeCondition; |
|
5
|
|
|
use DBFaker\Generators\Conditions\ConditionInterface; |
|
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
7
|
|
|
|
|
8
|
|
|
class GeneratorFinderBuilder |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $generators = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* GeneratorFinderBuilder constructor. |
|
18
|
|
|
* @param FakeDataGeneratorInterface[] $generators |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(array $generators) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->generators = $generators; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return GeneratorFinderBuilder |
|
28
|
|
|
* @throws \DBFaker\Exceptions\UnsupportedDataTypeException |
|
29
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function buildDefaultFinderBuilder() : GeneratorFinderBuilder |
|
32
|
|
|
{ |
|
33
|
|
|
$builder = new GeneratorFinderBuilder([]); |
|
34
|
|
|
|
|
35
|
|
|
$typeFactories = [ |
|
36
|
|
|
Type::TARRAY => new ComplexObjectGeneratorFactory(), |
|
37
|
|
|
Type::SIMPLE_ARRAY => new ComplexObjectGeneratorFactory(0), |
|
38
|
|
|
Type::JSON_ARRAY => new ComplexObjectGeneratorFactory(2), |
|
39
|
|
|
Type::JSON => new ComplexObjectGeneratorFactory(2), |
|
40
|
|
|
Type::OBJECT => new ComplexObjectGeneratorFactory(), |
|
41
|
|
|
|
|
42
|
|
|
Type::BOOLEAN => new SimpleGeneratorFactory('boolean'), |
|
43
|
|
|
|
|
44
|
|
|
Type::DATETIME => new SimpleGeneratorFactory('dateTime'), |
|
45
|
|
|
Type::DATETIMETZ => new SimpleGeneratorFactory('dateTime'), |
|
46
|
|
|
Type::DATE => new SimpleGeneratorFactory('dateTime'), |
|
47
|
|
|
Type::TIME => new SimpleGeneratorFactory('dateTime'), |
|
48
|
|
|
|
|
49
|
|
|
Type::DATETIME_IMMUTABLE => new DateTimeImmutableGeneratorFactory(), |
|
50
|
|
|
Type::DATE_IMMUTABLE => new DateTimeImmutableGeneratorFactory(), |
|
51
|
|
|
Type::DATETIMETZ_IMMUTABLE => new DateTimeImmutableGeneratorFactory(), |
|
52
|
|
|
Type::TIME_IMMUTABLE => new DateTimeImmutableGeneratorFactory(), |
|
53
|
|
|
Type::DATEINTERVAL => new DateIntervalGeneratorFactory(), |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
Type::BIGINT => new NumericGeneratorFactory(), |
|
57
|
|
|
Type::INTEGER => new NumericGeneratorFactory(), |
|
58
|
|
|
Type::SMALLINT => new NumericGeneratorFactory(), |
|
59
|
|
|
Type::FLOAT => new NumericGeneratorFactory(), |
|
60
|
|
|
Type::DECIMAL => new NumericGeneratorFactory(), |
|
61
|
|
|
|
|
62
|
|
|
Type::STRING => new TextGeneratorFactory(), |
|
63
|
|
|
Type::TEXT => new TextGeneratorFactory(), |
|
64
|
|
|
|
|
65
|
|
|
Type::GUID => new SimpleGeneratorFactory('uuid') |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($typeFactories as $type => $factory) { |
|
69
|
|
|
$builder->addGenerator( |
|
70
|
|
|
new CheckTypeCondition(Type::getType($type)), |
|
71
|
|
|
$factory |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $builder; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function addGenerator(ConditionInterface $condition, FakeDataGeneratorFactoryInterface $generator) : GeneratorFinderBuilder |
|
79
|
|
|
{ |
|
80
|
|
|
array_unshift($this->generators, [$condition, $generator]); |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function buildFinder() : GeneratorFinder |
|
85
|
|
|
{ |
|
86
|
|
|
return new GeneratorFinder($this->generators); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|