1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Node; |
8: | use PhpParser\Node\Name; |
9: | use PhpParser\Node\Stmt; |
10: | |
11: | class Interface_ extends Declaration |
12: | { |
13: | protected $name; |
14: | protected $extends = []; |
15: | protected $constants = []; |
16: | protected $methods = []; |
17: | |
18: | |
19: | protected $attributeGroups = []; |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | public function __construct(string $name) { |
27: | $this->name = $name; |
28: | } |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | public function extend(...$interfaces) { |
38: | foreach ($interfaces as $interface) { |
39: | $this->extends[] = BuilderHelpers::normalizeName($interface); |
40: | } |
41: | |
42: | return $this; |
43: | } |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | public function addStmt($stmt) { |
53: | $stmt = BuilderHelpers::normalizeNode($stmt); |
54: | |
55: | if ($stmt instanceof Stmt\ClassConst) { |
56: | $this->constants[] = $stmt; |
57: | } elseif ($stmt instanceof Stmt\ClassMethod) { |
58: | |
59: | $stmt->stmts = null; |
60: | $this->methods[] = $stmt; |
61: | } else { |
62: | throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); |
63: | } |
64: | |
65: | return $this; |
66: | } |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | public function addAttribute($attribute) { |
76: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
77: | |
78: | return $this; |
79: | } |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | public function getNode() : PhpParser\Node { |
87: | return new Stmt\Interface_($this->name, [ |
88: | 'extends' => $this->extends, |
89: | 'stmts' => array_merge($this->constants, $this->methods), |
90: | 'attrGroups' => $this->attributeGroups, |
91: | ], $this->attributes); |
92: | } |
93: | } |
94: | |