Skip to content

Commit

Permalink
Improve displayString implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 18, 2024
1 parent 3499890 commit ad3c6d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 4 additions & 1 deletion components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ All Notable changes to `League\Uri\Components` will be documented in this file

- `Component::when` conditional method to ease component building logic.
- `URLSearchParams::when` conditional method to ease component building logic.
- `Modifier::prependQueryParameters` returns a modifier with prepend query paramters
- `Modifier::when` conditional method to ease component building logic.
- `Modifier::with*` method from the underlying `Uri` object are proxy to improve DX.
- `Modifier::displayUriString` shows the URI in a human-readable format which may be an invalid URI.
- `Query::decoded` the string representation of the component decoded.
- `URLSearchParams::decoded` the string representation of the component decoded.

### Fixed

- None

### Deprecated

- None
- `Modifier::getIdnUriString` use `Modifier::displayUriString` instead

### Removed

Expand Down
5 changes: 5 additions & 0 deletions components/Components/URLSearchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ public function toString(): string
return $this->value() ?? '';
}

public function decoded(): string
{
return (string) Query::fromPairs($this->pairs)->decoded();
}

public function __toString(): string
{
return $this->toString();
Expand Down
15 changes: 13 additions & 2 deletions components/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,23 @@ public function displayUriString(): string
{
$userInfo = UserInfo::fromUri($this->uri);

$host = $this->uri->getHost();
if (null !== $host) {
$hostIp = self::ipv4Converter()->toDecimal($host);
$host = IdnConverter::toUnicode((string) Converter::compress(match (true) {
'' === $host,
null === $hostIp,
$host === $hostIp => $host,
default => $hostIp,
}))->domain();
}

return UriString::build([
'scheme' => $this->uri->getScheme(),
'host' => Host::fromUri($this->hostToDecimal()->hostToIpv6Compressed())->toUnicode(),
'port' => $this->uri->getPort(),
'user' => $userInfo->getUser(),
'pass' => $userInfo->getPass(),
'host' => $host,
'port' => $this->uri->getPort(),
'path' => Path::fromUri($this->uri)->withoutDotSegments()->decoded(),
'query' => Query::fromUri($this->uri)->decoded(),
'fragment' => Fragment::fromUri($this->uri)->decoded(),
Expand Down
2 changes: 1 addition & 1 deletion components/ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ public static function providesUriToDisplay(): iterable

yield 'host IPv6' => [
'input' => 'https://[fe80:0000:0000:0000:0000:0000:0000:000a%25en1]/foo/bar',
'output' => 'https://[fe80::a%25en1]/foo/bar',
'output' => 'https://[fe80::a%en1]/foo/bar',
];

yield 'IPv6 gets expanded if needed' => [
Expand Down

0 comments on commit ad3c6d1

Please sign in to comment.