Interface.php 2.4 KB

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