| 1 | <?php |
||
| 7 | class IPv4Address extends IPAddress |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Initialize from octets. |
||
| 11 | * |
||
| 12 | * @param string $octets |
||
| 13 | * |
||
| 14 | * @throws \InvalidArgumentException |
||
| 15 | * |
||
| 16 | 11 | * @return self |
|
| 17 | */ |
||
| 18 | 11 | public static function fromOctets(string $octets): self |
|
| 19 | 11 | { |
|
| 20 | 11 | $mask = null; |
|
| 21 | 11 | $bytes = unpack('C*', $octets) ?: []; |
|
| 22 | 9 | switch (count($bytes)) { |
|
| 23 | 9 | case 4: |
|
| 24 | 2 | $ip = implode('.', $bytes); |
|
| 25 | 1 | break; |
|
| 26 | 1 | case 8: |
|
| 27 | 1 | $ip = implode('.', array_slice($bytes, 0, 4)); |
|
| 28 | $mask = implode('.', array_slice($bytes, 4, 4)); |
||
| 29 | 1 | break; |
|
| 30 | default: |
||
| 31 | 10 | throw new \UnexpectedValueException('Invalid IPv4 octet length.'); |
|
| 32 | } |
||
| 33 | return new self($ip, $mask); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | 16 | */ |
|
| 39 | protected function _octets(): string |
||
| 47 | } |
||
| 48 | } |
||
| 49 |