1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Node; |
4: | |
5: | use PhpParser\NodeAbstract; |
6: | |
7: | class Name extends NodeAbstract |
8: | { |
9: | |
10: | public $parts; |
11: | |
12: | private static $specialClassNames = [ |
13: | 'self' => true, |
14: | 'parent' => true, |
15: | 'static' => true, |
16: | ]; |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | public function __construct($name, array $attributes = []) { |
25: | $this->attributes = $attributes; |
26: | $this->parts = self::prepareName($name); |
27: | } |
28: | |
29: | public function getSubNodeNames() : array { |
30: | return ['parts']; |
31: | } |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | public function getFirst() : string { |
39: | return $this->parts[0]; |
40: | } |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | public function getLast() : string { |
48: | return $this->parts[count($this->parts) - 1]; |
49: | } |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public function isUnqualified() : bool { |
57: | return 1 === count($this->parts); |
58: | } |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | public function isQualified() : bool { |
66: | return 1 < count($this->parts); |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | public function isFullyQualified() : bool { |
75: | return false; |
76: | } |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | public function isRelative() : bool { |
84: | return false; |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | public function toString() : string { |
94: | return implode('\\', $this->parts); |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | public function toCodeString() : string { |
104: | return $this->toString(); |
105: | } |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | public function toLowerString() : string { |
114: | return strtolower(implode('\\', $this->parts)); |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | public function isSpecialClassName() : bool { |
123: | return count($this->parts) === 1 |
124: | && isset(self::$specialClassNames[strtolower($this->parts[0])]); |
125: | } |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | public function __toString() : string { |
134: | return implode('\\', $this->parts); |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | public function slice(int $offset, int $length = null) { |
154: | $numParts = count($this->parts); |
155: | |
156: | $realOffset = $offset < 0 ? $offset + $numParts : $offset; |
157: | if ($realOffset < 0 || $realOffset > $numParts) { |
158: | throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
159: | } |
160: | |
161: | if (null === $length) { |
162: | $realLength = $numParts - $realOffset; |
163: | } else { |
164: | $realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
165: | if ($realLength < 0 || $realLength > $numParts - $realOffset) { |
166: | throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
167: | } |
168: | } |
169: | |
170: | if ($realLength === 0) { |
171: | |
172: | return null; |
173: | } |
174: | |
175: | return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes); |
176: | } |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | public static function concat($name1, $name2, array $attributes = []) { |
196: | if (null === $name1 && null === $name2) { |
197: | return null; |
198: | } elseif (null === $name1) { |
199: | return new static(self::prepareName($name2), $attributes); |
200: | } elseif (null === $name2) { |
201: | return new static(self::prepareName($name1), $attributes); |
202: | } else { |
203: | return new static( |
204: | array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes |
205: | ); |
206: | } |
207: | } |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | private static function prepareName($name) : array { |
218: | if (\is_string($name)) { |
219: | if ('' === $name) { |
220: | throw new \InvalidArgumentException('Name cannot be empty'); |
221: | } |
222: | |
223: | return explode('\\', $name); |
224: | } elseif (\is_array($name)) { |
225: | if (empty($name)) { |
226: | throw new \InvalidArgumentException('Name cannot be empty'); |
227: | } |
228: | |
229: | return $name; |
230: | } elseif ($name instanceof self) { |
231: | return $name->parts; |
232: | } |
233: | |
234: | throw new \InvalidArgumentException( |
235: | 'Expected string, array of parts or Name instance' |
236: | ); |
237: | } |
238: | |
239: | public function getType() : string { |
240: | return 'Name'; |
241: | } |
242: | } |
243: | |