Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,27 @@ public static function index(array $props = []): static
return new static($url, $props);
}

/**
* Inherit query, params and fragment from a parent Uri
* @since 5.2.0
* @return $this
*/
public function inherit(Uri|string $parent): static
{
if (is_string($parent) === true) {
$parent = new static($parent);
}

$this->query->merge($parent->query());
$this->params->merge($parent->params());

if ($fragment = $parent->fragment()) {
$this->setFragment($fragment);
}

return $this;
}

/**
* Checks if the host exists
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/Http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ public function testIndexInCli(): void
$this->assertSame('/', $uri->toString());
}

public function testInherit(): void
{
$base = new Uri('https://getkirby.com');

$uri = $base->inherit('https://foo.com/this/is/a/path');
$this->assertSame('https://getkirby.com', $uri->toString());

$uri = $base->inherit('https://foo.com/fox:bax?foo=bar#anchor');
$this->assertSame('https://getkirby.com/fox:bax?foo=bar#anchor', $uri->toString());

$base = new Uri('https://getkirby.com?one=two');
$uri = $base->inherit('https://foo.com/?foo=bar');
$this->assertSame('https://getkirby.com?one=two&foo=bar', $uri->toString());
}

public function testInvalidScheme(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down