1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Modifiers; |
8: | use PhpParser\Node; |
9: | use PhpParser\Node\Name; |
10: | use PhpParser\Node\Stmt; |
11: | |
12: | class Class_ extends Declaration { |
13: | protected string $name; |
14: | protected ?Name $extends = null; |
15: | |
16: | protected array $implements = []; |
17: | protected int $flags = 0; |
18: | |
19: | protected array $uses = []; |
20: | |
21: | protected array $constants = []; |
22: | |
23: | protected array $properties = []; |
24: | |
25: | protected array $methods = []; |
26: | |
27: | protected array $attributeGroups = []; |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | public function __construct(string $name) { |
35: | $this->name = $name; |
36: | } |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | public function extend($class) { |
46: | $this->extends = BuilderHelpers::normalizeName($class); |
47: | |
48: | return $this; |
49: | } |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | public function implement(...$interfaces) { |
59: | foreach ($interfaces as $interface) { |
60: | $this->implements[] = BuilderHelpers::normalizeName($interface); |
61: | } |
62: | |
63: | return $this; |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | public function makeAbstract() { |
72: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::ABSTRACT); |
73: | |
74: | return $this; |
75: | } |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | public function makeFinal() { |
83: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::FINAL); |
84: | |
85: | return $this; |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | public function makeReadonly() { |
94: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::READONLY); |
95: | |
96: | return $this; |
97: | } |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | public function addStmt($stmt) { |
107: | $stmt = BuilderHelpers::normalizeNode($stmt); |
108: | |
109: | if ($stmt instanceof Stmt\Property) { |
110: | $this->properties[] = $stmt; |
111: | } elseif ($stmt instanceof Stmt\ClassMethod) { |
112: | $this->methods[] = $stmt; |
113: | } elseif ($stmt instanceof Stmt\TraitUse) { |
114: | $this->uses[] = $stmt; |
115: | } elseif ($stmt instanceof Stmt\ClassConst) { |
116: | $this->constants[] = $stmt; |
117: | } else { |
118: | throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); |
119: | } |
120: | |
121: | return $this; |
122: | } |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | public function addAttribute($attribute) { |
132: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
133: | |
134: | return $this; |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | public function getNode(): PhpParser\Node { |
143: | return new Stmt\Class_($this->name, [ |
144: | 'flags' => $this->flags, |
145: | 'extends' => $this->extends, |
146: | 'implements' => $this->implements, |
147: | 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods), |
148: | 'attrGroups' => $this->attributeGroups, |
149: | ], $this->attributes); |
150: | } |
151: | } |
152: | |