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) |
||
32 | |||
33 | /** |
||
34 | * Create IP from string. |
||
35 | * |
||
36 | * @param $string |
||
37 | * |
||
38 | * @return IP |
||
39 | */ |
||
40 | public static function fromString($string) |
||
44 | |||
45 | /** |
||
46 | * Create IP from current request. |
||
47 | * |
||
48 | * @return IP |
||
49 | */ |
||
50 | public static function fromCurrentRequest() |
||
54 | |||
55 | /** |
||
56 | * Check if string is a valid IP. |
||
57 | * |
||
58 | * @param string $value |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | public static function validate($value) |
||
66 | |||
67 | /** |
||
68 | * Get the version (IPv4 or IPv6) of the IP. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getVersion() |
||
80 | |||
81 | /** |
||
82 | * Compare equality with another Host. |
||
83 | * |
||
84 | * @param Host $other |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function equals(Host $other) |
||
92 | |||
93 | /** |
||
94 | * Get string representation of IP. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function toString() |
||
102 | |||
103 | /** |
||
104 | * Cast IP to string. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function __toString() |
||
112 | } |
||
113 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: