Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser\Node;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\NodeAbstract;
|
Chris@0
|
6
|
Chris@0
|
7 class Name extends NodeAbstract
|
Chris@0
|
8 {
|
Chris@0
|
9 /**
|
Chris@0
|
10 * @var string[] Parts of the name
|
Chris@0
|
11 */
|
Chris@0
|
12 public $parts;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Constructs a name node.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @param string|array|self $name Name as string, part array or Name instance (copy ctor)
|
Chris@0
|
18 * @param array $attributes Additional attributes
|
Chris@0
|
19 */
|
Chris@0
|
20 public function __construct($name, array $attributes = array()) {
|
Chris@0
|
21 parent::__construct($attributes);
|
Chris@0
|
22 $this->parts = self::prepareName($name);
|
Chris@0
|
23 }
|
Chris@0
|
24
|
Chris@0
|
25 public function getSubNodeNames() {
|
Chris@0
|
26 return array('parts');
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Gets the first part of the name, i.e. everything before the first namespace separator.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @return string First part of the name
|
Chris@0
|
33 */
|
Chris@0
|
34 public function getFirst() {
|
Chris@0
|
35 return $this->parts[0];
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Gets the last part of the name, i.e. everything after the last namespace separator.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return string Last part of the name
|
Chris@0
|
42 */
|
Chris@0
|
43 public function getLast() {
|
Chris@0
|
44 return $this->parts[count($this->parts) - 1];
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Checks whether the name is unqualified. (E.g. Name)
|
Chris@0
|
49 *
|
Chris@0
|
50 * @return bool Whether the name is unqualified
|
Chris@0
|
51 */
|
Chris@0
|
52 public function isUnqualified() {
|
Chris@0
|
53 return 1 == count($this->parts);
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Checks whether the name is qualified. (E.g. Name\Name)
|
Chris@0
|
58 *
|
Chris@0
|
59 * @return bool Whether the name is qualified
|
Chris@0
|
60 */
|
Chris@0
|
61 public function isQualified() {
|
Chris@0
|
62 return 1 < count($this->parts);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Checks whether the name is fully qualified. (E.g. \Name)
|
Chris@0
|
67 *
|
Chris@0
|
68 * @return bool Whether the name is fully qualified
|
Chris@0
|
69 */
|
Chris@0
|
70 public function isFullyQualified() {
|
Chris@0
|
71 return false;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
|
Chris@0
|
76 *
|
Chris@0
|
77 * @return bool Whether the name is relative
|
Chris@0
|
78 */
|
Chris@0
|
79 public function isRelative() {
|
Chris@0
|
80 return false;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Returns a string representation of the name by imploding the namespace parts with the
|
Chris@0
|
85 * namespace separator.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return string String representation
|
Chris@0
|
88 */
|
Chris@0
|
89 public function toString() {
|
Chris@0
|
90 return implode('\\', $this->parts);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Returns a string representation of the name by imploding the namespace parts with the
|
Chris@0
|
95 * namespace separator.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return string String representation
|
Chris@0
|
98 */
|
Chris@0
|
99 public function __toString() {
|
Chris@0
|
100 return implode('\\', $this->parts);
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Gets a slice of a name (similar to array_slice).
|
Chris@0
|
105 *
|
Chris@0
|
106 * This method returns a new instance of the same type as the original and with the same
|
Chris@0
|
107 * attributes.
|
Chris@0
|
108 *
|
Chris@0
|
109 * If the slice is empty, null is returned. The null value will be correctly handled in
|
Chris@0
|
110 * concatenations using concat().
|
Chris@0
|
111 *
|
Chris@0
|
112 * Offset and length have the same meaning as in array_slice().
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param int $offset Offset to start the slice at (may be negative)
|
Chris@0
|
115 * @param int|null $length Length of the slice (may be negative)
|
Chris@0
|
116 *
|
Chris@0
|
117 * @return static|null Sliced name
|
Chris@0
|
118 */
|
Chris@0
|
119 public function slice($offset, $length = null) {
|
Chris@0
|
120 $numParts = count($this->parts);
|
Chris@0
|
121
|
Chris@0
|
122 $realOffset = $offset < 0 ? $offset + $numParts : $offset;
|
Chris@0
|
123 if ($realOffset < 0 || $realOffset > $numParts) {
|
Chris@0
|
124 throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 if (null === $length) {
|
Chris@0
|
128 $realLength = $numParts - $realOffset;
|
Chris@0
|
129 } else {
|
Chris@0
|
130 $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
|
Chris@0
|
131 if ($realLength < 0 || $realLength > $numParts) {
|
Chris@0
|
132 throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
|
Chris@0
|
133 }
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 if ($realLength === 0) {
|
Chris@0
|
137 // Empty slice is represented as null
|
Chris@0
|
138 return null;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * Concatenate two names, yielding a new Name instance.
|
Chris@0
|
146 *
|
Chris@0
|
147 * The type of the generated instance depends on which class this method is called on, for
|
Chris@0
|
148 * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
|
Chris@0
|
149 *
|
Chris@0
|
150 * If one of the arguments is null, a new instance of the other name will be returned. If both
|
Chris@0
|
151 * arguments are null, null will be returned. As such, writing
|
Chris@0
|
152 * Name::concat($namespace, $shortName)
|
Chris@0
|
153 * where $namespace is a Name node or null will work as expected.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @param string|array|self|null $name1 The first name
|
Chris@0
|
156 * @param string|array|self|null $name2 The second name
|
Chris@0
|
157 * @param array $attributes Attributes to assign to concatenated name
|
Chris@0
|
158 *
|
Chris@0
|
159 * @return static|null Concatenated name
|
Chris@0
|
160 */
|
Chris@0
|
161 public static function concat($name1, $name2, array $attributes = []) {
|
Chris@0
|
162 if (null === $name1 && null === $name2) {
|
Chris@0
|
163 return null;
|
Chris@0
|
164 } elseif (null === $name1) {
|
Chris@0
|
165 return new static(self::prepareName($name2), $attributes);
|
Chris@0
|
166 } else if (null === $name2) {
|
Chris@0
|
167 return new static(self::prepareName($name1), $attributes);
|
Chris@0
|
168 } else {
|
Chris@0
|
169 return new static(
|
Chris@0
|
170 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
|
Chris@0
|
171 );
|
Chris@0
|
172 }
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Prepares a (string, array or Name node) name for use in name changing methods by converting
|
Chris@0
|
177 * it to an array.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @param string|array|self $name Name to prepare
|
Chris@0
|
180 *
|
Chris@0
|
181 * @return array Prepared name
|
Chris@0
|
182 */
|
Chris@0
|
183 private static function prepareName($name) {
|
Chris@0
|
184 if (\is_string($name)) {
|
Chris@0
|
185 return explode('\\', $name);
|
Chris@0
|
186 } elseif (\is_array($name)) {
|
Chris@0
|
187 return $name;
|
Chris@0
|
188 } elseif ($name instanceof self) {
|
Chris@0
|
189 return $name->parts;
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 throw new \InvalidArgumentException(
|
Chris@0
|
193 'Expected string, array of parts or Name instance'
|
Chris@0
|
194 );
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|