1
|
|
|
<?php |
2
|
|
|
namespace DBFaker\Generators; |
3
|
|
|
|
4
|
|
|
use DBFaker\Exceptions\UnsupportedDataTypeException; |
5
|
|
|
use DBFaker\Generators\Conditions\ConditionInterface; |
6
|
|
|
use DBFaker\Helpers\SchemaHelper; |
7
|
|
|
use Doctrine\DBAL\Schema\Column; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
|
10
|
|
|
class GeneratorFinder |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var FakeDataGeneratorFactoryInterface[] |
15
|
|
|
*/ |
16
|
|
|
private $generatorFactories; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var FakeDataGeneratorInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $generators; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* GeneratorFinder constructor. |
25
|
|
|
* @param FakeDataGeneratorFactoryInterface[] $generatorFactories |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $generatorFactories) |
28
|
|
|
{ |
29
|
|
|
$this->generatorFactories = $generatorFactories; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Table $table |
34
|
|
|
* @param Column $column |
35
|
|
|
* @return FakeDataGeneratorInterface |
36
|
|
|
* @throws \DBFaker\Exceptions\UnsupportedDataTypeException |
37
|
|
|
*/ |
38
|
|
|
public function findGenerator(Table $table, Column $column, SchemaHelper $helper) : FakeDataGeneratorInterface |
39
|
|
|
{ |
40
|
|
|
$generator = null; |
41
|
|
|
if (!isset($this->generators[$table->getName() . '.' . $column->getName()])) { |
42
|
|
|
foreach ($this->generatorFactories as list($condition, $generatorFactory)) { |
43
|
|
|
/** @var $condition ConditionInterface */ |
44
|
|
|
if ($condition->canApply($table, $column)) { |
45
|
|
|
$generator = $generatorFactory->create($table, $column, $helper); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
if (!$generator) { |
49
|
|
|
throw new UnsupportedDataTypeException('Could not find suitable generator for column ' . $table->getName() . '.' . $column->getName() . ' of type : ' . $column->getType()->getName()); |
50
|
|
|
} |
51
|
|
|
$this->generators[$table->getName() . '.' . $column->getName()] = $generator; |
52
|
|
|
} |
53
|
|
|
return $this->generators[$table->getName() . '.' . $column->getName()]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|