1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
7
|
|
|
use function var_export; |
8
|
|
|
|
9
|
|
|
class ManyToManyRelationshipPathDescriptor |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $targetTable; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $pivotTable; |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $joinForeignKeys; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $joinLocalKeys; |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $whereKeys; |
32
|
|
|
|
33
|
|
|
public function __construct(string $targetTable, string $pivotTable, array $joinForeignKeys, array $joinLocalKeys, array $whereKeys) |
34
|
|
|
{ |
35
|
|
|
$this->targetTable = $targetTable; |
36
|
|
|
$this->pivotTable = $pivotTable; |
37
|
|
|
$this->joinForeignKeys = $joinForeignKeys; |
38
|
|
|
$this->joinLocalKeys = $joinLocalKeys; |
39
|
|
|
$this->whereKeys = $whereKeys; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed[] |
44
|
|
|
*/ |
45
|
|
|
public static function generateModelArray(ForeignKeyConstraint $remoteFk, ForeignKeyConstraint $localFk): array |
46
|
|
|
{ |
47
|
|
|
return [$remoteFk->getForeignTableName(), $remoteFk->getLocalTableName(), $remoteFk->getUnquotedForeignColumns(), $remoteFk->getUnquotedLocalColumns(), $localFk->getUnquotedLocalColumns()]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function generateModelKey(ForeignKeyConstraint $remoteFk, ForeignKeyConstraint $localFk): string |
51
|
|
|
{ |
52
|
|
|
return $remoteFk->getLocalTableName().".".implode("__", $localFk->getUnquotedLocalColumns()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed[] $modelArray |
57
|
|
|
*/ |
58
|
|
|
public static function createFromModelArray(array $modelArray): self |
59
|
|
|
{ |
60
|
|
|
$obj = new self(); |
|
|
|
|
61
|
|
|
$obj->targetTable = $modelArray[0]; |
62
|
|
|
$obj->pivotTable = $modelArray[1]; |
63
|
|
|
|
64
|
|
|
$obj->joinForeignKeys = $modelArray[2]; |
65
|
|
|
$obj->joinLocalKeys= $modelArray[3]; |
66
|
|
|
$obj->whereKeys = $modelArray[4]; |
67
|
|
|
|
68
|
|
|
return $obj; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getPivotName(): string |
72
|
|
|
{ |
73
|
|
|
return $this->pivotTable; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTargetName(): string |
77
|
|
|
{ |
78
|
|
|
return $this->targetTable; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getPivotFrom(): string |
82
|
|
|
{ |
83
|
|
|
$mainTable = $this->targetTable; |
84
|
|
|
$pivotTable = $this->pivotTable; |
85
|
|
|
|
86
|
|
|
$join = []; |
87
|
|
|
foreach ($this->joinForeignKeys as $key => $column) { |
88
|
|
|
$join[] = $mainTable.'.'.$column.' = pivot.'.$this->joinLocalKeys[$key]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $mainTable.' JOIN '.$pivotTable.' pivot ON '.implode(' AND ', $join); |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getPivotWhere(): string |
96
|
|
|
{ |
97
|
|
|
$paramList = []; |
98
|
|
|
foreach ($this->whereKeys as $key => $column) { |
99
|
|
|
$paramList[] = ' pivot.'.$column." = :param$key"; |
100
|
|
|
} |
101
|
|
|
return implode(" AND ", $paramList); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getPivotParams(array $primaryKeys): array |
106
|
|
|
{ |
107
|
|
|
$params = []; |
108
|
|
|
foreach ($primaryKeys as $key => $primaryKeyValue) { |
109
|
|
|
$params["param$key"] = $primaryKeyValue; |
110
|
|
|
} |
111
|
|
|
return $params; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.