diff --git a/CHANGELOG.md b/CHANGELOG.md index a39fab744..def18a3b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ All Notable changes to `League\Uri` will be documented in this file - `Uri::toRfc8089` return the URI in a RFC8089 formator `null` - `Uri::toAnchor` returns the HTML anchor string using the instance as the href attribute value - `Uri::toMarkdown` returns the markdown link construct using the instance as the href attribute value +- `Uri::getUser` to be inline with PHP native URI interface +- `Uri::withUser` to be inline with PHP native URI interface +- `Uri::withPassword` to be inline with PHP native URI interface ### Fixed diff --git a/Uri.php b/Uri.php index aec67591d..324ec07f7 100644 --- a/Uri.php +++ b/Uri.php @@ -1303,6 +1303,20 @@ public function withUserInfo( }; } + public function withUser(?string $user): UriInterface + { + return $this->withUserInfo($user, $this->pass); + } + + public function withPassword(?string $password): UriInterface + { + if (null === $this->user) { + throw new SyntaxError('The password component can not be if the URI user component is not set.'); + } + + return $this->withUserInfo($this->user, $password); + } + public function withHost(Stringable|string|null $host): UriInterface { $host = $this->formatHost($this->filterString($host)); diff --git a/UriTest.php b/UriTest.php index 587eca4d5..fab6cc872 100644 --- a/UriTest.php +++ b/UriTest.php @@ -1072,4 +1072,30 @@ public static function providesUriToHTML(): iterable 'expected' => 'http://bébé.be', ]; } + + #[Test] + public function it_can_update_the_user_component(): void + { + self::assertSame('user', Uri::new('example://host/path?query')->withUser('user')->getUser()); + self::assertNull(Uri::new('example://user@host/path?query')->withUser(null)->getUser()); + } + + #[Test] + public function it_can_update_the_password_component(): void + { + self::assertNull(Uri::new('example://user:pass@host/path?query')->withPassword(null)->getPassword()); + + self::assertSame( + 'example://user:pass@host/path?query', + Uri::new('example://user@host/path?query')->withPassword('pass')->toString() + ); + } + + #[Test] + public function it_requires_a_user_component_to_update_the_password_component(): void + { + $this->expectException(SyntaxError::class); + + Uri::new('example://host/path?query')->withPassword('pass'); + } }