Interface_.php 2.0 KB

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