-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding IPv6 Converter and usage in uri-interfaces and uri-components
- Loading branch information
Showing
6 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Uri\IPv6; | ||
|
||
use Stringable; | ||
use const FILTER_FLAG_IPV6; | ||
use const FILTER_VALIDATE_IP; | ||
|
||
use function filter_var; | ||
use function inet_pton; | ||
use function implode; | ||
use function str_split; | ||
use function strtolower; | ||
use function unpack; | ||
|
||
final class Converter | ||
{ | ||
public static function compress(Stringable|string|null $ipv6): ?string | ||
{ | ||
return match (true) { | ||
null === $ipv6 => null, | ||
self::isValidIpv6($ipv6) => (string) inet_ntop((string) inet_pton((string) $ipv6)), | ||
default => (string) $ipv6, | ||
}; | ||
} | ||
|
||
public static function expand(Stringable|string|null $ipv6): ?string | ||
{ | ||
if (null === $ipv6) { | ||
return null; | ||
} | ||
|
||
$ipv6 = (string) $ipv6; | ||
if (!self::isValidIpv6($ipv6)) { | ||
return $ipv6; | ||
} | ||
|
||
$hex = (array) unpack("H*hex", (string) inet_pton($ipv6)); | ||
|
||
return implode(':', str_split(strtolower($hex['hex'] ?? ''), 4)); | ||
} | ||
|
||
private static function isValidIpv6(Stringable|string|null $ipv6): bool | ||
{ | ||
return (bool) filter_var((string) $ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); | ||
} | ||
} |