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: /** @var Node\AttributeGroup[] */
21: protected $attributeGroups = [];
22: /** @var Identifier|Node\Name|Node\ComplexType */
23: protected $type;
24:
25: /**
26: * Creates a class constant builder
27: *
28: * @param string|Identifier $name Name
29: * @param Node\Expr|bool|null|int|float|string|array $value Value
30: */
31: public function __construct($name, $value) {
32: $this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
33: }
34:
35: /**
36: * Add another constant to const group
37: *
38: * @param string|Identifier $name Name
39: * @param Node\Expr|bool|null|int|float|string|array $value Value
40: *
41: * @return $this The builder instance (for fluid interface)
42: */
43: public function addConst($name, $value) {
44: $this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value));
45:
46: return $this;
47: }
48:
49: /**
50: * Makes the constant public.
51: *
52: * @return $this The builder instance (for fluid interface)
53: */
54: public function makePublic() {
55: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
56:
57: return $this;
58: }
59:
60: /**
61: * Makes the constant protected.
62: *
63: * @return $this The builder instance (for fluid interface)
64: */
65: public function makeProtected() {
66: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
67:
68: return $this;
69: }
70:
71: /**
72: * Makes the constant private.
73: *
74: * @return $this The builder instance (for fluid interface)
75: */
76: public function makePrivate() {
77: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
78:
79: return $this;
80: }
81:
82: /**
83: * Makes the constant final.
84: *
85: * @return $this The builder instance (for fluid interface)
86: */
87: public function makeFinal() {
88: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
89:
90: return $this;
91: }
92:
93: /**
94: * Sets doc comment for the constant.
95: *
96: * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
97: *
98: * @return $this The builder instance (for fluid interface)
99: */
100: public function setDocComment($docComment) {
101: $this->attributes = [
102: 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
103: ];
104:
105: return $this;
106: }
107:
108: /**
109: * Adds an attribute group.
110: *
111: * @param Node\Attribute|Node\AttributeGroup $attribute
112: *
113: * @return $this The builder instance (for fluid interface)
114: */
115: public function addAttribute($attribute) {
116: $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
117:
118: return $this;
119: }
120:
121: /**
122: * Sets the constant type.
123: *
124: * @param string|Node\Name|Identifier|Node\ComplexType $type
125: *
126: * @return $this
127: */
128: public function setType($type) {
129: $this->type = BuilderHelpers::normalizeType($type);
130:
131: return $this;
132: }
133:
134: /**
135: * Returns the built class node.
136: *
137: * @return Stmt\ClassConst The built constant node
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: