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