|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DBFaker\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use DBFaker\DBFaker; |
|
6
|
|
|
use DBFaker\Exceptions\SchemaLogicException; |
|
7
|
|
|
use DBFaker\Helpers\DBFakerSchemaManager; |
|
8
|
|
|
use DBFaker\Helpers\SchemaHelper; |
|
9
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Index; |
|
11
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
12
|
|
|
|
|
13
|
|
|
class CompoundColumnGenerator implements FakeDataGeneratorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $possibleValues = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var mixed[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $generatedValues = []; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* CompoundColumnGenerator constructor. |
|
29
|
|
|
* @param Table $table |
|
30
|
|
|
* @param Index $index |
|
31
|
|
|
* @param SchemaHelper $schemaHelper |
|
32
|
|
|
* @param DBFaker $dbFaker |
|
33
|
|
|
* @param AbstractSchemaManager $schemaManager |
|
34
|
|
|
* @param int $valuesCount |
|
35
|
|
|
* @throws \DBFaker\Exceptions\SchemaLogicException |
|
36
|
|
|
* @throws \DBFaker\Exceptions\UnsupportedDataTypeException |
|
37
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
|
38
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Table $table, Index $index, SchemaHelper $schemaHelper, DBFaker $dbFaker, AbstractSchemaManager $schemaManager, DBFakerSchemaManager $fakerManagerHelper, int $valuesCount) |
|
41
|
|
|
{ |
|
42
|
|
|
foreach ($index->getColumns() as $columnName) { |
|
43
|
|
|
//FK or normal column ? |
|
44
|
|
|
$column = $table->getColumn($columnName); |
|
45
|
|
|
if ($schemaHelper->isColumnPartOfForeignKeyConstraint($table, $column)) { |
|
46
|
|
|
$fkConstraint = $schemaHelper->getForeignKeyConstraintByLocal($table, $column); |
|
47
|
|
|
if ($fkConstraint === null) { |
|
48
|
|
|
throw new SchemaLogicException($column->getName() . ' was detected as foreign key but could not get it'); |
|
49
|
|
|
} |
|
50
|
|
|
$foreignTableName = $fkConstraint->getForeignTableName(); |
|
51
|
|
|
$foreignColumn = $fakerManagerHelper->getForeignColumn($table, $column); |
|
52
|
|
|
$foreignTable = $schemaManager->listTableDetails($foreignTableName); |
|
53
|
|
|
$pkRegistry = $dbFaker->getPkRegistry($foreignTable); |
|
54
|
|
|
$values = $pkRegistry->loadValuesFromTable()->getAllValues(); |
|
55
|
|
|
$this->possibleValues[$columnName] = array_map(function ($value) use ($foreignColumn) { |
|
56
|
|
|
return $value[$foreignColumn->getName()]; |
|
57
|
|
|
}, $values); |
|
58
|
|
|
} else { |
|
59
|
|
|
$generator = $dbFaker->getSimpleColumnGenerator($table, $column); |
|
60
|
|
|
for ($i = 0; $i < $valuesCount; $i++) { |
|
61
|
|
|
$this->possibleValues[$columnName][] = $generator(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
/** |
|
67
|
|
|
* @return mixed[] |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __invoke() : array |
|
70
|
|
|
{ |
|
71
|
|
|
$returnVal = []; |
|
72
|
|
|
foreach ($this->possibleValues as $columnName => $values) { |
|
73
|
|
|
$returnVal[$columnName] = $values[array_rand($values)]; |
|
74
|
|
|
} |
|
75
|
|
|
if (!\in_array($returnVal, $this->generatedValues, true)) { |
|
76
|
|
|
$this->generatedValues[] = $returnVal; |
|
77
|
|
|
return $returnVal; |
|
78
|
|
|
} |
|
79
|
|
|
return $this(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|