Name.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @property array $parts Parts of the name
  4. */
  5. class PHPParser_Node_Name extends PHPParser_NodeAbstract
  6. {
  7. /**
  8. * Constructs a name node.
  9. *
  10. * @param string|array $parts Parts of the name (or name as string)
  11. * @param array $attributes Additional attributes
  12. */
  13. public function __construct($parts, array $attributes = array()) {
  14. if (!is_array($parts)) {
  15. $parts = explode('\\', $parts);
  16. }
  17. parent::__construct(
  18. array(
  19. 'parts' => $parts,
  20. ),
  21. $attributes
  22. );
  23. }
  24. /**
  25. * Gets the first part of the name, i.e. everything before the first namespace separator.
  26. *
  27. * @return string First part of the name
  28. */
  29. public function getFirst() {
  30. return $this->parts[0];
  31. }
  32. /**
  33. * Gets the last part of the name, i.e. everything after the last namespace separator.
  34. *
  35. * @return string Last part of the name
  36. */
  37. public function getLast() {
  38. return $this->parts[count($this->parts) - 1];
  39. }
  40. /**
  41. * Checks whether the name is unqualified. (E.g. Name)
  42. *
  43. * @return bool Whether the name is unqualified
  44. */
  45. public function isUnqualified() {
  46. return 1 == count($this->parts);
  47. }
  48. /**
  49. * Checks whether the name is qualified. (E.g. Name\Name)
  50. *
  51. * @return bool Whether the name is qualified
  52. */
  53. public function isQualified() {
  54. return 1 < count($this->parts);
  55. }
  56. /**
  57. * Checks whether the name is fully qualified. (E.g. \Name)
  58. *
  59. * @return bool Whether the name is fully qualified
  60. */
  61. public function isFullyQualified() {
  62. return false;
  63. }
  64. /**
  65. * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
  66. *
  67. * @return bool Whether the name is relative
  68. */
  69. public function isRelative() {
  70. return false;
  71. }
  72. /**
  73. * Returns a string representation of the name by imploding the namespace parts with a separator.
  74. *
  75. * @param string $separator The separator to use (defaults to the namespace separator \)
  76. *
  77. * @return string String representation
  78. */
  79. public function toString($separator = '\\') {
  80. return implode($separator, $this->parts);
  81. }
  82. /**
  83. * Returns a string representation of the name by imploding the namespace parts with the
  84. * namespace separator.
  85. *
  86. * @return string String representation
  87. */
  88. public function __toString() {
  89. return implode('\\', $this->parts);
  90. }
  91. /**
  92. * Sets the whole name.
  93. *
  94. * @param string|array|self $name The name to set the whole name to
  95. */
  96. public function set($name) {
  97. $this->parts = $this->prepareName($name);
  98. }
  99. /**
  100. * Prepends a name to this name.
  101. *
  102. * @param string|array|self $name Name to prepend
  103. */
  104. public function prepend($name) {
  105. $this->parts = array_merge($this->prepareName($name), $this->parts);
  106. }
  107. /**
  108. * Appends a name to this name.
  109. *
  110. * @param string|array|self $name Name to append
  111. */
  112. public function append($name) {
  113. $this->parts = array_merge($this->parts, $this->prepareName($name));
  114. }
  115. /**
  116. * Sets the first part of the name.
  117. *
  118. * @param string|array|self $name The name to set the first part to
  119. */
  120. public function setFirst($name) {
  121. array_splice($this->parts, 0, 1, $this->prepareName($name));
  122. }
  123. /**
  124. * Sets the last part of the name.
  125. *
  126. * @param string|array|self $name The name to set the last part to
  127. */
  128. public function setLast($name) {
  129. array_splice($this->parts, -1, 1, $this->prepareName($name));
  130. }
  131. /**
  132. * Prepares a (string, array or Name node) name for use in name changing methods by converting
  133. * it to an array.
  134. *
  135. * @param string|array|self $name Name to prepare
  136. *
  137. * @return array Prepared name
  138. */
  139. protected function prepareName($name) {
  140. if (is_string($name)) {
  141. return explode('\\', $name);
  142. } elseif (is_array($name)) {
  143. return $name;
  144. } elseif ($name instanceof self) {
  145. return $name->parts;
  146. }
  147. throw new InvalidArgumentException(
  148. 'When changing a name you need to pass either a string, an array or a Name node'
  149. );
  150. }
  151. }