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: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | public function __construct($name, $value) { |
30: | $this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))]; |
31: | } |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | public function addConst($name, $value) { |
42: | $this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value)); |
43: | |
44: | return $this; |
45: | } |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | public function makePublic() { |
53: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC); |
54: | |
55: | return $this; |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | public function makeProtected() { |
64: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED); |
65: | |
66: | return $this; |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | public function makePrivate() { |
75: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE); |
76: | |
77: | return $this; |
78: | } |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | public function makeFinal() { |
86: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL); |
87: | |
88: | return $this; |
89: | } |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | public function setDocComment($docComment) { |
99: | $this->attributes = [ |
100: | 'comments' => [BuilderHelpers::normalizeDocComment($docComment)] |
101: | ]; |
102: | |
103: | return $this; |
104: | } |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | public function addAttribute($attribute) { |
114: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
115: | |
116: | return $this; |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function getNode(): PhpParser\Node { |
125: | return new Stmt\ClassConst( |
126: | $this->constants, |
127: | $this->flags, |
128: | $this->attributes, |
129: | $this->attributeGroups |
130: | ); |
131: | } |
132: | } |
133: | |