1 | <?php |
||
7 | class IP implements Host |
||
8 | { |
||
9 | const IPV4 = 'IPv4'; |
||
10 | const IPV6 = 'IPv6'; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $value; |
||
16 | |||
17 | /** |
||
18 | * Initialize IP. |
||
19 | * |
||
20 | * @param $value |
||
21 | * |
||
22 | * @throws InvalidArgumentException |
||
23 | */ |
||
24 | private function __construct($value) |
||
25 | { |
||
26 | if (!self::validate($value)) { |
||
27 | throw new InvalidArgumentException('The first parameter of IP must be a valid IP address.'); |
||
28 | } |
||
29 | |||
30 | $this->value = $value; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Create IP from string. |
||
35 | * |
||
36 | * @param $string |
||
37 | * |
||
38 | * @return IP |
||
39 | */ |
||
40 | public static function fromString($string) |
||
41 | { |
||
42 | return new self($string); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Check if string is a valid IP. |
||
47 | * |
||
48 | * @param string $value |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | public static function validate($value) |
||
56 | |||
57 | /** |
||
58 | * Get the version (IPv4 or IPv6) of the IP. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getVersion() |
||
71 | |||
72 | /** |
||
73 | * Compare equality with another Host. |
||
74 | * |
||
75 | * @param Host $other |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function equals(Host $other) |
||
83 | |||
84 | /** |
||
85 | * Get string representation of IP. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function toString() |
||
93 | |||
94 | /** |
||
95 | * Cast IP to string. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | public function __toString() |
||
103 | } |
||
104 |