![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/spomky-labs/pki-framework/src/X509/GeneralName/ |
<?php declare(strict_types=1); namespace SpomkyLabs\Pki\X509\GeneralName; use function array_slice; use function count; use UnexpectedValueException; final class IPv6Address extends IPAddress { public static function create(string $ip, ?string $mask = null): self { return new self($ip, $mask); } /** * Initialize from octets. */ public static function fromOctets(string $octets): self { $mask = null; $words = unpack('n*', $octets); $words = $words === false ? [] : $words; switch (count($words)) { case 8: $ip = self::wordsToIPv6String($words); break; case 16: $ip = self::wordsToIPv6String(array_slice($words, 0, 8)); $mask = self::wordsToIPv6String(array_slice($words, 8, 8)); break; default: throw new UnexpectedValueException('Invalid IPv6 octet length.'); } return self::create($ip, $mask); } /** * Convert an array of 16 bit words to an IPv6 string representation. * * @param int[] $words */ protected static function wordsToIPv6String(array $words): string { $groups = array_map(static fn ($word) => sprintf('%04x', $word), $words); return implode(':', $groups); } protected function octets(): string { $words = array_map('hexdec', explode(':', $this->ip)); if (isset($this->mask)) { $words = array_merge($words, array_map('hexdec', explode(':', $this->mask))); } return pack('n*', ...$words); } }