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 0be0822 commit df3c568
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 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.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 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 df3c568

Please sign in to comment.