Trait_.php 1.3 KB

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