IPv4Address::_octets()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
nc 2
cc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\GeneralName;
6
7
class IPv4Address extends IPAddress
8
{
9
    /**
10
     * Initialize from octets.
11
     *
12
     * @param string $octets
13
     * @throws \InvalidArgumentException
14
     * @return self
15
     */
16 11
    public static function fromOctets(string $octets): self
17
    {
18 11
        $mask = null;
19 11
        $bytes = unpack("C*", $octets);
20 11
        switch (count($bytes)) {
21 11
            case 4:
22 9
                $ip = implode(".", $bytes);
23 9
                break;
24 2
            case 8:
25 1
                $ip = implode(".", array_slice($bytes, 0, 4));
26 1
                $mask = implode(".", array_slice($bytes, 4, 4));
27 1
                break;
28
            default:
29 1
                throw new \UnexpectedValueException("Invalid IPv4 octet length.");
30
        }
31 10
        return new self($ip, $mask);
32
    }
33
    
34
    /**
35
     *
36
     * {@inheritdoc}
37
     */
38 16
    protected function _octets(): string
39
    {
40 16
        $bytes = array_map("intval", explode(".", $this->_ip));
41 16
        if (isset($this->_mask)) {
42 1
            $bytes = array_merge($bytes,
43 1
                array_map("intval", explode(".", $this->_mask)));
44
        }
45 16
        return pack("C*", ...$bytes);
46
    }
47
}
48