1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PhpParser\Builder; |
6: | |
7: | use PhpParser; |
8: | use PhpParser\BuilderHelpers; |
9: | use PhpParser\Modifiers; |
10: | use PhpParser\Node; |
11: | use PhpParser\Node\Const_; |
12: | use PhpParser\Node\Identifier; |
13: | use PhpParser\Node\Stmt; |
14: | |
15: | class ClassConst implements PhpParser\Builder { |
16: | protected int $flags = 0; |
17: | |
18: | protected array $attributes = []; |
19: | |
20: | protected array $constants = []; |
21: | |
22: | |
23: | protected array $attributeGroups = []; |
24: | |
25: | protected ?Node $type = null; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | public function __construct($name, $value) { |
34: | $this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))]; |
35: | } |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | public function addConst($name, $value) { |
46: | $this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value)); |
47: | |
48: | return $this; |
49: | } |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public function makePublic() { |
57: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
58: | |
59: | return $this; |
60: | } |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | public function makeProtected() { |
68: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
69: | |
70: | return $this; |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | public function makePrivate() { |
79: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
80: | |
81: | return $this; |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | public function makeFinal() { |
90: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL); |
91: | |
92: | return $this; |
93: | } |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public function setDocComment($docComment) { |
103: | $this->attributes = [ |
104: | 'comments' => [BuilderHelpers::normalizeDocComment($docComment)] |
105: | ]; |
106: | |
107: | return $this; |
108: | } |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | public function addAttribute($attribute) { |
118: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
119: | |
120: | return $this; |
121: | } |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | public function setType($type) { |
131: | $this->type = BuilderHelpers::normalizeType($type); |
132: | |
133: | return $this; |
134: | } |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | public function getNode(): PhpParser\Node { |
142: | return new Stmt\ClassConst( |
143: | $this->constants, |
144: | $this->flags, |
145: | $this->attributes, |
146: | $this->attributeGroups, |
147: | $this->type |
148: | ); |
149: | } |
150: | } |
151: | |