1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser\Builder; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Node; |
8: | use PhpParser\Node\Stmt; |
9: | |
10: | class TraitUse implements Builder { |
11: | |
12: | protected array $traits = []; |
13: | |
14: | protected array $adaptations = []; |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | public function __construct(...$traits) { |
22: | foreach ($traits as $trait) { |
23: | $this->and($trait); |
24: | } |
25: | } |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | public function and($trait) { |
35: | $this->traits[] = BuilderHelpers::normalizeName($trait); |
36: | return $this; |
37: | } |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | public function with($adaptation) { |
47: | $adaptation = BuilderHelpers::normalizeNode($adaptation); |
48: | |
49: | if (!$adaptation instanceof Stmt\TraitUseAdaptation) { |
50: | throw new \LogicException('Adaptation must have type TraitUseAdaptation'); |
51: | } |
52: | |
53: | $this->adaptations[] = $adaptation; |
54: | return $this; |
55: | } |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | public function getNode(): Node { |
63: | return new Stmt\TraitUse($this->traits, $this->adaptations); |
64: | } |
65: | } |
66: | |