@@ -40,6 +40,10 @@ final public function __construct(iterable|string|Identifier $name)
4040 */
4141 private function parseName (iterable |string |Identifier $ name ): array
4242 {
43+ if (\is_string ($ name )) {
44+ $ name = \array_filter (\explode ('\\' , $ name ));
45+ }
46+
4347 if (\is_iterable ($ name )) {
4448 $ result = [];
4549
@@ -110,6 +114,70 @@ public function slice(int $offset = 0, int $length = null): self
110114 return new static (\array_slice ($ this ->parts , $ offset , $ length ));
111115 }
112116
117+ /**
118+ * Appends the passed {@see Name} to the existing one at the end.
119+ *
120+ * ```php
121+ * $name = new Name('Some\Any');
122+ *
123+ * echo $name->withAdded(new Name('Test\Class'));
124+ * > "Some\Any\Test\Class"
125+ *
126+ * echo $name->withAdded(new Name('Any\Class'));
127+ * > "Some\Any\Any\Class"
128+ * ```
129+ */
130+ public function withAdded (self $ name ): self
131+ {
132+ return new static ([
133+ ...$ this ->parts ,
134+ ...$ name ->parts ,
135+ ]);
136+ }
137+
138+ /**
139+ * Combines two names into one (in case the last one is an alias).
140+ *
141+ * ```php
142+ * $name = new Name('Some\Any');
143+ *
144+ * echo $name->mergeWith(new Name('Test\Class'));
145+ * > "Some\Any\Class"
146+ *
147+ * echo $name->mergeWith(new Name('Any\Class'));
148+ * > "Some\Any\Class"
149+ * ```
150+ *
151+ * Real world use case:
152+ * ```php
153+ * // use TypeLang\Parser\Node;
154+ * // echo Node::class;
155+ *
156+ * $name = new Name('TypeLang\Parser\Node');
157+ * echo $name->mergeWith(new Name('Node'));
158+ *
159+ * // > TypeLang\Parser\Node
160+ * ```
161+ *
162+ * Or aliased:
163+ * ```php
164+ * // use TypeLang\Parser\Exception as Error;
165+ * // echo Error\SemanticException::class;
166+ *
167+ * $name = new Name('TypeLang\Parser\Exception');
168+ * echo $name->mergeWith(new Name('Error\SemanticException'));
169+ *
170+ * // > TypeLang\Parser\Exception\SemanticException
171+ * ```
172+ */
173+ public function mergeWith (self $ name ): self
174+ {
175+ return new static ([
176+ ...$ this ->parts ,
177+ ...\array_slice ($ name ->parts , 1 ),
178+ ]);
179+ }
180+
113181 /**
114182 * Convert name to full qualified name instance.
115183 */
0 commit comments