|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types = 1); |
| 4 | + |
3 | 5 | namespace Mesour\IpAddresses; |
4 | 6 |
|
5 | | -/** |
6 | | - * @author Matouš Němec <mesour.com> |
7 | | - */ |
| 7 | +/** @author Matouš Němec <mesour.com> */ |
8 | 8 | abstract class IpAddressNormalizer |
9 | 9 | { |
10 | 10 |
|
11 | | - final public static function normalizeIpV6($address): string |
| 11 | + final public static function normalizeIpV6(string $address): string |
12 | 12 | { |
13 | | - if (strpos($address, '::') !== false) { |
14 | | - $part = explode('::', $address); |
15 | | - $part[0] = explode(':', $part[0]); |
16 | | - $part[1] = explode(':', $part[1]); |
| 13 | + if (\strpos($address, '::') !== false) { |
| 14 | + $part = \explode('::', $address); |
| 15 | + $part[0] = \explode(':', $part[0]); |
| 16 | + $part[1] = \explode(':', $part[1]); |
17 | 17 | $missing = []; |
18 | | - for ($i = 0; $i < (8 - (count($part[0]) + count($part[1]))); $i++) { |
19 | | - array_push($missing, '0000'); |
| 18 | + |
| 19 | + for ($i = 0; $i < 8 - (\count($part[0]) + \count($part[1])); $i++) { |
| 20 | + \array_push($missing, '0000'); |
20 | 21 | } |
21 | | - $missing = array_merge($part[0], $missing); |
22 | | - $part = array_merge($missing, $part[1]); |
| 22 | + |
| 23 | + $missing = \array_merge($part[0], $missing); |
| 24 | + $part = \array_merge($missing, $part[1]); |
23 | 25 | } else { |
24 | | - $part = explode(':', $address); |
| 26 | + $part = \explode(':', $address); |
25 | 27 | } |
26 | 28 |
|
27 | 29 | foreach ($part as &$p) { |
28 | | - while (strlen($p) < 4) { |
| 30 | + while (\strlen($p) < 4) { |
29 | 31 | $p = '0' . $p; |
30 | 32 | } |
31 | 33 | } |
| 34 | + |
32 | 35 | unset($p); |
33 | 36 |
|
34 | | - $result = implode(':', $part); |
35 | | - if (strlen($result) == 39) { |
| 37 | + $result = \implode(':', $part); |
| 38 | + |
| 39 | + if (\strlen($result) === 39) { |
36 | 40 | return $result; |
37 | 41 | } |
| 42 | + |
38 | 43 | throw new \UnexpectedValueException('Ip address is not valid.'); |
39 | 44 | } |
40 | 45 |
|
41 | | - final public static function compressIpV6($ip): string |
| 46 | + final public static function compressIpV6(string $ip): string |
42 | 47 | { |
43 | | - if (substr($ip, 0, 4) === '0000') { |
44 | | - $ip = substr_replace($ip, ':0', 0, 4); |
| 48 | + if (\substr($ip, 0, 4) === '0000') { |
| 49 | + $ip = \substr_replace($ip, ':0', 0, 4); |
45 | 50 | } |
46 | | - $ip = str_replace(':0000', ':0', $ip); |
47 | | - $ip = preg_replace('/:0{1,3}(?=\w)/', ':', $ip); |
| 51 | + |
| 52 | + $ip = \str_replace(':0000', ':0', $ip); |
| 53 | + $ip = \preg_replace('/:0{1,3}(?=\w)/', ':', $ip); |
48 | 54 | $z = ':0:0:0:0:0:0:0:'; |
49 | | - while (strpos($ip, '::') === false && strlen($z) >= 5) { |
50 | | - $pos = strpos($ip, $z); |
| 55 | + |
| 56 | + while (\strpos($ip, '::') === false && \strlen($z) >= 5) { |
| 57 | + $pos = \strpos($ip, $z); |
| 58 | + |
51 | 59 | if ($pos !== false) { |
52 | | - $ip = substr_replace($ip, '::', $pos, strlen($z)); |
| 60 | + $ip = \substr_replace($ip, '::', $pos, \strlen($z)); |
| 61 | + |
53 | 62 | break; |
54 | 63 | } |
55 | | - $z = substr($z, 0, strlen($z) - 2); |
| 64 | + |
| 65 | + $z = \substr($z, 0, \strlen($z) - 2); |
56 | 66 | } |
57 | | - if (substr($ip, 1, 1) !== ':') { |
58 | | - return ltrim($ip, ':'); |
| 67 | + |
| 68 | + if (\substr($ip, 1, 1) !== ':') { |
| 69 | + return \ltrim($ip, ':'); |
59 | 70 | } |
| 71 | + |
60 | 72 | return $ip; |
61 | 73 | } |
62 | 74 |
|
|
0 commit comments