Geen omschrijving

NodeAbstract.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace PhpParser;
  3. abstract class NodeAbstract implements Node, \IteratorAggregate
  4. {
  5. protected $subNodes;
  6. protected $attributes;
  7. /**
  8. * Creates a Node.
  9. *
  10. * @param array $subNodes Array of sub nodes
  11. * @param array $attributes Array of attributes
  12. */
  13. public function __construct(array $subNodes = array(), array $attributes = array()) {
  14. $this->subNodes = $subNodes;
  15. $this->attributes = $attributes;
  16. }
  17. /**
  18. * Gets the type of the node.
  19. *
  20. * @return string Type of the node
  21. */
  22. public function getType() {
  23. return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
  24. }
  25. /**
  26. * Gets the names of the sub nodes.
  27. *
  28. * @return array Names of sub nodes
  29. */
  30. public function getSubNodeNames() {
  31. return array_keys($this->subNodes);
  32. }
  33. /**
  34. * Gets line the node started in.
  35. *
  36. * @return int Line
  37. */
  38. public function getLine() {
  39. return $this->getAttribute('startLine', -1);
  40. }
  41. /**
  42. * Sets line the node started in.
  43. *
  44. * @param int $line Line
  45. */
  46. public function setLine($line) {
  47. $this->setAttribute('startLine', (int) $line);
  48. }
  49. /**
  50. * Gets the doc comment of the node.
  51. *
  52. * The doc comment has to be the last comment associated with the node.
  53. *
  54. * @return null|Comment\Doc Doc comment object or null
  55. */
  56. public function getDocComment() {
  57. $comments = $this->getAttribute('comments');
  58. if (!$comments) {
  59. return null;
  60. }
  61. $lastComment = $comments[count($comments) - 1];
  62. if (!$lastComment instanceof Comment\Doc) {
  63. return null;
  64. }
  65. return $lastComment;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function setAttribute($key, $value) {
  71. $this->attributes[$key] = $value;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function hasAttribute($key) {
  77. return array_key_exists($key, $this->attributes);
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function &getAttribute($key, $default = null) {
  83. if (!array_key_exists($key, $this->attributes)) {
  84. return $default;
  85. } else {
  86. return $this->attributes[$key];
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function getAttributes() {
  93. return $this->attributes;
  94. }
  95. /* Magic interfaces */
  96. public function &__get($name) {
  97. return $this->subNodes[$name];
  98. }
  99. public function __set($name, $value) {
  100. $this->subNodes[$name] = $value;
  101. }
  102. public function __isset($name) {
  103. return isset($this->subNodes[$name]);
  104. }
  105. public function __unset($name) {
  106. unset($this->subNodes[$name]);
  107. }
  108. public function getIterator() {
  109. return new \ArrayIterator($this->subNodes);
  110. }
  111. }