Skip to content

Commit 58f734f

Browse files
committed
Improve implementation
1 parent c8c4c96 commit 58f734f

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

interfaces/Encoder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function rawurldecode;
3131
use function rawurlencode;
3232
use function sprintf;
33+
use function str_starts_with;
3334
use function strtolower;
3435
use function strtoupper;
3536

@@ -300,8 +301,8 @@ public static function normalizeHost(Stringable|string|null $host): ?string
300301
return $host;
301302
}
302303

303-
if (($newHost = IPv6Converter::normalize($host)) !== $host) {
304-
return $newHost;
304+
if (str_starts_with($host, '[')) {
305+
return IPv6Converter::normalize($host);
305306
}
306307

307308
$host = strtolower($host);

interfaces/IPv6/Converter.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ public static function build(array $components): string
100100
}
101101

102102
/**
103-
* @param Stringable|string|null $host
104-
*
105103
* @return array{ipAddress:string|null, zoneIdentifier:string|null}
106104
*/
107105
private static function parse(Stringable|string|null $host): array
@@ -145,9 +143,14 @@ public static function isIpv6(Stringable|string|null $host): bool
145143

146144
public static function normalize(Stringable|string|null $host): ?string
147145
{
146+
if (null === $host || '' === $host) {
147+
return $host;
148+
}
149+
150+
$host = (string) $host;
148151
$components = self::parse($host);
149152
if (null === $components['ipAddress']) {
150-
return $host;
153+
return strtolower($host);
151154
}
152155

153156
$components['ipAddress'] = strtolower($components['ipAddress']);

interfaces/UriString.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ public static function parseNormalized(Stringable|string $uri): array
298298

299299
$host = $components['host'];
300300
if (null !== $host && false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
301-
$host = IPv6Converter::normalize($host);
301+
$host = (string) IPv6Converter::normalize($host);
302302
if ($host === $components['host']) {
303303
$host = (string) preg_replace_callback(
304304
'/%[0-9A-F]{2}/i',
305305
fn (array $matches): string => strtoupper($matches[0]),
306-
strtolower($components['host'])
306+
strtolower($host)
307307
);
308308
if ($isSupported) {
309309
$idnaHost = IdnaConverter::toAscii(rawurldecode($components['host']));

0 commit comments

Comments
 (0)