|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Mouf\Database\TDBM\Utils; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a method to get a list of beans from a direct foreign key pointing to our bean. |
|
12
|
|
|
*/ |
|
13
|
|
|
class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ForeignKeyConstraint |
|
17
|
|
|
*/ |
|
18
|
|
|
private $fk; |
|
19
|
|
|
|
|
20
|
|
|
private $useAlternateName = false; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Table |
|
23
|
|
|
*/ |
|
24
|
|
|
private $mainTable; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ForeignKeyConstraint $fk The foreign key pointing to our bean. |
|
28
|
|
|
* @param Table $mainTable The main table that is pointed to. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ForeignKeyConstraint $fk, Table $mainTable) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->fk = $fk; |
|
33
|
|
|
$this->mainTable = $mainTable; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the name of the method to be generated. |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getName() : string |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$this->useAlternateName) { |
|
44
|
|
|
return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
45
|
|
|
} else { |
|
46
|
|
|
$methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
47
|
|
|
|
|
48
|
|
|
$camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
49
|
|
|
|
|
50
|
|
|
$methodName .= implode('And', $camelizedColumns); |
|
51
|
|
|
|
|
52
|
|
|
return $methodName; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Requests the use of an alternative name for this method. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function useAlternativeName() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->useAlternateName = true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the code of the method. |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getCode() : string |
|
70
|
|
|
{ |
|
71
|
|
|
$code = ''; |
|
72
|
|
|
|
|
73
|
|
|
$getterCode = ' /** |
|
74
|
|
|
* Returns the list of %s pointing to this bean via the %s column. |
|
75
|
|
|
* |
|
76
|
|
|
* @return %s[]|AlterableResultIterator |
|
77
|
|
|
*/ |
|
78
|
|
|
public function %s() : AlterableResultIterator |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->retrieveManyToOneRelationshipsStorage(%s, %s, %s, %s); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
'; |
|
84
|
|
|
|
|
85
|
|
|
$beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName()); |
|
86
|
|
|
$code .= sprintf($getterCode, |
|
87
|
|
|
$beanClass, |
|
88
|
|
|
implode(', ', $this->fk->getColumns()), |
|
89
|
|
|
$beanClass, |
|
90
|
|
|
$this->getName(), |
|
91
|
|
|
var_export($this->fk->getLocalTableName(), true), |
|
92
|
|
|
var_export($this->fk->getName(), true), |
|
93
|
|
|
var_export($this->fk->getLocalTableName(), true), |
|
94
|
|
|
$this->getFilters($this->fk) |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
return $code; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getFilters(ForeignKeyConstraint $fk) : string |
|
101
|
|
|
{ |
|
102
|
|
|
$counter = 0; |
|
103
|
|
|
$parameters = []; |
|
104
|
|
|
|
|
105
|
|
|
$pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($fk->getLocalColumns() as $columnName) { |
|
108
|
|
|
$pkColumn = $pkColumns[$counter]; |
|
109
|
|
|
$parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
110
|
|
|
++$counter; |
|
111
|
|
|
} |
|
112
|
|
|
$parametersCode = '['.implode(', ', $parameters).']'; |
|
113
|
|
|
|
|
114
|
|
|
return $parametersCode; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns an array of classes that needs a "use" for this method. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string[] |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getUsedClasses() : array |
|
123
|
|
|
{ |
|
124
|
|
|
return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the code to past in jsonSerialize. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getJsonSerializeCode() : string |
|
133
|
|
|
{ |
|
134
|
|
|
return ''; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|