Trait_.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node\Stmt;
  5. class Trait_ extends Declaration
  6. {
  7. protected $name;
  8. protected $properties = array();
  9. protected $methods = array();
  10. /**
  11. * Creates an interface builder.
  12. *
  13. * @param string $name Name of the interface
  14. */
  15. public function __construct($name) {
  16. $this->name = $name;
  17. }
  18. /**
  19. * Adds a statement.
  20. *
  21. * @param Stmt|PhpParser\Builder $stmt The statement to add
  22. *
  23. * @return $this The builder instance (for fluid interface)
  24. */
  25. public function addStmt($stmt) {
  26. $stmt = $this->normalizeNode($stmt);
  27. if ($stmt instanceof Stmt\Property) {
  28. $this->properties[] = $stmt;
  29. } else if ($stmt instanceof Stmt\ClassMethod) {
  30. $this->methods[] = $stmt;
  31. } else {
  32. throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
  33. }
  34. return $this;
  35. }
  36. /**
  37. * Returns the built trait node.
  38. *
  39. * @return Stmt\Trait_ The built interface node
  40. */
  41. public function getNode() {
  42. return new Stmt\Trait_(
  43. $this->name, array(
  44. 'stmts' => array_merge($this->properties, $this->methods)
  45. ), $this->attributes
  46. );
  47. }
  48. }