-
-
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
11 changed files
with
222 additions
and
8 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
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,41 @@ | ||
--- | ||
layout: default | ||
title: IPv6 Converter | ||
--- | ||
|
||
IPv6 Converter | ||
======= | ||
|
||
The `League\Uri\IPv6\Converter` is a IPv6 Converter. | ||
|
||
```php | ||
<?php | ||
|
||
use League\Uri\IPv6\Converter; | ||
|
||
echo Converter::expand('[::1]'); | ||
// returns '[0000:0000:0000:0000:0000:0000:0000:0001]' | ||
echo Converter::compress('[1050:0000:0000:0000:0005:0000:300c:326b]'); | ||
// returns [1050::5:0:300c:326b] | ||
``` | ||
|
||
Usage | ||
-------- | ||
|
||
The `Converter::compress` method convert an expanded IPv6 host into its compressed form. | ||
The method only parameter should represent a host value. The `Converter::exoend` method | ||
does the opposite. | ||
|
||
If you submit a host which is not an IPv6 one then, the submitted host value will be returned | ||
as is.Conversely, trying to expand an IPv6 host which is already expanded or trying to compress | ||
an already compressed IPv6 host will return the same value so nothing will be gain performing | ||
such obvious operation. | ||
|
||
```php | ||
<?php | ||
|
||
echo Converter::compress('[::1]'); | ||
// returns '[::1]' | ||
echo Converter::expand('[1050:0000:0000:0000:0005:0000:300c:326b]'); | ||
// returns [1050:0000:0000:0000:0005:0000:300c:326b] | ||
``` |
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,94 @@ | ||
<?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 $host): ?string | ||
{ | ||
$components = self::parse($host); | ||
if (null === $components['ipv6']) { | ||
return match ($host) { | ||
null => $host, | ||
default => (string) $host, | ||
}; | ||
} | ||
|
||
[$ipv6, $zoneIdentifier] = $components; | ||
$ipv6 = (string) inet_ntop((string) inet_pton($ipv6)); | ||
|
||
return self::build($ipv6, $zoneIdentifier); | ||
} | ||
|
||
public static function expand(Stringable|string|null $host): ?string | ||
{ | ||
$components = self::parse($host); | ||
if (null === $components['ipv6']) { | ||
return match ($host) { | ||
null => $host, | ||
default => (string) $host, | ||
}; | ||
} | ||
|
||
[$ipv6, $zoneIdentifier] = $components; | ||
$hex = (array) unpack("H*hex", (string) inet_pton($ipv6)); | ||
$ipv6 = implode(':', str_split(strtolower($hex['hex'] ?? ''), 4)); | ||
|
||
return self::build($ipv6, $zoneIdentifier); | ||
} | ||
|
||
private static function build(string $ip, ?string $zoneIdentifier): string | ||
{ | ||
$zoneIdentifier = match ($zoneIdentifier) { | ||
null => '', | ||
default => '%'.$zoneIdentifier, | ||
}; | ||
|
||
return '['.$ip.$zoneIdentifier.']'; | ||
} | ||
|
||
/**] | ||
* @param Stringable|string|null $host | ||
* | ||
* @return array{ipv6:?string, zoneIdentifier:?string} | ||
*/ | ||
private static function parse(Stringable|string|null $host): array | ||
{ | ||
if ($host === null) { | ||
return ['ipv6' => null, 'zoneIdentifier' => null]; | ||
} | ||
|
||
$host = (string) $host; | ||
if ($host === '') { | ||
return ['ipv6' => null, 'zoneIdentifier' => null]; | ||
} | ||
|
||
if (!str_starts_with($host, '[')) { | ||
return ['ipv6' => null, 'zoneIdentifier' => null]; | ||
} | ||
|
||
if (!str_ends_with($host, ']')) { | ||
return ['ipv6' => null, 'zoneIdentifier' => null]; | ||
} | ||
|
||
[$ipv6, $zoneIdentifier] = explode('%', substr($host, 1, -1), 2) + [1 => null]; | ||
if (false === filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | ||
return ['ipv6' => null, 'zoneIdentifier' => null]; | ||
} | ||
|
||
return ['ipv6' => $ipv6, 'zoneIdentifier' => $zoneIdentifier]; | ||
} | ||
} |