|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
9
|
|
|
use TheCodingMachine\TDBM\AlterableResultIterator; |
|
10
|
|
|
use TheCodingMachine\TDBM\Schema\ForeignKey; |
|
11
|
|
|
use TheCodingMachine\TDBM\TDBMException; |
|
12
|
|
|
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser; |
|
13
|
|
|
use TheCodingMachine\TDBM\Utils\Annotation; |
|
14
|
|
|
use Zend\Code\Generator\AbstractMemberGenerator; |
|
15
|
|
|
use Zend\Code\Generator\DocBlock\Tag\ReturnTag; |
|
16
|
|
|
use Zend\Code\Generator\MethodGenerator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Represents a method to get a list of beans from a direct foreign key pointing to our bean. |
|
20
|
|
|
*/ |
|
21
|
|
|
class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ForeignKeyAnalyzerTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ForeignKeyConstraint |
|
27
|
|
|
*/ |
|
28
|
|
|
private $foreignKey; |
|
29
|
|
|
|
|
30
|
|
|
private $useAlternateName = false; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Table |
|
33
|
|
|
*/ |
|
34
|
|
|
private $mainTable; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var NamingStrategyInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $namingStrategy; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var AnnotationParser |
|
41
|
|
|
*/ |
|
42
|
|
|
private $annotationParser; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
46
|
|
|
* @param Table $mainTable The main table that is pointed to |
|
47
|
|
|
* @param NamingStrategyInterface $namingStrategy |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy, AnnotationParser $annotationParser) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->foreignKey = $fk; |
|
52
|
|
|
$this->mainTable = $mainTable; |
|
53
|
|
|
$this->namingStrategy = $namingStrategy; |
|
54
|
|
|
$this->annotationParser = $annotationParser; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the name of the method to be generated. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getName() : string |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$this->useAlternateName) { |
|
65
|
|
|
return 'get'.TDBMDaoGenerator::toCamelCase($this->foreignKey->getLocalTableName()); |
|
66
|
|
|
} else { |
|
67
|
|
|
$methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->foreignKey->getLocalTableName()).'By'; |
|
68
|
|
|
|
|
69
|
|
|
$camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->foreignKey->getUnquotedLocalColumns()); |
|
70
|
|
|
|
|
71
|
|
|
$methodName .= implode('And', $camelizedColumns); |
|
72
|
|
|
|
|
73
|
|
|
return $methodName; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the name of the class that will be returned by the getter (short name). |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getBeanClassName(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->namingStrategy->getBeanClassName($this->foreignKey->getLocalTableName()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Requests the use of an alternative name for this method. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function useAlternativeName(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->useAlternateName = true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the code of the method. |
|
97
|
|
|
* |
|
98
|
|
|
* @return MethodGenerator[] |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getCode() : array |
|
101
|
|
|
{ |
|
102
|
|
|
$beanClass = $this->getBeanClassName(); |
|
103
|
|
|
|
|
104
|
|
|
$getter = new MethodGenerator($this->getName()); |
|
105
|
|
|
$getter->setDocBlock(sprintf('Returns the list of %s pointing to this bean via the %s column.', $beanClass, implode(', ', $this->foreignKey->getUnquotedLocalColumns()))); |
|
106
|
|
|
$getter->getDocBlock()->setTag(new ReturnTag([ |
|
107
|
|
|
$beanClass.'[]', |
|
108
|
|
|
'\\'.AlterableResultIterator::class |
|
109
|
|
|
])); |
|
110
|
|
|
$getter->setReturnType(AlterableResultIterator::class); |
|
111
|
|
|
|
|
112
|
|
|
$tdbmFk = ForeignKey::createFromFk($this->foreignKey); |
|
113
|
|
|
|
|
114
|
|
|
$code = sprintf( |
|
115
|
|
|
'return $this->retrieveManyToOneRelationshipsStorage(%s, %s, %s);', |
|
116
|
|
|
var_export($this->foreignKey->getLocalTableName(), true), |
|
117
|
|
|
var_export($tdbmFk->getCacheKey(), true), |
|
118
|
|
|
$this->getFilters($this->foreignKey) |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$getter->setBody($code); |
|
122
|
|
|
|
|
123
|
|
|
if ($this->isProtected()) { |
|
124
|
|
|
$getter->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return [ $getter ]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function getFilters(ForeignKeyConstraint $fk) : string |
|
131
|
|
|
{ |
|
132
|
|
|
$counter = 0; |
|
133
|
|
|
$parameters = []; |
|
134
|
|
|
|
|
135
|
|
|
$fkForeignColumns = $fk->getUnquotedForeignColumns(); |
|
136
|
|
|
|
|
137
|
|
|
foreach ($fk->getUnquotedLocalColumns() as $columnName) { |
|
138
|
|
|
$fkColumn = $fkForeignColumns[$counter]; |
|
139
|
|
|
$parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($fkColumn, true), var_export($this->foreignKey->getForeignTableName(), true)); |
|
140
|
|
|
++$counter; |
|
141
|
|
|
} |
|
142
|
|
|
$parametersCode = '['.implode(', ', $parameters).']'; |
|
143
|
|
|
|
|
144
|
|
|
return $parametersCode; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Returns an array of classes that needs a "use" for this method. |
|
149
|
|
|
* |
|
150
|
|
|
* @return string[] |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getUsedClasses() : array |
|
153
|
|
|
{ |
|
154
|
|
|
return [$this->getBeanClassName()]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the code to past in jsonSerialize. |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getJsonSerializeCode() : string |
|
163
|
|
|
{ |
|
164
|
|
|
/** @var Annotation\JsonCollection|null $jsonCollection */ |
|
165
|
|
|
$jsonCollection = $this->findAnnotation(Annotation\JsonCollection::class); |
|
166
|
|
|
if ($jsonCollection === null) { |
|
167
|
|
|
return ''; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** @var Annotation\JsonFormat|null $jsonFormat */ |
|
171
|
|
|
$jsonFormat = $this->findAnnotation(Annotation\JsonFormat::class); |
|
172
|
|
|
if ($jsonFormat !== null) { |
|
173
|
|
|
$method = $jsonFormat->method ?? 'get' . ucfirst($jsonFormat->property); |
|
174
|
|
|
$format = "$method()"; |
|
175
|
|
|
} else { |
|
176
|
|
|
$stopRecursion = $this->findAnnotation(Annotation\JsonRecursive::class) ? '' : 'true'; |
|
177
|
|
|
$format = "jsonSerialize($stopRecursion)"; |
|
178
|
|
|
} |
|
179
|
|
|
$isIncluded = $this->findAnnotation(Annotation\JsonInclude::class) !== null; |
|
180
|
|
|
$index = $jsonCollection->key ?: lcfirst(TDBMDaoGenerator::toCamelCase($this->foreignKey->getLocalTableName())); |
|
181
|
|
|
$class = $this->getBeanClassName(); |
|
182
|
|
|
$variableName = '$' . TDBMDaoGenerator::toVariableName($class); |
|
183
|
|
|
$getter = $this->getName(); |
|
184
|
|
|
$code = <<<PHP |
|
185
|
|
|
\$array['$index'] = array_map(function ($class $variableName) { |
|
186
|
|
|
return ${variableName}->$format; |
|
187
|
|
|
}, \$this->$getter()->toArray()); |
|
188
|
|
|
PHP; |
|
189
|
|
|
if (!$isIncluded) { |
|
190
|
|
|
$code = preg_replace('(\n)', '\0 ', $code); |
|
191
|
|
|
$code = <<<PHP |
|
192
|
|
|
if (!\$stopRecursion) { |
|
193
|
|
|
$code |
|
194
|
|
|
}; |
|
195
|
|
|
PHP; |
|
196
|
|
|
} |
|
197
|
|
|
return $code; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return ForeignKeyConstraint |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getForeignKey(): ForeignKeyConstraint |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->foreignKey; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns the table that is pointed to. |
|
210
|
|
|
* @return Table |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getMainTable(): Table |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->mainTable; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
private function isProtected(): bool |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->findAnnotation(Annotation\ProtectedOneToMany::class) !== null; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param string $type |
|
224
|
|
|
* @return null|object |
|
225
|
|
|
*/ |
|
226
|
|
|
private function findAnnotation(string $type) |
|
227
|
|
|
{ |
|
228
|
|
|
foreach ($this->getAnnotations() as $annotations) { |
|
229
|
|
|
$annotation = $annotations->findAnnotation($type); |
|
230
|
|
|
if ($annotation !== null) { |
|
231
|
|
|
return $annotation; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
return null; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|