Interface_.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 $interface Name of interface to extend
  24. * @param Name|string $... More interfaces to extend
  25. *
  26. * @return $this The builder instance (for fluid interface)
  27. */
  28. public function extend() {
  29. foreach (func_get_args() as $interface) {
  30. $this->extends[] = $this->normalizeName($interface);
  31. }
  32. return $this;
  33. }
  34. /**
  35. * Adds a statement.
  36. *
  37. * @param Stmt|PhpParser\Builder $stmt The statement to add
  38. *
  39. * @return $this The builder instance (for fluid interface)
  40. */
  41. public function addStmt($stmt) {
  42. $stmt = $this->normalizeNode($stmt);
  43. $type = $stmt->getType();
  44. switch ($type) {
  45. case 'Stmt_ClassConst':
  46. $this->constants[] = $stmt;
  47. break;
  48. case 'Stmt_ClassMethod':
  49. // we erase all statements in the body of an interface method
  50. $stmt->stmts = null;
  51. $this->methods[] = $stmt;
  52. break;
  53. default:
  54. throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Returns the built interface node.
  60. *
  61. * @return Stmt\Interface_ The built interface node
  62. */
  63. public function getNode() {
  64. return new Stmt\Interface_($this->name, array(
  65. 'extends' => $this->extends,
  66. 'stmts' => array_merge($this->constants, $this->methods),
  67. ), $this->attributes);
  68. }
  69. }