CoreFiles by mySites.guru
IPv6Address.php59 lines, 1,648 bytes, from Joomla_5.0.2-Stable-Full_Package.zip
Raw History Compare with 5.0.1
md5
332647d30cea9432c16454b2b6d67d76
sha1
36c8c5aa1ff04abe0d962eb67a032cb5d78c2dc7
sha256
cf3c986df9ad362bc09e505a1dcece7579139b0a0a713421d2212b51180c8472
<?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);
    }
}

The same content is in 22 releases, Joomla 5.0.0-alpha1 to 5.1.0-alpha4. See them all, or every version of this file.