13
13
14
14
15
15
/**
16
- * Class/Interface/Trait description.
16
+ * Class/Interface/Trait/Enum description.
17
17
*
18
18
* @property Method[] $methods
19
19
* @property Property[] $properties
@@ -27,7 +27,8 @@ final class ClassType
27
27
public const
28
28
TYPE_CLASS = 'class ' ,
29
29
TYPE_INTERFACE = 'interface ' ,
30
- TYPE_TRAIT = 'trait ' ;
30
+ TYPE_TRAIT = 'trait ' ,
31
+ TYPE_ENUM = 'enum ' ;
31
32
32
33
public const
33
34
VISIBILITY_PUBLIC = 'public ' ,
@@ -67,6 +68,9 @@ final class ClassType
67
68
/** @var Method[] name => Method */
68
69
private $ methods = [];
69
70
71
+ /** @var EnumCase[] name => EnumCase */
72
+ private $ cases = [];
73
+
70
74
71
75
public static function class (string $ name = null , PhpNamespace $ namespace = null ): self
72
76
{
@@ -86,6 +90,12 @@ public static function trait(string $name = null, PhpNamespace $namespace = null
86
90
}
87
91
88
92
93
+ public static function enum (string $ name = null , PhpNamespace $ namespace = null ): self
94
+ {
95
+ return (new self ($ name , $ namespace ))->setType (self ::TYPE_ENUM );
96
+ }
97
+
98
+
89
99
/**
90
100
* @param string|object $class
91
101
*/
@@ -191,11 +201,17 @@ public function isTrait(): bool
191
201
}
192
202
193
203
204
+ public function isEnum (): bool
205
+ {
206
+ return $ this ->type === self ::TYPE_ENUM ;
207
+ }
208
+
209
+
194
210
/** @return static */
195
211
public function setType (string $ type ): self
196
212
{
197
- if (!in_array ($ type , [self ::TYPE_CLASS , self ::TYPE_INTERFACE , self ::TYPE_TRAIT ], true )) {
198
- throw new Nette \InvalidArgumentException ('Argument must be class|interface|trait. ' );
213
+ if (!in_array ($ type , [self ::TYPE_CLASS , self ::TYPE_INTERFACE , self ::TYPE_TRAIT , self :: TYPE_ENUM ], true )) {
214
+ throw new Nette \InvalidArgumentException ('Argument must be class|interface|trait|enum . ' );
199
215
}
200
216
$ this ->type = $ type ;
201
217
return $ this ;
@@ -351,7 +367,7 @@ public function removeTrait(string $name): self
351
367
352
368
353
369
/**
354
- * @param Method|Property|Constant $member
370
+ * @param Method|Property|Constant|EnumCase $member
355
371
* @return static
356
372
*/
357
373
public function addMember ($ member ): self
@@ -368,8 +384,11 @@ public function addMember($member): self
368
384
} elseif ($ member instanceof Constant) {
369
385
$ this ->consts [$ member ->getName ()] = $ member ;
370
386
387
+ } elseif ($ member instanceof EnumCase) {
388
+ $ this ->cases [$ member ->getName ()] = $ member ;
389
+
371
390
} else {
372
- throw new Nette \InvalidArgumentException ('Argument must be Method|Property|Constant. ' );
391
+ throw new Nette \InvalidArgumentException ('Argument must be Method|Property|Constant|EnumCase . ' );
373
392
}
374
393
375
394
return $ this ;
@@ -414,6 +433,44 @@ public function removeConstant(string $name): self
414
433
}
415
434
416
435
436
+ /**
437
+ * Sets cases to enum
438
+ * @param EnumCase[] $consts
439
+ * @return static
440
+ */
441
+ public function setCases (array $ cases ): self
442
+ {
443
+ (function (EnumCase ...$ cases ) {})(...$ cases );
444
+ $ this ->cases = [];
445
+ foreach ($ cases as $ case ) {
446
+ $ this ->cases [$ case ->getName ()] = $ case ;
447
+ }
448
+ return $ this ;
449
+ }
450
+
451
+
452
+ /** @return EnumCase[] */
453
+ public function getCases (): array
454
+ {
455
+ return $ this ->cases ;
456
+ }
457
+
458
+
459
+ /** Adds case to enum */
460
+ public function addCase (string $ name , $ value = null ): EnumCase
461
+ {
462
+ return $ this ->cases [$ name ] = (new EnumCase ($ name ))->setValue ($ value );
463
+ }
464
+
465
+
466
+ /** @return static */
467
+ public function removeCase (string $ name ): self
468
+ {
469
+ unset($ this ->cases [$ name ]);
470
+ return $ this ;
471
+ }
472
+
473
+
417
474
/**
418
475
* @param Property[] $props
419
476
* @return static
@@ -540,6 +597,9 @@ public function validate(): void
540
597
if ($ this ->abstract && $ this ->final ) {
541
598
throw new Nette \InvalidStateException ('Class cannot be abstract and final. ' );
542
599
600
+ } elseif ($ this ->isEnum () && ($ this ->abstract || $ this ->final || $ this ->extends || $ this ->properties )) {
601
+ throw new Nette \InvalidStateException ('Enum cannot be abstract or final or extends class or have properties. ' );
602
+
543
603
} elseif (!$ this ->name && ($ this ->abstract || $ this ->final )) {
544
604
throw new Nette \InvalidStateException ('Anonymous class cannot be abstract or final. ' );
545
605
}
@@ -559,6 +619,7 @@ private function validateNames(array $names): void
559
619
public function __clone ()
560
620
{
561
621
$ clone = function ($ item ) { return clone $ item ; };
622
+ $ this ->cases = array_map ($ clone , $ this ->cases );
562
623
$ this ->consts = array_map ($ clone , $ this ->consts );
563
624
$ this ->properties = array_map ($ clone , $ this ->properties );
564
625
$ this ->methods = array_map ($ clone , $ this ->methods );
0 commit comments