Name.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace PhpParser\Node;
  3. use PhpParser\NodeAbstract;
  4. class Name extends NodeAbstract
  5. {
  6. /** @var string[] Parts of the name */
  7. public $parts;
  8. /**
  9. * Constructs a name node.
  10. *
  11. * @param string|array $parts Parts of the name (or name as string)
  12. * @param array $attributes Additional attributes
  13. */
  14. public function __construct($parts, array $attributes = array()) {
  15. if (!is_array($parts)) {
  16. $parts = explode('\\', $parts);
  17. }
  18. parent::__construct($attributes);
  19. $this->parts = $parts;
  20. }
  21. public function getSubNodeNames() {
  22. return array('parts');
  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. * @deprecated Create a new Name instead, or manually modify the $parts property
  95. *
  96. * @param string|array|self $name The name to set the whole name to
  97. */
  98. public function set($name) {
  99. $this->parts = self::prepareName($name);
  100. }
  101. /**
  102. * Prepends a name to this name.
  103. *
  104. * @deprecated Use Name::concat($name1, $name2) instead
  105. *
  106. * @param string|array|self $name Name to prepend
  107. */
  108. public function prepend($name) {
  109. $this->parts = array_merge(self::prepareName($name), $this->parts);
  110. }
  111. /**
  112. * Appends a name to this name.
  113. *
  114. * @deprecated Use Name::concat($name1, $name2) instead
  115. *
  116. * @param string|array|self $name Name to append
  117. */
  118. public function append($name) {
  119. $this->parts = array_merge($this->parts, self::prepareName($name));
  120. }
  121. /**
  122. * Sets the first part of the name.
  123. *
  124. * @deprecated Use concat($first, $name->slice(1)) instead
  125. *
  126. * @param string|array|self $name The name to set the first part to
  127. */
  128. public function setFirst($name) {
  129. array_splice($this->parts, 0, 1, self::prepareName($name));
  130. }
  131. /**
  132. * Sets the last part of the name.
  133. *
  134. * @param string|array|self $name The name to set the last part to
  135. */
  136. public function setLast($name) {
  137. array_splice($this->parts, -1, 1, self::prepareName($name));
  138. }
  139. /**
  140. * Gets a slice of a name (similar to array_slice).
  141. *
  142. * This method returns a new instance of the same type as the original and with the same
  143. * attributes.
  144. *
  145. * If the slice is empty, a Name with an empty parts array is returned. While this is
  146. * meaningless in itself, it works correctly in conjunction with concat().
  147. *
  148. * @param int $offset Offset to start the slice at
  149. *
  150. * @return static Sliced name
  151. */
  152. public function slice($offset) {
  153. // TODO negative offset and length
  154. if ($offset < 0 || $offset > count($this->parts)) {
  155. throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
  156. }
  157. return new static(array_slice($this->parts, $offset), $this->attributes);
  158. }
  159. /**
  160. * Concatenate two names, yielding a new Name instance.
  161. *
  162. * The type of the generated instance depends on which class this method is called on, for
  163. * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
  164. *
  165. * @param string|array|self $name1 The first name
  166. * @param string|array|self $name2 The second name
  167. * @param array $attributes Attributes to assign to concatenated name
  168. *
  169. * @return static Concatenated name
  170. */
  171. public static function concat($name1, $name2, array $attributes = []) {
  172. return new static(
  173. array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
  174. );
  175. }
  176. /**
  177. * Prepares a (string, array or Name node) name for use in name changing methods by converting
  178. * it to an array.
  179. *
  180. * @param string|array|self $name Name to prepare
  181. *
  182. * @return array Prepared name
  183. */
  184. private static function prepareName($name) {
  185. if (is_string($name)) {
  186. return explode('\\', $name);
  187. } elseif (is_array($name)) {
  188. return $name;
  189. } elseif ($name instanceof self) {
  190. return $name->parts;
  191. }
  192. throw new \InvalidArgumentException(
  193. 'When changing a name you need to pass either a string, an array or a Name node'
  194. );
  195. }
  196. }