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