| 1 | <?php | ||
| 7 | class IP implements Host | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * @var string | ||
| 11 | */ | ||
| 12 | private $value; | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Initialize IP. | ||
| 16 | * | ||
| 17 | * @param $value | ||
| 18 | * | ||
| 19 | * @throws InvalidArgumentException | ||
| 20 | */ | ||
| 21 | private function __construct($value) | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Create IP from string. | ||
| 32 | * | ||
| 33 | * @param $string | ||
| 34 | * | ||
| 35 | * @return IP | ||
| 36 | */ | ||
| 37 | public static function fromString($string) | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Create IP from current request. | ||
| 44 | * | ||
| 45 | * @return IP | ||
| 46 | */ | ||
| 47 | public static function fromCurrentRequest() | ||
| 51 | |||
| 52 | /** | ||
| 53 | * Check if string is a valid IP. | ||
| 54 | * | ||
| 55 | * @param string $value | ||
| 56 | * | ||
| 57 | * @return bool | ||
| 58 | */ | ||
| 59 | public static function validate($value) | ||
| 60 |     { | ||
| 61 | return filter_var($value, FILTER_VALIDATE_IP) !== false; | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Compare equality with another Host. | ||
| 66 | * | ||
| 67 | * @param Host $other | ||
| 68 | * | ||
| 69 | * @return bool | ||
| 70 | */ | ||
| 71 | public function equals(Host $other) | ||
| 75 | |||
| 76 | /** | ||
| 77 | * Get string representation of IP. | ||
| 78 | * | ||
| 79 | * @return string | ||
| 80 | */ | ||
| 81 | public function toString() | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Cast IP to string. | ||
| 88 | * | ||
| 89 | * @return string | ||
| 90 | */ | ||
| 91 | public function __toString() | ||
| 95 | } | ||
| 96 | 
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: