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