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