CoreFiles by mySites.guru
IPv4Address.php48 lines, 1,287 bytes, from Joomla_5.3.3-Stable-Full_Package.zip
Raw History Compare with 5.3.2
md5
fcdb41b91eef02509ab75a320c960fb4
sha1
eb3bd2552308c2c14aabbef1a1da5adf15291790
sha256
e6d0120333c02c09c01405fb31db174edf37bb1706a192690dcb5e802ae05d5b
<?php

declare(strict_types=1);

namespace SpomkyLabs\Pki\X509\GeneralName;

use UnexpectedValueException;
use function array_slice;
use function count;

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 105 releases, Joomla 5.1.0-beta1 to 6.1.0-alpha2. See them all, or every version of this file.