Skip to content

Commit

Permalink
Adding missing PHP Uri interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 25, 2024
1 parent 5bf877f commit 7f34bb9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/uri/7.0/rfc3986.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Let's examine the result of building a URI:
~~~php
$uri = Uri::new("http://foo:[email protected]: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"
Expand Down Expand Up @@ -353,6 +353,9 @@ $uri = Uri::new("ftp://thephpleague.com/fr/")
echo $uri; //displays yolo://foo:[email protected]:81?foo=baz#fine
~~~

<p class="message-notice">The <code>withUser</code> and <code>withPassword</code> methods are available
since version <code>7.6.0</code> to be inline with PHP native <code>Uri</code> interface.</p>

<p class="message-notice">The <code>when</code> method is available since version <code>7.6.0</code></p>
To ease building the instance, the `when` method is added to conditionally create your component.

Expand Down
2 changes: 2 additions & 0 deletions interfaces/Contracts/UriInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions uri/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
26 changes: 26 additions & 0 deletions uri/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1072,4 +1072,30 @@ public static function providesUriToHTML(): iterable
'expected' => '<a href="http://xn--bb-bjab.be" class="foo bar" target="_blank">http://bébé.be</a>',
];
}

#[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');
}
}

0 comments on commit 7f34bb9

Please sign in to comment.