1 | <?php |
||
16 | class Toolbox |
||
17 | { |
||
18 | /** |
||
19 | * Expands an IPv6 address to its full, non-shorthand representation. |
||
20 | * |
||
21 | * E.g. 2001:db8:9a::42 -> 2001:0db8:009a:0000:0000:0000:0000:0042 |
||
22 | * |
||
23 | * @param string $ip IPv6 address |
||
24 | * |
||
25 | * @throws \InvalidArgumentException |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | 10 | public static function expandIpv6(string $ip): string |
|
43 | |||
44 | /** |
||
45 | * This function will expand in incomplete IPv6 address. |
||
46 | * An incomplete IPv6 address is of the form `2001:db8:ff:abcd` |
||
47 | * i.e. one where there is less than eight hextets. |
||
48 | * |
||
49 | * @param string $ip IPv6 address |
||
50 | * |
||
51 | * @return string Expanded incomplete IPv6 address |
||
52 | */ |
||
53 | 2 | public static function expandIncompleteIpv6(string $ip): string |
|
61 | |||
62 | /** |
||
63 | * Takes a valid IPv6 address and contracts it |
||
64 | * to its shorter version. |
||
65 | * |
||
66 | * E.g.: 2001:0000:0000:acad:0000:0000:0000:0001 -> 2001:0:0:acad::1 |
||
67 | * |
||
68 | * Note: If there is more than one set of consecutive hextets, the function |
||
69 | * will favour the larger of the sets. If both sets of zeroes are the same |
||
70 | * the first will be favoured in the omission of zeroes. |
||
71 | * |
||
72 | * E.g.: 2001:0000:0000:ab80:2390:0000:0000:000a -> 2001:0:0:ab80:2390::a |
||
73 | * |
||
74 | * @param string $ip IPv6 address |
||
75 | * |
||
76 | * @throws \InvalidArgumentException |
||
77 | * |
||
78 | * @return string Contracted IPv6 address |
||
79 | */ |
||
80 | 11 | public static function contractIpv6(string $ip): string |
|
96 | |||
97 | /** |
||
98 | * Creates a reverse IPv4 address. |
||
99 | * |
||
100 | * E.g. 192.168.1.213 -> 213.1.168.192.in-addr.arpa. |
||
101 | * |
||
102 | * @param string $ip Valid IPv4 address |
||
103 | * |
||
104 | * @return string Reversed IP address appended with ".in-addr.arpa." |
||
105 | */ |
||
106 | 2 | public static function reverseIpv4(string $ip): string |
|
112 | |||
113 | /** |
||
114 | * Creates a reverse IPv6 address. |
||
115 | * |
||
116 | * E.g. 2001:db8::567:89ab -> b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa. |
||
117 | * |
||
118 | * @param string $ip A full or partial IPv6 address |
||
119 | * @param bool $appendSuffix Whether or not to append ".ip6.arpa.' suffix. |
||
120 | * |
||
121 | * @return string The reversed address appended with ".ip6.arpa." |
||
122 | */ |
||
123 | 2 | public static function reverseIpv6(string $ip, bool $appendSuffix = true): string |
|
138 | } |
||
139 |