1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | use PhpParser\Node\Arg; |
6: | use PhpParser\Node\Expr; |
7: | use PhpParser\Node\Expr\BinaryOp\Concat; |
8: | use PhpParser\Node\Identifier; |
9: | use PhpParser\Node\Name; |
10: | use PhpParser\Node\Scalar\String_; |
11: | use PhpParser\Node\Stmt\Use_; |
12: | |
13: | class BuilderFactory |
14: | { |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | public function attribute($name, array $args = []) : Node\Attribute { |
24: | return new Node\Attribute( |
25: | BuilderHelpers::normalizeName($name), |
26: | $this->args($args) |
27: | ); |
28: | } |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | public function namespace($name) : Builder\Namespace_ { |
38: | return new Builder\Namespace_($name); |
39: | } |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | public function class(string $name) : Builder\Class_ { |
49: | return new Builder\Class_($name); |
50: | } |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | public function interface(string $name) : Builder\Interface_ { |
60: | return new Builder\Interface_($name); |
61: | } |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | public function trait(string $name) : Builder\Trait_ { |
71: | return new Builder\Trait_($name); |
72: | } |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | public function enum(string $name) : Builder\Enum_ { |
82: | return new Builder\Enum_($name); |
83: | } |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | public function useTrait(...$traits) : Builder\TraitUse { |
93: | return new Builder\TraitUse(...$traits); |
94: | } |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation { |
105: | if ($method === null) { |
106: | $method = $trait; |
107: | $trait = null; |
108: | } |
109: | |
110: | return new Builder\TraitUseAdaptation($trait, $method); |
111: | } |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | public function method(string $name) : Builder\Method { |
121: | return new Builder\Method($name); |
122: | } |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | public function param(string $name) : Builder\Param { |
132: | return new Builder\Param($name); |
133: | } |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | public function property(string $name) : Builder\Property { |
143: | return new Builder\Property($name); |
144: | } |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | public function function(string $name) : Builder\Function_ { |
154: | return new Builder\Function_($name); |
155: | } |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | public function use($name) : Builder\Use_ { |
165: | return new Builder\Use_($name, Use_::TYPE_NORMAL); |
166: | } |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | public function useFunction($name) : Builder\Use_ { |
176: | return new Builder\Use_($name, Use_::TYPE_FUNCTION); |
177: | } |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | public function useConst($name) : Builder\Use_ { |
187: | return new Builder\Use_($name, Use_::TYPE_CONSTANT); |
188: | } |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | public function classConst($name, $value) : Builder\ClassConst { |
199: | return new Builder\ClassConst($name, $value); |
200: | } |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | public function enumCase($name) : Builder\EnumCase { |
210: | return new Builder\EnumCase($name); |
211: | } |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | public function val($value) : Expr { |
221: | return BuilderHelpers::normalizeValue($value); |
222: | } |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | public function var($name) : Expr\Variable { |
232: | if (!\is_string($name) && !$name instanceof Expr) { |
233: | throw new \LogicException('Variable name must be string or Expr'); |
234: | } |
235: | |
236: | return new Expr\Variable($name); |
237: | } |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | public function args(array $args) : array { |
249: | $normalizedArgs = []; |
250: | foreach ($args as $key => $arg) { |
251: | if (!($arg instanceof Arg)) { |
252: | $arg = new Arg(BuilderHelpers::normalizeValue($arg)); |
253: | } |
254: | if (\is_string($key)) { |
255: | $arg->name = BuilderHelpers::normalizeIdentifier($key); |
256: | } |
257: | $normalizedArgs[] = $arg; |
258: | } |
259: | return $normalizedArgs; |
260: | } |
261: | |
262: | |
263: | |
264: | |
265: | |
266: | |
267: | |
268: | |
269: | |
270: | public function funcCall($name, array $args = []) : Expr\FuncCall { |
271: | return new Expr\FuncCall( |
272: | BuilderHelpers::normalizeNameOrExpr($name), |
273: | $this->args($args) |
274: | ); |
275: | } |
276: | |
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall { |
287: | return new Expr\MethodCall( |
288: | $var, |
289: | BuilderHelpers::normalizeIdentifierOrExpr($name), |
290: | $this->args($args) |
291: | ); |
292: | } |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: | |
300: | |
301: | |
302: | |
303: | public function staticCall($class, $name, array $args = []) : Expr\StaticCall { |
304: | return new Expr\StaticCall( |
305: | BuilderHelpers::normalizeNameOrExpr($class), |
306: | BuilderHelpers::normalizeIdentifierOrExpr($name), |
307: | $this->args($args) |
308: | ); |
309: | } |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | public function new($class, array $args = []) : Expr\New_ { |
320: | return new Expr\New_( |
321: | BuilderHelpers::normalizeNameOrExpr($class), |
322: | $this->args($args) |
323: | ); |
324: | } |
325: | |
326: | |
327: | |
328: | |
329: | |
330: | |
331: | |
332: | |
333: | public function constFetch($name) : Expr\ConstFetch { |
334: | return new Expr\ConstFetch(BuilderHelpers::normalizeName($name)); |
335: | } |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | |
345: | public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch { |
346: | return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name)); |
347: | } |
348: | |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | public function classConstFetch($class, $name): Expr\ClassConstFetch { |
358: | return new Expr\ClassConstFetch( |
359: | BuilderHelpers::normalizeNameOrExpr($class), |
360: | BuilderHelpers::normalizeIdentifier($name) |
361: | ); |
362: | } |
363: | |
364: | |
365: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | public function concat(...$exprs) : Concat { |
372: | $numExprs = count($exprs); |
373: | if ($numExprs < 2) { |
374: | throw new \LogicException('Expected at least two expressions'); |
375: | } |
376: | |
377: | $lastConcat = $this->normalizeStringExpr($exprs[0]); |
378: | for ($i = 1; $i < $numExprs; $i++) { |
379: | $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i])); |
380: | } |
381: | return $lastConcat; |
382: | } |
383: | |
384: | |
385: | |
386: | |
387: | |
388: | private function normalizeStringExpr($expr) : Expr { |
389: | if ($expr instanceof Expr) { |
390: | return $expr; |
391: | } |
392: | |
393: | if (\is_string($expr)) { |
394: | return new String_($expr); |
395: | } |
396: | |
397: | throw new \LogicException('Expected string or Expr'); |
398: | } |
399: | } |
400: | |