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