Skip to content

Commit 7368a2a

Browse files
committed
adds method to add path segments to URI
1 parent 3736570 commit 7368a2a

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/Uri.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ function withPath ($path) : static {
207207
return $instance;
208208
}
209209

210+
function withPathAdded (string $addedPath) : static {
211+
$instance = clone $this;
212+
$addedPath = Parse::parsePath($addedPath);
213+
if ($instance->path !== null) {
214+
$instance->path = $instance->path->concat($addedPath);
215+
} else {
216+
$instance->path = $addedPath;
217+
}
218+
219+
return $instance;
220+
}
221+
210222
/**
211223
* @throws InvalidArgumentException
212224
* @param string $scheme

tests/UriTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,20 @@ function provideSetterMethodsWithInvalidTypes () : \Generator {
193193
yield 'fragment' => ['withFragment'];
194194
}
195195

196+
function testExpectsUriWithAddedPath () {
197+
$uri = new Uri('https://www.example.com/foo');
198+
$uri = $uri->withPathAdded('/bar');
199+
200+
$this->assertSame('https://www.example.com/foo/bar', $uri->compose());
201+
$this->assertSame('/foo/bar', $uri->getPath());
202+
}
203+
204+
function testExpectsUriWithAddedPathEvenIfPathWasEmptyBefore () {
205+
$uri = new Uri('https://www.example.com');
206+
$uri = $uri->withPathAdded('/foo/bar');
207+
208+
$this->assertSame('https://www.example.com/foo/bar', $uri->compose());
209+
$this->assertSame('/foo/bar', $uri->getPath());
210+
}
211+
196212
}

0 commit comments

Comments
 (0)