@@ -6,59 +6,59 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IPv6Address extends IPAddress |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Initialize from octets. |
|
| 11 | - * |
|
| 12 | - * @param string $octets |
|
| 13 | - * |
|
| 14 | - * @throws \InvalidArgumentException |
|
| 15 | - * |
|
| 16 | - * @return self |
|
| 17 | - */ |
|
| 18 | - public static function fromOctets(string $octets): self |
|
| 19 | - { |
|
| 20 | - $mask = null; |
|
| 21 | - $words = unpack('n*', $octets) ?: []; |
|
| 22 | - switch (count($words)) { |
|
| 23 | - case 8: |
|
| 24 | - $ip = self::_wordsToIPv6String($words); |
|
| 25 | - break; |
|
| 26 | - case 16: |
|
| 27 | - $ip = self::_wordsToIPv6String(array_slice($words, 0, 8)); |
|
| 28 | - $mask = self::_wordsToIPv6String(array_slice($words, 8, 8)); |
|
| 29 | - break; |
|
| 30 | - default: |
|
| 31 | - throw new \UnexpectedValueException('Invalid IPv6 octet length.'); |
|
| 32 | - } |
|
| 33 | - return new self($ip, $mask); |
|
| 34 | - } |
|
| 9 | + /** |
|
| 10 | + * Initialize from octets. |
|
| 11 | + * |
|
| 12 | + * @param string $octets |
|
| 13 | + * |
|
| 14 | + * @throws \InvalidArgumentException |
|
| 15 | + * |
|
| 16 | + * @return self |
|
| 17 | + */ |
|
| 18 | + public static function fromOctets(string $octets): self |
|
| 19 | + { |
|
| 20 | + $mask = null; |
|
| 21 | + $words = unpack('n*', $octets) ?: []; |
|
| 22 | + switch (count($words)) { |
|
| 23 | + case 8: |
|
| 24 | + $ip = self::_wordsToIPv6String($words); |
|
| 25 | + break; |
|
| 26 | + case 16: |
|
| 27 | + $ip = self::_wordsToIPv6String(array_slice($words, 0, 8)); |
|
| 28 | + $mask = self::_wordsToIPv6String(array_slice($words, 8, 8)); |
|
| 29 | + break; |
|
| 30 | + default: |
|
| 31 | + throw new \UnexpectedValueException('Invalid IPv6 octet length.'); |
|
| 32 | + } |
|
| 33 | + return new self($ip, $mask); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Convert an array of 16 bit words to an IPv6 string representation. |
|
| 38 | - * |
|
| 39 | - * @param int[] $words |
|
| 40 | - * |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - protected static function _wordsToIPv6String(array $words): string |
|
| 44 | - { |
|
| 45 | - $groups = array_map( |
|
| 46 | - function ($word) { |
|
| 47 | - return sprintf('%04x', $word); |
|
| 48 | - }, $words); |
|
| 49 | - return implode(':', $groups); |
|
| 50 | - } |
|
| 36 | + /** |
|
| 37 | + * Convert an array of 16 bit words to an IPv6 string representation. |
|
| 38 | + * |
|
| 39 | + * @param int[] $words |
|
| 40 | + * |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + protected static function _wordsToIPv6String(array $words): string |
|
| 44 | + { |
|
| 45 | + $groups = array_map( |
|
| 46 | + function ($word) { |
|
| 47 | + return sprintf('%04x', $word); |
|
| 48 | + }, $words); |
|
| 49 | + return implode(':', $groups); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - protected function _octets(): string |
|
| 56 | - { |
|
| 57 | - $words = array_map('hexdec', explode(':', $this->_ip)); |
|
| 58 | - if (isset($this->_mask)) { |
|
| 59 | - $words = array_merge($words, |
|
| 60 | - array_map('hexdec', explode(':', $this->_mask))); |
|
| 61 | - } |
|
| 62 | - return pack('n*', ...$words); |
|
| 63 | - } |
|
| 52 | + /** |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + protected function _octets(): string |
|
| 56 | + { |
|
| 57 | + $words = array_map('hexdec', explode(':', $this->_ip)); |
|
| 58 | + if (isset($this->_mask)) { |
|
| 59 | + $words = array_merge($words, |
|
| 60 | + array_map('hexdec', explode(':', $this->_mask))); |
|
| 61 | + } |
|
| 62 | + return pack('n*', ...$words); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -6,43 +6,43 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IPv4Address extends IPAddress |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Initialize from octets. |
|
| 11 | - * |
|
| 12 | - * @param string $octets |
|
| 13 | - * |
|
| 14 | - * @throws \InvalidArgumentException |
|
| 15 | - * |
|
| 16 | - * @return self |
|
| 17 | - */ |
|
| 18 | - public static function fromOctets(string $octets): self |
|
| 19 | - { |
|
| 20 | - $mask = null; |
|
| 21 | - $bytes = unpack('C*', $octets) ?: []; |
|
| 22 | - switch (count($bytes)) { |
|
| 23 | - case 4: |
|
| 24 | - $ip = implode('.', $bytes); |
|
| 25 | - break; |
|
| 26 | - case 8: |
|
| 27 | - $ip = implode('.', array_slice($bytes, 0, 4)); |
|
| 28 | - $mask = implode('.', array_slice($bytes, 4, 4)); |
|
| 29 | - break; |
|
| 30 | - default: |
|
| 31 | - throw new \UnexpectedValueException('Invalid IPv4 octet length.'); |
|
| 32 | - } |
|
| 33 | - return new self($ip, $mask); |
|
| 34 | - } |
|
| 9 | + /** |
|
| 10 | + * Initialize from octets. |
|
| 11 | + * |
|
| 12 | + * @param string $octets |
|
| 13 | + * |
|
| 14 | + * @throws \InvalidArgumentException |
|
| 15 | + * |
|
| 16 | + * @return self |
|
| 17 | + */ |
|
| 18 | + public static function fromOctets(string $octets): self |
|
| 19 | + { |
|
| 20 | + $mask = null; |
|
| 21 | + $bytes = unpack('C*', $octets) ?: []; |
|
| 22 | + switch (count($bytes)) { |
|
| 23 | + case 4: |
|
| 24 | + $ip = implode('.', $bytes); |
|
| 25 | + break; |
|
| 26 | + case 8: |
|
| 27 | + $ip = implode('.', array_slice($bytes, 0, 4)); |
|
| 28 | + $mask = implode('.', array_slice($bytes, 4, 4)); |
|
| 29 | + break; |
|
| 30 | + default: |
|
| 31 | + throw new \UnexpectedValueException('Invalid IPv4 octet length.'); |
|
| 32 | + } |
|
| 33 | + return new self($ip, $mask); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - protected function _octets(): string |
|
| 40 | - { |
|
| 41 | - $bytes = array_map('intval', explode('.', $this->_ip)); |
|
| 42 | - if (isset($this->_mask)) { |
|
| 43 | - $bytes = array_merge($bytes, |
|
| 44 | - array_map('intval', explode('.', $this->_mask))); |
|
| 45 | - } |
|
| 46 | - return pack('C*', ...$bytes); |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + protected function _octets(): string |
|
| 40 | + { |
|
| 41 | + $bytes = array_map('intval', explode('.', $this->_ip)); |
|
| 42 | + if (isset($this->_mask)) { |
|
| 43 | + $bytes = array_merge($bytes, |
|
| 44 | + array_map('intval', explode('.', $this->_mask))); |
|
| 45 | + } |
|
| 46 | + return pack('C*', ...$bytes); |
|
| 47 | + } |
|
| 48 | 48 | } |