diff --git a/docs/uri/7.0/rfc3986.md b/docs/uri/7.0/rfc3986.md index 1064b1bd..feda815f 100644 --- a/docs/uri/7.0/rfc3986.md +++ b/docs/uri/7.0/rfc3986.md @@ -187,7 +187,7 @@ Let's examine the result of building a URI: ~~~php $uri = Uri::new("http://foo:bar@www.example.com:81/how/are/you?foo=baz#title"); echo $uri->getScheme(); //displays "http" -echo $uri->getUsername(); //displays "foo" +echo $uri->getUser(); //displays "foo" echo $uri->getPassword(); //displays "bar" echo $uri->getUserInfo(); //displays "foo:bar" echo $uri->getHost(); //displays "www.example.com" @@ -353,6 +353,9 @@ $uri = Uri::new("ftp://thephpleague.com/fr/") echo $uri; //displays yolo://foo:bar@www.example.com:81?foo=baz#fine ~~~ +

The withUser and withPassword methods are available +since version 7.6.0 to be inline with PHP native Uri interface.

+

The when method is available since version 7.6.0

To ease building the instance, the `when` method is added to conditionally create your component. diff --git a/interfaces/Contracts/UriInterface.php b/interfaces/Contracts/UriInterface.php index 688c30f2..10274bc9 100644 --- a/interfaces/Contracts/UriInterface.php +++ b/interfaces/Contracts/UriInterface.php @@ -24,7 +24,9 @@ * * @method string|null getUsername() returns the user component of the URI (deprecated). * @method string|null getUser() returns the user component of the URI. + * @method self withUser(?string $user) returns a new URI instance with user component updated, if the user is set to null the password also will be set to null. * @method string|null getPassword() returns the scheme-specific information about how to gain authorization to access the resource. + * @method self withPassword(?string $password) returns a new URI instance with password component updated, if the user is set to null the password also will be set to null. * @method string toNormalizedString() returns the normalized string representation of the URI * @method array toComponents() returns an associative array containing all the URI components. * @method self normalize() returns a new URI instance with normalized components diff --git a/uri/CHANGELOG.md b/uri/CHANGELOG.md index a39fab74..def18a3b 100644 --- a/uri/CHANGELOG.md +++ b/uri/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/Uri.php b/uri/Uri.php index aec67591..324ec07f 100644 --- a/uri/Uri.php +++ b/uri/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/uri/UriTest.php b/uri/UriTest.php index 587eca4d..fab6cc87 100644 --- a/uri/UriTest.php +++ b/uri/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'); + } }