|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DBFaker\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use DBFaker\Helpers\DBFakerSchemaManager; |
|
6
|
|
|
use DBFaker\Helpers\PrimaryKeyRegistry; |
|
7
|
|
|
use DBFaker\Helpers\SchemaHelper; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
9
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
11
|
|
|
|
|
12
|
|
|
class ForeignKeyColumnGenerator implements FakeDataGeneratorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Column |
|
16
|
|
|
*/ |
|
17
|
|
|
private $foreignColumn; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var PrimaryKeyRegistry |
|
21
|
|
|
*/ |
|
22
|
|
|
private $foreignPkRegistry; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $generateUniqueValues; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $alreadyGeneratedValues = []; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ForeignKeyColumnGenerator constructor. |
|
37
|
|
|
* @param Table $table |
|
38
|
|
|
* @param Column $column |
|
39
|
|
|
* @param PrimaryKeyRegistry $foreignPkRegistry |
|
40
|
|
|
* @param ForeignKeyConstraint $fk |
|
41
|
|
|
* @param DBFakerSchemaManager $schemaManager |
|
42
|
|
|
* @param SchemaHelper $schemaHelper |
|
43
|
|
|
* @throws \DBFaker\Exceptions\SchemaLogicException |
|
44
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
45
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Table $table, Column $column, PrimaryKeyRegistry $foreignPkRegistry, ForeignKeyConstraint $fk, DBFakerSchemaManager $schemaManager, SchemaHelper $schemaHelper) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->foreignColumn = $schemaManager->getForeignColumn($table, $column); |
|
50
|
|
|
$this->foreignPkRegistry = $foreignPkRegistry; |
|
51
|
|
|
$this->generateUniqueValues = $schemaHelper->isForeignKeyAlsoUniqueIndex($fk); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __invoke() |
|
60
|
|
|
{ |
|
61
|
|
|
$randomPk = $this->foreignPkRegistry->loadValuesFromTable()->getRandomValue($this->alreadyGeneratedValues); |
|
62
|
|
|
$value = $randomPk[$this->foreignColumn->getName()]; |
|
63
|
|
|
if ($this->generateUniqueValues) { |
|
64
|
|
|
$this->alreadyGeneratedValues[] = $randomPk; |
|
65
|
|
|
} |
|
66
|
|
|
return $value; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|