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