CoreFiles by mySites.guru
IPv4Address.php48 lines, 1,287 bytes, from Joomla_5.0.2-Stable-Full_Package.zip
Raw History Compare with 5.0.1
md5
6d012da771ecf2ec863a37aee54ae096
sha1
f58252388eaf14cfc0018f6a4caa2ddc75a00a02
sha256
6e38bed7e39e6acb8f38343624246d1e70c7a51a28398e1d4873d7122fbd96b1
<?php

declare(strict_types=1);

namespace SpomkyLabs\Pki\X509\GeneralName;

use function array_slice;
use function count;
use UnexpectedValueException;

final class IPv4Address 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;
        $bytes = unpack('C*', $octets);
        $bytes = $bytes === false ? [] : $bytes;
        switch (count($bytes)) {
            case 4:
                $ip = implode('.', $bytes);
                break;
            case 8:
                $ip = implode('.', array_slice($bytes, 0, 4));
                $mask = implode('.', array_slice($bytes, 4, 4));
                break;
            default:
                throw new UnexpectedValueException('Invalid IPv4 octet length.');
        }
        return self::create($ip, $mask);
    }

    protected function octets(): string
    {
        $bytes = array_map('intval', explode('.', $this->ip));
        if (isset($this->mask)) {
            $bytes = array_merge($bytes, array_map('intval', explode('.', $this->mask)));
        }
        return pack('C*', ...$bytes);
    }
}

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.