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