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