Skip to content

Commit

Permalink
Adding Modifier::whatWgHost
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 30, 2024
1 parent 564245f commit 96a810c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All Notable changes to `League\Uri\Components` will be documented in this file
- `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::whatWgHost` returns the host as normalized by the WHATWG algorithm
- `Modifier::with*` method from the underlying `Uri` object are proxy to improve DX.
- `Query::decoded` the string representation of the component decoded.
- `URLSearchParams::decoded` the string representation of the component decoded.
Expand Down
26 changes: 26 additions & 0 deletions components/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use League\Uri\Contracts\PathInterface;
use League\Uri\Contracts\UriAccess;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\MissingFeature;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\Idna\Converter as IdnaConverter;
use League\Uri\IPv4\Converter as IPv4Converter;
Expand All @@ -36,6 +37,7 @@
use Stringable;

use function array_map;
use function filter_var;
use function get_object_vars;
use function is_bool;
use function ltrim;
Expand All @@ -44,6 +46,9 @@
use function str_ends_with;
use function str_starts_with;

use const FILTER_FLAG_IPV4;
use const FILTER_VALIDATE_IP;

/**
* @method static withScheme(Stringable|string|null $scheme) returns a new instance with the specified scheme.
* @method static withUserInfo(Stringable|string|null $user, Stringable|string|null $password = null) returns a new instance with the specified user info.
Expand Down Expand Up @@ -597,6 +602,27 @@ public function replaceLabel(int $offset, Stringable|string|null $label): static
));
}

public function whatWgHost(): static
{
$host = $this->uri->getHost();
try {
$converted = IPv4Converter::fromEnvironment()->toDecimal($host);
} catch (MissingFeature) {
$converted = null;
}

if (false === filter_var($converted, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$converted = IPv6Converter::compress($host);
}

return match (true) {
null !== $converted => new static($this->uri->withHost($converted)),
'' === $host,
$this->uri instanceof UriInterface => $this,
default => new static($this->uri->withHost((string) Uri::fromComponents(['host' => $host])->getHost())),
};
}

/*********************************
* Path modifier methods
*********************************/
Expand Down
12 changes: 12 additions & 0 deletions components/ModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,4 +883,16 @@ public function it_will_remove_empty_pairs_fix_issue_133(): void
self::assertNull($removeEmptyPairs('https://a.b/c?=d'));
self::assertNull($removeEmptyPairs('https://a.b/c?='));
}

#[Test]
public function it_will_convert_uri_host_following_whatwg_rules(): void
{
self::assertSame(
'192.168.2.13',
Modifier::from(Http::new('https://0:0@0xc0a8020d/0?0#0'))
->whatWgHost()
->getUri()
->getHost()
);
}
}
14 changes: 14 additions & 0 deletions docs/components/7.0/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ echo Modifier::from('http://bébé.be')
<li><a href="#modifierreplacelabel">replaceLabel</a></li>
<li><a href="#modifierremovelabels">removeLabels</a></li>
<li><a href="#modifierslicelabels">sliceLabels</a></li>
<li><a href="#modifierwhatwghost">whatWgHost</a></li>
</ul>
</div>
<div>
Expand Down Expand Up @@ -604,6 +605,19 @@ echo Modifier::from($uri)->sliceLabels(1, 1)->getUriString();

<p class="message-info">This modifier supports negative offset</p>

### Modifier::whatWgHost

Returns the host as formatted following WHATWG host formatting

<p class="message-notice">available since version <code>7.6.0</code></p>

~~~php
$uri = "https://0:0@0:0";
echo Modifier::from($uri)->whatWgHost()->getUriString();
//display "https://0:[email protected]"
~~~

In case of IPv4 and/or IPv6 some extra normalization are applied.

## Path modifiers

Expand Down

0 comments on commit 96a810c

Please sign in to comment.