Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser\Builder;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser;
|
Chris@13
|
6 use PhpParser\BuilderHelpers;
|
Chris@0
|
7 use PhpParser\Node\Name;
|
Chris@0
|
8 use PhpParser\Node\Stmt;
|
Chris@0
|
9
|
Chris@0
|
10 class Interface_ extends Declaration
|
Chris@0
|
11 {
|
Chris@0
|
12 protected $name;
|
Chris@13
|
13 protected $extends = [];
|
Chris@13
|
14 protected $constants = [];
|
Chris@13
|
15 protected $methods = [];
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Creates an interface builder.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @param string $name Name of the interface
|
Chris@0
|
21 */
|
Chris@13
|
22 public function __construct(string $name) {
|
Chris@0
|
23 $this->name = $name;
|
Chris@0
|
24 }
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Extends one or more interfaces.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param Name|string ...$interfaces Names of interfaces to extend
|
Chris@0
|
30 *
|
Chris@0
|
31 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
32 */
|
Chris@13
|
33 public function extend(...$interfaces) {
|
Chris@13
|
34 foreach ($interfaces as $interface) {
|
Chris@13
|
35 $this->extends[] = BuilderHelpers::normalizeName($interface);
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 return $this;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Adds a statement.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param Stmt|PhpParser\Builder $stmt The statement to add
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
47 */
|
Chris@0
|
48 public function addStmt($stmt) {
|
Chris@13
|
49 $stmt = BuilderHelpers::normalizeNode($stmt);
|
Chris@0
|
50
|
Chris@13
|
51 if ($stmt instanceof Stmt\ClassConst) {
|
Chris@13
|
52 $this->constants[] = $stmt;
|
Chris@13
|
53 } elseif ($stmt instanceof Stmt\ClassMethod) {
|
Chris@13
|
54 // we erase all statements in the body of an interface method
|
Chris@13
|
55 $stmt->stmts = null;
|
Chris@13
|
56 $this->methods[] = $stmt;
|
Chris@13
|
57 } else {
|
Chris@13
|
58 throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 return $this;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Returns the built interface node.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @return Stmt\Interface_ The built interface node
|
Chris@0
|
68 */
|
Chris@13
|
69 public function getNode() : PhpParser\Node {
|
Chris@13
|
70 return new Stmt\Interface_($this->name, [
|
Chris@0
|
71 'extends' => $this->extends,
|
Chris@0
|
72 'stmts' => array_merge($this->constants, $this->methods),
|
Chris@13
|
73 ], $this->attributes);
|
Chris@0
|
74 }
|
Chris@13
|
75 }
|