No Description

Name.php 4.4KB

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