Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@13
|
5 use PhpParser\Node\Arg;
|
Chris@13
|
6 use PhpParser\Node\Expr;
|
Chris@13
|
7 use PhpParser\Node\Expr\BinaryOp\Concat;
|
Chris@13
|
8 use PhpParser\Node\Identifier;
|
Chris@13
|
9 use PhpParser\Node\Name;
|
Chris@13
|
10 use PhpParser\Node\Scalar\String_;
|
Chris@13
|
11 use PhpParser\Node\Stmt;
|
Chris@0
|
12 use PhpParser\Node\Stmt\Use_;
|
Chris@0
|
13
|
Chris@0
|
14 class BuilderFactory
|
Chris@0
|
15 {
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Creates a namespace builder.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @param null|string|Node\Name $name Name of the namespace
|
Chris@0
|
20 *
|
Chris@0
|
21 * @return Builder\Namespace_ The created namespace builder
|
Chris@0
|
22 */
|
Chris@13
|
23 public function namespace($name) : Builder\Namespace_ {
|
Chris@0
|
24 return new Builder\Namespace_($name);
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Creates a class builder.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param string $name Name of the class
|
Chris@0
|
31 *
|
Chris@0
|
32 * @return Builder\Class_ The created class builder
|
Chris@0
|
33 */
|
Chris@13
|
34 public function class(string $name) : Builder\Class_ {
|
Chris@0
|
35 return new Builder\Class_($name);
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Creates an interface builder.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param string $name Name of the interface
|
Chris@0
|
42 *
|
Chris@0
|
43 * @return Builder\Interface_ The created interface builder
|
Chris@0
|
44 */
|
Chris@13
|
45 public function interface(string $name) : Builder\Interface_ {
|
Chris@0
|
46 return new Builder\Interface_($name);
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Creates a trait builder.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @param string $name Name of the trait
|
Chris@0
|
53 *
|
Chris@0
|
54 * @return Builder\Trait_ The created trait builder
|
Chris@0
|
55 */
|
Chris@13
|
56 public function trait(string $name) : Builder\Trait_ {
|
Chris@0
|
57 return new Builder\Trait_($name);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Creates a method builder.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $name Name of the method
|
Chris@0
|
64 *
|
Chris@0
|
65 * @return Builder\Method The created method builder
|
Chris@0
|
66 */
|
Chris@13
|
67 public function method(string $name) : Builder\Method {
|
Chris@0
|
68 return new Builder\Method($name);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Creates a parameter builder.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param string $name Name of the parameter
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return Builder\Param The created parameter builder
|
Chris@0
|
77 */
|
Chris@13
|
78 public function param(string $name) : Builder\Param {
|
Chris@0
|
79 return new Builder\Param($name);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Creates a property builder.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @param string $name Name of the property
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return Builder\Property The created property builder
|
Chris@0
|
88 */
|
Chris@13
|
89 public function property(string $name) : Builder\Property {
|
Chris@0
|
90 return new Builder\Property($name);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Creates a function builder.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @param string $name Name of the function
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return Builder\Function_ The created function builder
|
Chris@0
|
99 */
|
Chris@13
|
100 public function function(string $name) : Builder\Function_ {
|
Chris@0
|
101 return new Builder\Function_($name);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Creates a namespace/class use builder.
|
Chris@0
|
106 *
|
Chris@0
|
107 * @param string|Node\Name Name to alias
|
Chris@0
|
108 *
|
Chris@0
|
109 * @return Builder\Use_ The create use builder
|
Chris@0
|
110 */
|
Chris@13
|
111 public function use($name) : Builder\Use_ {
|
Chris@0
|
112 return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@13
|
115 /**
|
Chris@13
|
116 * Creates node a for a literal value.
|
Chris@13
|
117 *
|
Chris@13
|
118 * @param Expr|bool|null|int|float|string|array $value $value
|
Chris@13
|
119 *
|
Chris@13
|
120 * @return Expr
|
Chris@13
|
121 */
|
Chris@13
|
122 public function val($value) : Expr {
|
Chris@13
|
123 return BuilderHelpers::normalizeValue($value);
|
Chris@13
|
124 }
|
Chris@13
|
125
|
Chris@13
|
126 /**
|
Chris@13
|
127 * Normalizes an argument list.
|
Chris@13
|
128 *
|
Chris@13
|
129 * Creates Arg nodes for all arguments and converts literal values to expressions.
|
Chris@13
|
130 *
|
Chris@13
|
131 * @param array $args List of arguments to normalize
|
Chris@13
|
132 *
|
Chris@13
|
133 * @return Arg[]
|
Chris@13
|
134 */
|
Chris@13
|
135 public function args(array $args) : array {
|
Chris@13
|
136 $normalizedArgs = [];
|
Chris@13
|
137 foreach ($args as $arg) {
|
Chris@13
|
138 if ($arg instanceof Arg) {
|
Chris@13
|
139 $normalizedArgs[] = $arg;
|
Chris@13
|
140 } else {
|
Chris@13
|
141 $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
|
Chris@13
|
142 }
|
Chris@13
|
143 }
|
Chris@13
|
144 return $normalizedArgs;
|
Chris@13
|
145 }
|
Chris@13
|
146
|
Chris@13
|
147 /**
|
Chris@13
|
148 * Creates a function call node.
|
Chris@13
|
149 *
|
Chris@13
|
150 * @param string|Name|Expr $name Function name
|
Chris@13
|
151 * @param array $args Function arguments
|
Chris@13
|
152 *
|
Chris@13
|
153 * @return Expr\FuncCall
|
Chris@13
|
154 */
|
Chris@13
|
155 public function funcCall($name, array $args = []) : Expr\FuncCall {
|
Chris@13
|
156 return new Expr\FuncCall(
|
Chris@13
|
157 BuilderHelpers::normalizeNameOrExpr($name),
|
Chris@13
|
158 $this->args($args)
|
Chris@13
|
159 );
|
Chris@13
|
160 }
|
Chris@13
|
161
|
Chris@13
|
162 /**
|
Chris@13
|
163 * Creates a method call node.
|
Chris@13
|
164 *
|
Chris@13
|
165 * @param Expr $var Variable the method is called on
|
Chris@13
|
166 * @param string|Identifier|Expr $name Method name
|
Chris@13
|
167 * @param array $args Method arguments
|
Chris@13
|
168 *
|
Chris@13
|
169 * @return Expr\MethodCall
|
Chris@13
|
170 */
|
Chris@13
|
171 public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
|
Chris@13
|
172 return new Expr\MethodCall(
|
Chris@13
|
173 $var,
|
Chris@13
|
174 BuilderHelpers::normalizeIdentifierOrExpr($name),
|
Chris@13
|
175 $this->args($args)
|
Chris@13
|
176 );
|
Chris@13
|
177 }
|
Chris@13
|
178
|
Chris@13
|
179 /**
|
Chris@13
|
180 * Creates a static method call node.
|
Chris@13
|
181 *
|
Chris@13
|
182 * @param string|Name|Expr $class Class name
|
Chris@13
|
183 * @param string|Identifier|Expr $name Method name
|
Chris@13
|
184 * @param array $args Method arguments
|
Chris@13
|
185 *
|
Chris@13
|
186 * @return Expr\StaticCall
|
Chris@13
|
187 */
|
Chris@13
|
188 public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
|
Chris@13
|
189 return new Expr\StaticCall(
|
Chris@13
|
190 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
191 BuilderHelpers::normalizeIdentifierOrExpr($name),
|
Chris@13
|
192 $this->args($args)
|
Chris@13
|
193 );
|
Chris@13
|
194 }
|
Chris@13
|
195
|
Chris@13
|
196 /**
|
Chris@13
|
197 * Creates an object creation node.
|
Chris@13
|
198 *
|
Chris@13
|
199 * @param string|Name|Expr $class Class name
|
Chris@13
|
200 * @param array $args Constructor arguments
|
Chris@13
|
201 *
|
Chris@13
|
202 * @return Expr\New_
|
Chris@13
|
203 */
|
Chris@13
|
204 public function new($class, array $args = []) : Expr\New_ {
|
Chris@13
|
205 return new Expr\New_(
|
Chris@13
|
206 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
207 $this->args($args)
|
Chris@13
|
208 );
|
Chris@13
|
209 }
|
Chris@13
|
210
|
Chris@13
|
211 /**
|
Chris@13
|
212 * Creates a constant fetch node.
|
Chris@13
|
213 *
|
Chris@13
|
214 * @param string|Name $name Constant name
|
Chris@13
|
215 *
|
Chris@13
|
216 * @return Expr\ConstFetch
|
Chris@13
|
217 */
|
Chris@13
|
218 public function constFetch($name) : Expr\ConstFetch {
|
Chris@13
|
219 return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
|
Chris@13
|
220 }
|
Chris@13
|
221
|
Chris@13
|
222 /**
|
Chris@13
|
223 * Creates a class constant fetch node.
|
Chris@13
|
224 *
|
Chris@13
|
225 * @param string|Name|Expr $class Class name
|
Chris@13
|
226 * @param string|Identifier $name Constant name
|
Chris@13
|
227 *
|
Chris@13
|
228 * @return Expr\ClassConstFetch
|
Chris@13
|
229 */
|
Chris@13
|
230 public function classConstFetch($class, $name): Expr\ClassConstFetch {
|
Chris@13
|
231 return new Expr\ClassConstFetch(
|
Chris@13
|
232 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
233 BuilderHelpers::normalizeIdentifier($name)
|
Chris@13
|
234 );
|
Chris@13
|
235 }
|
Chris@13
|
236
|
Chris@13
|
237 /**
|
Chris@13
|
238 * Creates nested Concat nodes from a list of expressions.
|
Chris@13
|
239 *
|
Chris@13
|
240 * @param Expr|string ...$exprs Expressions or literal strings
|
Chris@13
|
241 *
|
Chris@13
|
242 * @return Concat
|
Chris@13
|
243 */
|
Chris@13
|
244 public function concat(...$exprs) : Concat {
|
Chris@13
|
245 $numExprs = count($exprs);
|
Chris@13
|
246 if ($numExprs < 2) {
|
Chris@13
|
247 throw new \LogicException('Expected at least two expressions');
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@13
|
250 $lastConcat = $this->normalizeStringExpr($exprs[0]);
|
Chris@13
|
251 for ($i = 1; $i < $numExprs; $i++) {
|
Chris@13
|
252 $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
|
Chris@13
|
253 }
|
Chris@13
|
254 return $lastConcat;
|
Chris@13
|
255 }
|
Chris@13
|
256
|
Chris@13
|
257 /**
|
Chris@13
|
258 * @param string|Expr $expr
|
Chris@13
|
259 * @return Expr
|
Chris@13
|
260 */
|
Chris@13
|
261 private function normalizeStringExpr($expr) : Expr {
|
Chris@13
|
262 if ($expr instanceof Expr) {
|
Chris@13
|
263 return $expr;
|
Chris@13
|
264 }
|
Chris@13
|
265
|
Chris@13
|
266 if (\is_string($expr)) {
|
Chris@13
|
267 return new String_($expr);
|
Chris@13
|
268 }
|
Chris@13
|
269
|
Chris@13
|
270 throw new \LogicException('Expected string or Expr');
|
Chris@0
|
271 }
|
Chris@0
|
272 }
|