1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\Builder;
4:
5: use PhpParser;
6: use PhpParser\BuilderHelpers;
7: use PhpParser\Node;
8: use PhpParser\Node\Identifier;
9: use PhpParser\Node\Name;
10: use PhpParser\Node\Stmt;
11: use PhpParser\Node\ComplexType;
12:
13: class Property implements PhpParser\Builder
14: {
15: protected $name;
16:
17: protected $flags = 0;
18: protected $default = null;
19: protected $attributes = [];
20:
21: /** @var null|Identifier|Name|NullableType */
22: protected $type;
23:
24: /** @var Node\AttributeGroup[] */
25: protected $attributeGroups = [];
26:
27: /**
28: * Creates a property builder.
29: *
30: * @param string $name Name of the property
31: */
32: public function __construct(string $name) {
33: $this->name = $name;
34: }
35:
36: /**
37: * Makes the property public.
38: *
39: * @return $this The builder instance (for fluid interface)
40: */
41: public function makePublic() {
42: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
43:
44: return $this;
45: }
46:
47: /**
48: * Makes the property protected.
49: *
50: * @return $this The builder instance (for fluid interface)
51: */
52: public function makeProtected() {
53: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
54:
55: return $this;
56: }
57:
58: /**
59: * Makes the property private.
60: *
61: * @return $this The builder instance (for fluid interface)
62: */
63: public function makePrivate() {
64: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
65:
66: return $this;
67: }
68:
69: /**
70: * Makes the property static.
71: *
72: * @return $this The builder instance (for fluid interface)
73: */
74: public function makeStatic() {
75: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
76:
77: return $this;
78: }
79:
80: /**
81: * Makes the property readonly.
82: *
83: * @return $this The builder instance (for fluid interface)
84: */
85: public function makeReadonly() {
86: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_READONLY);
87:
88: return $this;
89: }
90:
91: /**
92: * Sets default value for the property.
93: *
94: * @param mixed $value Default value to use
95: *
96: * @return $this The builder instance (for fluid interface)
97: */
98: public function setDefault($value) {
99: $this->default = BuilderHelpers::normalizeValue($value);
100:
101: return $this;
102: }
103:
104: /**
105: * Sets doc comment for the property.
106: *
107: * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
108: *
109: * @return $this The builder instance (for fluid interface)
110: */
111: public function setDocComment($docComment) {
112: $this->attributes = [
113: 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
114: ];
115:
116: return $this;
117: }
118:
119: /**
120: * Sets the property type for PHP 7.4+.
121: *
122: * @param string|Name|Identifier|ComplexType $type
123: *
124: * @return $this
125: */
126: public function setType($type) {
127: $this->type = BuilderHelpers::normalizeType($type);
128:
129: return $this;
130: }
131:
132: /**
133: * Adds an attribute group.
134: *
135: * @param Node\Attribute|Node\AttributeGroup $attribute
136: *
137: * @return $this The builder instance (for fluid interface)
138: */
139: public function addAttribute($attribute) {
140: $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
141:
142: return $this;
143: }
144:
145: /**
146: * Returns the built class node.
147: *
148: * @return Stmt\Property The built property node
149: */
150: public function getNode() : PhpParser\Node {
151: return new Stmt\Property(
152: $this->flags !== 0 ? $this->flags : Stmt\Class_::MODIFIER_PUBLIC,
153: [
154: new Stmt\PropertyProperty($this->name, $this->default)
155: ],
156: $this->attributes,
157: $this->type,
158: $this->attributeGroups
159: );
160: }
161: }
162: