1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ClassConfig; |
4
|
|
|
|
5
|
|
|
use ClassConfig\Annotation\Config; |
6
|
|
|
use Nette\InvalidArgumentException; |
7
|
|
|
use Nette\PhpGenerator\ClassType; |
8
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
9
|
|
|
use Nette\PhpGenerator\Property; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ClassGenerator |
13
|
|
|
* @package ClassConfig |
14
|
|
|
*/ |
15
|
|
|
class ClassGenerator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ClassType |
19
|
|
|
*/ |
20
|
|
|
protected $class; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $ownerCanonicalClassName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $value |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
protected static function camelCase(string $value): string |
32
|
|
|
{ |
33
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $value)))); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $type |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function getTypeHint(string $type) |
41
|
|
|
{ |
42
|
|
|
if ('[]' === substr($type, -2)) { |
43
|
|
|
return 'array'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ('mixed' === $type) { |
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $type; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $type |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function getCommentTypeHint(string $type) |
58
|
|
|
{ |
59
|
|
|
if (preg_match('/^(.+?)((?:\[\])+)$/', $type, $match)) { |
60
|
|
|
$type = $match[1]; |
61
|
|
|
$brackets = $match[2]; |
62
|
|
|
} else { |
63
|
|
|
$brackets = ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!in_array($type, ['string', 'int', 'float', 'bool', 'mixed'], true)) { |
67
|
|
|
$this->class->getNamespace()->addUse($type); |
68
|
|
|
return $this->class->getNamespace()->unresolveName($type) . $brackets; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $type . $brackets; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getCanonicalClassName(): string |
78
|
|
|
{ |
79
|
|
|
return $this->class->getNamespace()->getName() . '\\' . $this->class->getName(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* ClassGenerator constructor. |
84
|
|
|
* |
85
|
|
|
* @param Config $annotation |
86
|
|
|
* @param string $className |
87
|
|
|
* @param string $classNamespace |
88
|
|
|
* @param string $ownerCanonicalClassName |
89
|
|
|
*/ |
90
|
|
|
public function __construct( |
91
|
|
|
Config $annotation, |
|
|
|
|
92
|
|
|
string $className, |
93
|
|
|
string $classNamespace, |
94
|
|
|
string $ownerCanonicalClassName |
95
|
|
|
) { |
96
|
|
|
$this->class = (new PhpNamespace($classNamespace))->addClass($className); |
97
|
|
|
$this->ownerCanonicalClassName = $ownerCanonicalClassName; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
|
|
public function __toString() |
104
|
|
|
{ |
105
|
|
|
$this->class |
106
|
|
|
->getNamespace() |
107
|
|
|
->addUse(AbstractConfig::class); |
108
|
|
|
|
109
|
|
|
$this->class |
110
|
|
|
->setFinal(true) |
111
|
|
|
->addExtend(AbstractConfig::class) |
112
|
|
|
->addComment( |
113
|
|
|
'THIS IS AN AUTOMATICALLY GENERATED FILE, PLEASE DO NOT MODIFY IT.' . PHP_EOL . |
114
|
|
|
'YOU MAY SAFELY DELETE THE FILE AS IT WILL BE REGENERATED ON-DEMAND.' |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$this->class |
118
|
|
|
->addMethod('end') |
119
|
|
|
->addComment( |
120
|
|
|
'@return ' . $this->getCommentTypeHint($this->ownerCanonicalClassName) |
121
|
|
|
)->setReturnType($this->getTypeHint($this->ownerCanonicalClassName)) |
122
|
|
|
->setBody( |
123
|
|
|
'/** @var ' . $this->getCommentTypeHint($this->ownerCanonicalClassName) . ' $owner */' . PHP_EOL . |
124
|
|
|
'$owner = $this->___owner;' . PHP_EOL . |
125
|
|
|
'return $owner;' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return '<?php' . PHP_EOL . PHP_EOL . (string) $this->class->getNamespace(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
* @param string $type |
134
|
|
|
* @param null $default |
135
|
|
|
* @return ClassGenerator |
136
|
|
|
*/ |
137
|
|
|
public function generateProperty(string $name, string $type, $default = null): ClassGenerator |
138
|
|
|
{ |
139
|
|
|
$this->class |
140
|
|
|
->addProperty('__' . $name . '__', [null, $default]) |
141
|
|
|
->addComment( |
142
|
|
|
'@var ' . $this->getCommentTypeHint($type . '[]') |
143
|
|
|
)->setVisibility('private'); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $name |
149
|
|
|
* @param string $type |
150
|
|
|
* @return ClassGenerator |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function generateGet(string $name, string $type): ClassGenerator |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$this->class |
155
|
|
|
->addMethod(static::camelCase('get_' . $name)) |
156
|
|
|
->addComment( |
157
|
|
|
'@return null|' . $this->getCommentTypeHint($type) |
158
|
|
|
)->setReturnType($this->getTypeHint($type)) |
159
|
|
|
->setReturnNullable(true) |
160
|
|
|
->setBody( |
161
|
|
|
'if (isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
162
|
|
|
' return $this->__' . $name . '__[0];' . PHP_EOL . |
163
|
|
|
'}' . PHP_EOL . |
164
|
|
|
'return $this->__' . $name . '__[1];' |
165
|
|
|
); |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $name |
171
|
|
|
* @param string $type |
172
|
|
|
* @return ClassGenerator |
173
|
|
|
*/ |
174
|
|
|
public function generateSet(string $name, string $type): ClassGenerator |
175
|
|
|
{ |
176
|
|
|
$this->class |
177
|
|
|
->addMethod(static::camelCase('set_' . $name)) |
178
|
|
|
->addComment( |
179
|
|
|
'@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
180
|
|
|
'@return ' . $this->class->getName() |
181
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
182
|
|
|
->setBody( |
183
|
|
|
'$this->__' . $name . '__[0] = $value;' . PHP_EOL . |
184
|
|
|
'return $this;' |
185
|
|
|
)->addParameter('value') |
186
|
|
|
->setTypeHint($this->getTypeHint($type)); |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $name |
192
|
|
|
* @return ClassGenerator |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function generateIsset(string $name): ClassGenerator |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$this->class |
197
|
|
|
->addMethod(static::camelCase('isset_' . $name)) |
198
|
|
|
->addComment( |
199
|
|
|
'@return bool' |
200
|
|
|
)->setReturnType('bool') |
201
|
|
|
->setBody( |
202
|
|
|
'return isset($this->__' . $name . '__[0]);' |
203
|
|
|
); |
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $name |
209
|
|
|
* @return ClassGenerator |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function generateUnset(string $name): ClassGenerator |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$this->class |
214
|
|
|
->addMethod(static::camelCase('unset_' . $name)) |
215
|
|
|
->addComment( |
216
|
|
|
'@return ' . $this->class->getName() |
217
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
218
|
|
|
->setBody( |
219
|
|
|
'unset($this->__' . $name . '__[0]);' . PHP_EOL . |
220
|
|
|
'return $this;' |
221
|
|
|
); |
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $name |
227
|
|
|
* @param string $type |
228
|
|
|
* @return ClassGenerator |
229
|
|
|
*/ |
230
|
|
|
public function generateListSet(string $name, string $type): ClassGenerator |
231
|
|
|
{ |
232
|
|
|
$this->class |
233
|
|
|
->addMethod(static::camelCase('set_' . $name)) |
234
|
|
|
->addComment( |
235
|
|
|
'@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL . |
236
|
|
|
'@return ' . $this->class->getName() |
237
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
238
|
|
|
->setBody( |
239
|
|
|
'$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL . |
240
|
|
|
'foreach ($values as $value) {' . PHP_EOL . |
241
|
|
|
' $this->' . static::camelCase('push_' . $name) . '($value);' . PHP_EOL . |
242
|
|
|
'}' . PHP_EOL . |
243
|
|
|
'return $this;' |
244
|
|
|
)->addParameter('values') |
245
|
|
|
->setTypeHint($this->getTypeHint($type)); |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $name |
251
|
|
|
* @param string $type |
252
|
|
|
* @return ClassGenerator |
253
|
|
|
*/ |
254
|
|
|
public function generateListGetAt(string $name, string $type): ClassGenerator |
255
|
|
|
{ |
256
|
|
|
$this->class |
257
|
|
|
->addMethod(static::camelCase('get_' . $name . '_at')) |
258
|
|
|
->addComment( |
259
|
|
|
'@param int $index' . PHP_EOL . |
260
|
|
|
'@return ' . $this->getCommentTypeHint($type) |
261
|
|
|
)->setReturnType($this->getTypeHint($type)) |
262
|
|
|
->setReturnNullable(true) |
263
|
|
|
->setBody( |
264
|
|
|
'if (isset($this->__' . $name . '__[0]) && array_key_exists($index, $this->__' . $name . '__[0])) {' . |
265
|
|
|
PHP_EOL . ' return $this->__' . $name . '__[0][$index];' . PHP_EOL . |
266
|
|
|
'}' . PHP_EOL . |
267
|
|
|
'return null;' |
268
|
|
|
)->addParameter('index') |
269
|
|
|
->setTypeHint('int'); |
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $name |
275
|
|
|
* @param string $type |
276
|
|
|
* @return ClassGenerator |
277
|
|
|
*/ |
278
|
|
|
public function generateListSetAt(string $name, string $type): ClassGenerator |
279
|
|
|
{ |
280
|
|
|
$method = $this->class |
281
|
|
|
->addMethod(static::camelCase('set_' . $name . '_at')) |
282
|
|
|
->addComment( |
283
|
|
|
'@param int $index' . PHP_EOL . |
284
|
|
|
'@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
285
|
|
|
'@return ' . $this->class->getName() |
286
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
287
|
|
|
->setBody( |
288
|
|
|
'if (0 > $index || (0 < $index && (!isset($this->__' . $name . '__[0]) ||' . PHP_EOL . |
289
|
|
|
' empty($this->__' . $name . '__[0])) || $index > count($this->__' . $name . '__[0]))) {' . PHP_EOL . |
290
|
|
|
' return $this;' . PHP_EOL . |
291
|
|
|
'}' . PHP_EOL . PHP_EOL . |
292
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
293
|
|
|
' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
294
|
|
|
'}' . PHP_EOL . PHP_EOL . |
295
|
|
|
'$this->__' . $name . '__[0][$index] = $value;' . PHP_EOL . |
296
|
|
|
'return $this;' |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$method->addParameter('index')->setTypeHint('int'); |
300
|
|
|
$method->addParameter('value')->setTypeHint($this->getTypeHint($type)); |
301
|
|
|
|
302
|
|
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param string $name |
307
|
|
|
* @param string $type |
308
|
|
|
* @return ClassGenerator |
309
|
|
|
*/ |
310
|
|
View Code Duplication |
public function generateListPush(string $name, string $type): ClassGenerator |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$this->class |
313
|
|
|
->addMethod(static::camelCase('push_' . $name)) |
314
|
|
|
->addComment( |
315
|
|
|
'@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
316
|
|
|
'@return ' . $this->class->getName() |
317
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
318
|
|
|
->setBody( |
319
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
320
|
|
|
' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
321
|
|
|
'}' . PHP_EOL . |
322
|
|
|
'array_push($this->__' . $name . '__[0], $value);' . PHP_EOL . |
323
|
|
|
'return $this;' |
324
|
|
|
)->addParameter('value') |
325
|
|
|
->setTypeHint($this->getTypeHint($type)); |
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param string $name |
331
|
|
|
* @param string $type |
332
|
|
|
* @return ClassGenerator |
333
|
|
|
*/ |
334
|
|
View Code Duplication |
public function generateListUnshift(string $name, string $type): ClassGenerator |
|
|
|
|
335
|
|
|
{ |
336
|
|
|
$this->class |
337
|
|
|
->addMethod(static::camelCase('unshift_' . $name)) |
338
|
|
|
->addComment( |
339
|
|
|
'@param ' . $this->getCommentTypeHint($type) . ' $value' . PHP_EOL . |
340
|
|
|
'@return ' . $this->class->getName() |
341
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
342
|
|
|
->setBody( |
343
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
344
|
|
|
' $this->__' . $name . '__[0] = [];' . PHP_EOL . |
345
|
|
|
'}' . PHP_EOL . |
346
|
|
|
'array_unshift($this->__' . $name . '__[0], $value);' . PHP_EOL . |
347
|
|
|
'return $this;' |
348
|
|
|
)->addParameter('value') |
349
|
|
|
->setTypeHint($this->getTypeHint($type)); |
350
|
|
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param string $name |
355
|
|
|
* @param string $type |
356
|
|
|
* @return ClassGenerator |
357
|
|
|
*/ |
358
|
|
View Code Duplication |
public function generateArrayPop(string $name, string $type): ClassGenerator |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
$this->class |
361
|
|
|
->addMethod(static::camelCase('pop_' . $name)) |
362
|
|
|
->addComment( |
363
|
|
|
'@return null|' . $this->getCommentTypeHint($type) |
364
|
|
|
)->setReturnType($this->getTypeHint($type)) |
365
|
|
|
->setReturnNullable(true) |
366
|
|
|
->setBody( |
367
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
368
|
|
|
' return null;' . PHP_EOL . |
369
|
|
|
'}' . PHP_EOL . |
370
|
|
|
'return array_pop($this->__' . $name . '__[0]);' |
371
|
|
|
); |
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param string $name |
377
|
|
|
* @param string $type |
378
|
|
|
* @return ClassGenerator |
379
|
|
|
*/ |
380
|
|
View Code Duplication |
public function generateArrayShift(string $name, string $type): ClassGenerator |
|
|
|
|
381
|
|
|
{ |
382
|
|
|
$this->class |
383
|
|
|
->addMethod(static::camelCase('shift_' . $name)) |
384
|
|
|
->addComment( |
385
|
|
|
'@return null|' . $this->getCommentTypeHint($type) |
386
|
|
|
)->setReturnType($this->getTypeHint($type)) |
387
|
|
|
->setReturnNullable(true) |
388
|
|
|
->setBody( |
389
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
390
|
|
|
' return null;' . PHP_EOL . |
391
|
|
|
'}' . PHP_EOL . |
392
|
|
|
'return array_shift($this->__' . $name . '__[0]);' |
393
|
|
|
); |
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param string $name |
399
|
|
|
* @return ClassGenerator |
400
|
|
|
*/ |
401
|
|
View Code Duplication |
public function generateArrayClear(string $name): ClassGenerator |
|
|
|
|
402
|
|
|
{ |
403
|
|
|
$this->class |
404
|
|
|
->addMethod(static::camelCase('clear_' . $name)) |
405
|
|
|
->addComment( |
406
|
|
|
'@return ' . $this->class->getName() |
407
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
408
|
|
|
->setBody( |
409
|
|
|
'unset($this->__' . $name . '__[0]);' . PHP_EOL . |
410
|
|
|
'return $this;' |
411
|
|
|
); |
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param string $name |
417
|
|
|
* @param string $type |
418
|
|
|
* @return ClassGenerator |
419
|
|
|
*/ |
420
|
|
|
public function generateConfigGet(string $name, string $type): ClassGenerator |
421
|
|
|
{ |
422
|
|
|
$this->class |
423
|
|
|
->addMethod(static::camelCase('get_' . $name)) |
424
|
|
|
->addComment( |
425
|
|
|
'@return null|' . $this->getCommentTypeHint($type) |
426
|
|
|
)->setReturnType($this->getTypeHint($type)) |
427
|
|
|
->setBody( |
428
|
|
|
'if (!isset($this->__' . $name . '__[0])) {' . PHP_EOL . |
429
|
|
|
' $this->__' . $name . '__[0] = new ' . $this->getCommentTypeHint($type) . |
430
|
|
|
'($this->___owner, $this, \'' . $name . '\');' . PHP_EOL . |
431
|
|
|
'}' . PHP_EOL . |
432
|
|
|
'return $this->__' . $name . '__[0];' |
433
|
|
|
); |
434
|
|
|
return $this; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param string $name |
439
|
|
|
* @return ClassGenerator |
440
|
|
|
*/ |
441
|
|
|
public function generateConfigSet(string $name): ClassGenerator |
442
|
|
|
{ |
443
|
|
|
$this->class |
444
|
|
|
->addMethod(static::camelCase('set_' . $name)) |
445
|
|
|
->addComment( |
446
|
|
|
'@return ' . $this->class->getName() |
447
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
448
|
|
|
->setBody( |
449
|
|
|
'// config is immutable' . PHP_EOL . |
450
|
|
|
'return $this;' |
451
|
|
|
); |
452
|
|
|
return $this; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @param string $name |
457
|
|
|
* @return ClassGenerator |
458
|
|
|
*/ |
459
|
|
View Code Duplication |
public function generateConfigIsset(string $name): ClassGenerator |
|
|
|
|
460
|
|
|
{ |
461
|
|
|
$this->class |
462
|
|
|
->addMethod(static::camelCase('isset_' . $name)) |
463
|
|
|
->addComment( |
464
|
|
|
'@return bool' |
465
|
|
|
)->setReturnType('bool') |
466
|
|
|
->setBody( |
467
|
|
|
'// config is immutable' . PHP_EOL . |
468
|
|
|
'return true;' |
469
|
|
|
); |
470
|
|
|
return $this; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* @param string $name |
475
|
|
|
* @return ClassGenerator |
476
|
|
|
*/ |
477
|
|
|
public function generateConfigUnset(string $name): ClassGenerator |
478
|
|
|
{ |
479
|
|
|
$this->class |
480
|
|
|
->addMethod(static::camelCase('unset_' . $name)) |
481
|
|
|
->addComment( |
482
|
|
|
'@return ' . $this->class->getName() |
483
|
|
|
)->setReturnType($this->getCanonicalClassName()) |
484
|
|
|
->setBody( |
485
|
|
|
'// config is immutable' . PHP_EOL . |
486
|
|
|
'return $this;' |
487
|
|
|
); |
488
|
|
|
return $this; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @return ClassGenerator |
493
|
|
|
*/ |
494
|
|
View Code Duplication |
public function generateMagicGet(): ClassGenerator |
|
|
|
|
495
|
|
|
{ |
496
|
|
|
$cases = ''; |
497
|
|
|
|
498
|
|
|
/** @var Property $property */ |
499
|
|
|
foreach ($this->class->getProperties() as $property) { |
500
|
|
|
$getter = static::camelCase('get_' . $property->getName()); |
501
|
|
|
|
502
|
|
|
try { |
503
|
|
|
$this->class->getMethod($getter); |
504
|
|
|
} catch (InvalidArgumentException $e) { |
505
|
|
|
continue; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$cases .= |
509
|
|
|
' case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL . |
510
|
|
|
' return $this->' . $getter . '();' . PHP_EOL . PHP_EOL; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
$this->class |
514
|
|
|
->addMethod('__get') |
515
|
|
|
->addComment( |
516
|
|
|
'@inheritDoc' |
517
|
|
|
)->setBody( |
518
|
|
|
'switch ($name) {' . PHP_EOL . |
519
|
|
|
$cases . |
520
|
|
|
' default:' . PHP_EOL . |
521
|
|
|
' return null;' . PHP_EOL . |
522
|
|
|
'}' |
523
|
|
|
)->addParameter('name'); |
524
|
|
|
return $this; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* @return ClassGenerator |
529
|
|
|
*/ |
530
|
|
|
public function generateMagicSet(): ClassGenerator |
531
|
|
|
{ |
532
|
|
|
$cases = ''; |
533
|
|
|
|
534
|
|
|
/** @var Property $property */ |
535
|
|
|
foreach ($this->class->getProperties() as $property) { |
536
|
|
|
$setter = static::camelCase('set_' . $property->getName()); |
537
|
|
|
|
538
|
|
|
try { |
539
|
|
|
$setterMethod = $this->class->getMethod($setter); |
540
|
|
|
} catch (InvalidArgumentException $e) { |
541
|
|
|
continue; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
$cases .= |
545
|
|
|
' case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL . |
546
|
|
|
' return $this->' . $setter . '(' . (0 < count($setterMethod->getParameters()) ? '$value' : '') . |
547
|
|
|
');' . PHP_EOL . PHP_EOL; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
$method = $this->class |
551
|
|
|
->addMethod('__set') |
552
|
|
|
->addComment( |
553
|
|
|
'@inheritDoc' |
554
|
|
|
)->setBody( |
555
|
|
|
'switch ($name) {' . PHP_EOL . |
556
|
|
|
$cases . |
557
|
|
|
' default:' . PHP_EOL . |
558
|
|
|
' return null;' . PHP_EOL . |
559
|
|
|
'}' |
560
|
|
|
); |
561
|
|
|
|
562
|
|
|
$method->addParameter('name'); |
563
|
|
|
$method->addParameter('value'); |
564
|
|
|
|
565
|
|
|
return $this; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @return ClassGenerator |
570
|
|
|
*/ |
571
|
|
View Code Duplication |
public function generateMagicIsset(): ClassGenerator |
|
|
|
|
572
|
|
|
{ |
573
|
|
|
$cases = ''; |
574
|
|
|
|
575
|
|
|
/** @var Property $property */ |
576
|
|
|
foreach ($this->class->getProperties() as $property) { |
577
|
|
|
$isset = static::camelCase('isset_' . $property->getName()); |
578
|
|
|
|
579
|
|
|
try { |
580
|
|
|
$this->class->getMethod($isset); |
581
|
|
|
} catch (InvalidArgumentException $e) { |
582
|
|
|
continue; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
$cases .= |
586
|
|
|
' case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL . |
587
|
|
|
' return $this->' . $isset . '();' . PHP_EOL . PHP_EOL; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
$this->class |
591
|
|
|
->addMethod('__isset') |
592
|
|
|
->addComment( |
593
|
|
|
'@inheritDoc' |
594
|
|
|
)->setBody( |
595
|
|
|
'switch ($name) {' . PHP_EOL . |
596
|
|
|
$cases . |
597
|
|
|
' default:' . PHP_EOL . |
598
|
|
|
' return false;' . PHP_EOL . |
599
|
|
|
'}' |
600
|
|
|
)->addParameter('name'); |
601
|
|
|
return $this; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* @return ClassGenerator |
606
|
|
|
*/ |
607
|
|
View Code Duplication |
public function generateMagicUnset(): ClassGenerator |
|
|
|
|
608
|
|
|
{ |
609
|
|
|
$cases = ''; |
610
|
|
|
|
611
|
|
|
/** @var Property $property */ |
612
|
|
|
foreach ($this->class->getProperties() as $property) { |
613
|
|
|
$unset = static::camelCase('unset_' . $property->getName()); |
614
|
|
|
|
615
|
|
|
try { |
616
|
|
|
$this->class->getMethod($unset); |
617
|
|
|
} catch (InvalidArgumentException $e) { |
618
|
|
|
continue; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
$cases .= |
622
|
|
|
' case \'' . substr($property->getName(), 2, -2) . '\':' . PHP_EOL . |
623
|
|
|
' return $this->' . $unset . '();' . PHP_EOL . PHP_EOL; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
$this->class |
627
|
|
|
->addMethod('__unset') |
628
|
|
|
->addComment( |
629
|
|
|
'@inheritDoc' |
630
|
|
|
)->setBody( |
631
|
|
|
'switch ($name) {' . PHP_EOL . |
632
|
|
|
$cases . |
633
|
|
|
' default:' . PHP_EOL . |
634
|
|
|
' return $this;' . PHP_EOL . |
635
|
|
|
'}' |
636
|
|
|
)->addParameter('name'); |
637
|
|
|
return $this; |
638
|
|
|
} |
639
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.