1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\Src\Entity\DataTransferObjects; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ProcessInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\ReflectionHelper; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\PrimaryKey\IdFieldInterface; |
13
|
|
|
|
14
|
|
|
class CreateDtoBodyProcess implements ProcessInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DoctrineStaticMeta |
18
|
|
|
*/ |
19
|
|
|
private $dsm; |
20
|
|
|
/** |
21
|
|
|
* @var ReflectionHelper |
22
|
|
|
*/ |
23
|
|
|
private $reflectionHelper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $properties = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $setters = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $getters = []; |
39
|
|
|
/** |
40
|
|
|
* @var CodeHelper |
41
|
|
|
*/ |
42
|
|
|
private $codeHelper; |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $entityFqn; |
47
|
|
|
/** |
48
|
|
|
* @var NamespaceHelper |
49
|
|
|
*/ |
50
|
|
|
private $namespaceHelper; |
51
|
|
|
|
52
|
2 |
|
public function __construct( |
53
|
|
|
ReflectionHelper $reflectionHelper, |
54
|
|
|
CodeHelper $codeHelper, |
55
|
|
|
NamespaceHelper $namespaceHelper |
56
|
|
|
) { |
57
|
2 |
|
$this->reflectionHelper = $reflectionHelper; |
58
|
2 |
|
$this->codeHelper = $codeHelper; |
59
|
2 |
|
$this->namespaceHelper = $namespaceHelper; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
2 |
|
public function setEntityFqn(string $entityFqn) |
63
|
|
|
{ |
64
|
2 |
|
$this->entityFqn = $entityFqn; |
65
|
2 |
|
if (false === \ts\stringContains($entityFqn, '\\Entities\\')) { |
66
|
|
|
throw new \RuntimeException( |
67
|
|
|
'This does not look like an Entity FQN: ' . $entityFqn |
68
|
|
|
); |
69
|
|
|
} |
70
|
2 |
|
$this->dsm = new DoctrineStaticMeta($entityFqn); |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function run(File\FindReplace $findReplace): void |
76
|
|
|
{ |
77
|
2 |
|
$this->buildArraysOfCode(); |
78
|
2 |
|
$this->updateFileContents($findReplace); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
private function buildArraysOfCode() |
82
|
|
|
{ |
83
|
2 |
|
foreach ($this->dsm->getSetters() as $getterName => $setterName) { |
84
|
2 |
|
if ('getId' === $getterName) { |
85
|
2 |
|
continue; |
86
|
|
|
} |
87
|
2 |
|
$trait = $this->reflectionHelper->getTraitImplementingMethod( |
88
|
2 |
|
$this->dsm->getReflectionClass(), |
89
|
2 |
|
$setterName |
90
|
|
|
); |
91
|
2 |
|
$setter = $trait->getMethod($setterName); |
92
|
2 |
|
$property = $trait->getProperties(\ReflectionProperty::IS_PRIVATE)[0]->getName(); |
93
|
2 |
|
$type = $this->getPropertyTypeFromSetter($setter); |
94
|
2 |
|
$this->setProperty($property, $type); |
95
|
2 |
|
$this->setGetterFromPropertyAndType($getterName, $property, $type); |
96
|
2 |
|
$this->setSetterFromPropertyAndType($setterName, $property, $type); |
97
|
2 |
|
$this->addIssetMethodsForProperty($property, $type); |
98
|
|
|
} |
99
|
2 |
|
} |
100
|
|
|
|
101
|
2 |
|
private function getPropertyTypeFromSetter(\ReflectionMethod $setter): string |
102
|
|
|
{ |
103
|
|
|
/** |
104
|
|
|
* @var \ReflectionParameter $param |
105
|
|
|
*/ |
106
|
2 |
|
$param = current($setter->getParameters()); |
107
|
2 |
|
$type = $param->getType(); |
108
|
2 |
|
if (null !== $type) { |
109
|
2 |
|
$type = $type->getName(); |
110
|
2 |
|
if (!\in_array($type, ['string', 'bool', 'int', 'float'], true)) { |
111
|
2 |
|
$type = "\\$type"; |
112
|
|
|
} |
113
|
2 |
|
if ($param->allowsNull()) { |
114
|
2 |
|
$type = "?$type"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return (string)$type; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
private function setProperty(string $property, string $type): void |
122
|
|
|
{ |
123
|
2 |
|
$defaultValue = $this->getDefaultValueCodeForProperty($property); |
124
|
2 |
|
$type = $this->getPropertyVarType($type); |
125
|
2 |
|
$type = $this->makeIdTypesNullable($property, $type); |
126
|
|
|
|
127
|
2 |
|
$code = ''; |
128
|
2 |
|
$code .= "\n" . ' /**'; |
129
|
2 |
|
$code .= ('' !== $type) ? "\n" . ' * @var ' . $type : ''; |
130
|
2 |
|
$code .= "\n" . ' */'; |
131
|
2 |
|
$code .= "\n" . ' private $' . $property . ' = ' . $defaultValue . ';'; |
132
|
|
|
|
133
|
2 |
|
$this->properties[] = $code; |
134
|
2 |
|
} |
135
|
|
|
|
136
|
2 |
|
private function getDefaultValueCodeForProperty( |
137
|
|
|
string $property |
138
|
|
|
) { |
139
|
2 |
|
$defaultValueConst = 'DEFAULT_' . $this->codeHelper->consty($property); |
140
|
2 |
|
$fullValueString = $this->entityFqn . '::' . $defaultValueConst; |
141
|
2 |
|
if (\defined($fullValueString)) { |
142
|
2 |
|
return $this->dsm->getShortName() . '::' . $defaultValueConst; |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
return 'null'; |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
private function getPropertyVarType(string $type): string |
149
|
|
|
{ |
150
|
2 |
|
if (false === \ts\stringContains($type, '\\Entity\\Interfaces\\')) { |
151
|
2 |
|
return $type; |
152
|
|
|
} |
153
|
2 |
|
$types = []; |
154
|
2 |
|
if (0 === strpos($type, '?')) { |
155
|
|
|
$types[] = 'null'; |
156
|
|
|
$type = substr($type, 1); |
157
|
|
|
} |
158
|
2 |
|
$types[] = $type; |
159
|
2 |
|
$types[] = $this->namespaceHelper->getEntityDtoFqnFromEntityFqn( |
160
|
2 |
|
$this->namespaceHelper->getEntityFqnFromEntityInterfaceFqn($type) |
161
|
|
|
); |
162
|
|
|
|
163
|
2 |
|
return implode('|', $types); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
private function makeIdTypesNullable(string $property, string $type): string |
167
|
|
|
{ |
168
|
2 |
|
if (IdFieldInterface::PROP_ID === $property && 0 !== strpos($type, '?')) { |
169
|
|
|
$type = "?$type"; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
return $type; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
177
|
|
|
* @param string $getterName |
178
|
|
|
* @param string $property |
179
|
|
|
* @param string $type |
180
|
|
|
*/ |
181
|
2 |
|
private function setGetterFromPropertyAndType( |
182
|
|
|
string $getterName, |
183
|
|
|
string $property, |
184
|
|
|
string $type |
185
|
|
|
) { |
186
|
2 |
|
$type = $this->makeIdTypesNullable($property, $type); |
187
|
2 |
|
$code = ''; |
188
|
2 |
|
$code .= "\n public function $getterName()" . (('' !== $type) ? ": $type" : ''); |
189
|
2 |
|
$code .= "\n {"; |
190
|
2 |
|
$code .= $this->getGetterBody($property, $type); |
191
|
2 |
|
$code .= "\n }\n"; |
192
|
2 |
|
$this->getters[] = $code; |
193
|
2 |
|
} |
194
|
|
|
|
195
|
2 |
|
private function getGetterBody( |
196
|
|
|
string $property, |
197
|
|
|
string $type |
198
|
|
|
) { |
199
|
2 |
|
if ('\\' . Collection::class === $type) { |
200
|
2 |
|
return "\n return \$this->$property ?? \$this->$property = new ArrayCollection();"; |
201
|
|
|
} |
202
|
2 |
|
if (\ts\stringContains($type, '\\Entity\\Interfaces\\')) { |
203
|
2 |
|
$getterCode = ''; |
204
|
2 |
|
$getterCode .= "\n if(null === \$this->$property){"; |
205
|
2 |
|
$getterCode .= "\n return \$this->$property;"; |
206
|
2 |
|
$getterCode .= "\n }"; |
207
|
2 |
|
if (0 === strpos($type, '?')) { |
208
|
|
|
$type = substr($type, 1); |
209
|
|
|
} |
210
|
2 |
|
$getterCode .= "\n if(\$this->$property instanceof $type){"; |
211
|
2 |
|
$getterCode .= "\n return \$this->$property;"; |
212
|
2 |
|
$getterCode .= "\n }"; |
213
|
2 |
|
$getterCode .= "\n throw new \RuntimeException("; |
214
|
2 |
|
$getterCode .= "\n '\$this->$property is not an Entity, but is '. \get_class(\$this->$property)"; |
215
|
2 |
|
$getterCode .= "\n );"; |
216
|
|
|
|
217
|
2 |
|
return $getterCode; |
218
|
|
|
} |
219
|
|
|
|
220
|
2 |
|
return "\n return \$this->$property;"; |
221
|
|
|
} |
222
|
|
|
|
223
|
2 |
|
private function setSetterFromPropertyAndType( |
224
|
|
|
string $setterName, |
225
|
|
|
string $property, |
226
|
|
|
string $type |
227
|
|
|
) { |
228
|
2 |
|
$code = ''; |
229
|
2 |
|
$code .= "\n public function $setterName($type \$$property): self "; |
230
|
2 |
|
$code .= "\n {"; |
231
|
2 |
|
$code .= "\n \$this->$property = \$$property;"; |
232
|
2 |
|
$code .= "\n return \$this;"; |
233
|
2 |
|
$code .= "\n }\n"; |
234
|
2 |
|
$this->setters[] = $code; |
235
|
2 |
|
if (\ts\stringContains($type, '\\Entity\\Interfaces\\')) { |
236
|
2 |
|
$this->setDtoGetterAndSetterForEntityProperty($setterName, $property, $type); |
237
|
|
|
} |
238
|
2 |
|
} |
239
|
|
|
|
240
|
2 |
|
private function setDtoGetterAndSetterForEntityProperty( |
241
|
|
|
string $setterName, |
242
|
|
|
string $property, |
243
|
|
|
string $entityInterfaceFqn |
244
|
|
|
) { |
245
|
2 |
|
$dtoFqn = $this->namespaceHelper->getEntityDtoFqnFromEntityFqn( |
246
|
2 |
|
$this->namespaceHelper->getEntityFqnFromEntityInterfaceFqn($entityInterfaceFqn) |
247
|
|
|
); |
248
|
2 |
|
$setterCode = ''; |
249
|
2 |
|
$setterCode .= "\n public function ${setterName}Dto($dtoFqn \$$property): self "; |
250
|
2 |
|
$setterCode .= "\n {"; |
251
|
2 |
|
$setterCode .= "\n \$this->$property = \$$property;"; |
252
|
2 |
|
$setterCode .= "\n return \$this;"; |
253
|
2 |
|
$setterCode .= "\n }\n"; |
254
|
2 |
|
$this->setters[] = $setterCode; |
255
|
|
|
|
256
|
2 |
|
$getterName = 'get' . substr($setterName, 3); |
257
|
2 |
|
$getterCode = ''; |
258
|
2 |
|
$getterCode .= "\n public function $getterName" . "Dto(): $dtoFqn"; |
259
|
2 |
|
$getterCode .= "\n {"; |
260
|
2 |
|
$getterCode .= "\n if(null === \$this->$property){"; |
261
|
2 |
|
$getterCode .= "\n return \$this->$property;"; |
262
|
2 |
|
$getterCode .= "\n }"; |
263
|
2 |
|
if (0 === strpos($dtoFqn, '?')) { |
264
|
|
|
$dtoFqn = substr($dtoFqn, 1); |
265
|
|
|
} |
266
|
2 |
|
$getterCode .= "\n if(\$this->$property instanceof $dtoFqn){"; |
267
|
2 |
|
$getterCode .= "\n return \$this->$property;"; |
268
|
2 |
|
$getterCode .= "\n }"; |
269
|
2 |
|
$getterCode .= "\n throw new \RuntimeException("; |
270
|
2 |
|
$getterCode .= "\n '\$this->$property is not a DTO, but is '. \get_class(\$this->$property)"; |
271
|
2 |
|
$getterCode .= "\n );"; |
272
|
2 |
|
$getterCode .= "\n }\n"; |
273
|
2 |
|
$this->getters[] = $getterCode; |
274
|
2 |
|
} |
275
|
|
|
|
276
|
2 |
|
private function addIssetMethodsForProperty(string $property, string $type): void |
277
|
|
|
{ |
278
|
2 |
|
if (false === \ts\stringContains($type, '\\Entity\\Interfaces\\')) { |
279
|
2 |
|
return; |
280
|
|
|
} |
281
|
2 |
|
$methodName = 'isset' . ucfirst($property) . 'AsDto'; |
282
|
2 |
|
$getterCodeDto = ''; |
283
|
2 |
|
$getterCodeDto .= "\n public function $methodName(): bool"; |
284
|
2 |
|
$getterCodeDto .= "\n {"; |
285
|
2 |
|
$getterCodeDto .= "\n return \$this->$property instanceof DataTransferObjectInterface;"; |
286
|
2 |
|
$getterCodeDto .= "\n }\n"; |
287
|
2 |
|
$this->getters[] = $getterCodeDto; |
288
|
|
|
|
289
|
2 |
|
$methodName = 'isset' . ucfirst($property) . 'AsEntity'; |
290
|
2 |
|
$getterCodeEntity = ''; |
291
|
2 |
|
$getterCodeEntity .= "\n public function $methodName(): bool"; |
292
|
2 |
|
$getterCodeEntity .= "\n {"; |
293
|
2 |
|
$getterCodeEntity .= "\n return \$this->$property instanceof EntityInterface;"; |
294
|
2 |
|
$getterCodeEntity .= "\n }\n"; |
295
|
2 |
|
$this->getters[] = $getterCodeEntity; |
296
|
2 |
|
} |
297
|
|
|
|
298
|
2 |
|
private function updateFileContents( |
299
|
|
|
File\FindReplace $findReplace |
300
|
|
|
) { |
301
|
2 |
|
sort($this->properties, SORT_STRING); |
302
|
2 |
|
sort($this->getters, SORT_STRING); |
303
|
2 |
|
sort($this->setters, SORT_STRING); |
304
|
|
|
|
305
|
2 |
|
$body = implode("\n", $this->properties) . |
306
|
2 |
|
"\n\n" . |
307
|
2 |
|
implode("\n", $this->getters) . |
308
|
2 |
|
"\n" . |
309
|
2 |
|
implode("\n", $this->setters); |
310
|
|
|
|
311
|
2 |
|
$findReplace->findReplaceRegex('%{(.+)}%s', "{\n\$1\n$body\n}"); |
312
|
2 |
|
} |
313
|
|
|
} |
314
|
|
|
|