1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Node; |
8: | use PhpParser\Node\Identifier; |
9: | use PhpParser\Node\Name; |
10: | use PhpParser\Node\Stmt; |
11: | |
12: | class Enum_ extends Declaration { |
13: | protected string $name; |
14: | protected ?Identifier $scalarType = null; |
15: | |
16: | protected array $implements = []; |
17: | |
18: | protected array $uses = []; |
19: | |
20: | protected array $enumCases = []; |
21: | |
22: | protected array $constants = []; |
23: | |
24: | protected array $methods = []; |
25: | |
26: | protected array $attributeGroups = []; |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | public function __construct(string $name) { |
34: | $this->name = $name; |
35: | } |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | public function setScalarType($scalarType) { |
45: | $this->scalarType = BuilderHelpers::normalizeType($scalarType); |
46: | |
47: | return $this; |
48: | } |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | public function implement(...$interfaces) { |
58: | foreach ($interfaces as $interface) { |
59: | $this->implements[] = BuilderHelpers::normalizeName($interface); |
60: | } |
61: | |
62: | return $this; |
63: | } |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | public function addStmt($stmt) { |
73: | $stmt = BuilderHelpers::normalizeNode($stmt); |
74: | |
75: | if ($stmt instanceof Stmt\EnumCase) { |
76: | $this->enumCases[] = $stmt; |
77: | } elseif ($stmt instanceof Stmt\ClassMethod) { |
78: | $this->methods[] = $stmt; |
79: | } elseif ($stmt instanceof Stmt\TraitUse) { |
80: | $this->uses[] = $stmt; |
81: | } elseif ($stmt instanceof Stmt\ClassConst) { |
82: | $this->constants[] = $stmt; |
83: | } else { |
84: | throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); |
85: | } |
86: | |
87: | return $this; |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | public function addAttribute($attribute) { |
98: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
99: | |
100: | return $this; |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function getNode(): PhpParser\Node { |
109: | return new Stmt\Enum_($this->name, [ |
110: | 'scalarType' => $this->scalarType, |
111: | 'implements' => $this->implements, |
112: | 'stmts' => array_merge($this->uses, $this->enumCases, $this->constants, $this->methods), |
113: | 'attrGroups' => $this->attributeGroups, |
114: | ], $this->attributes); |
115: | } |
116: | } |
117: | |