1 | <?php |
||
32 | class RemoteIp extends ObjectAbstract implements ValidatorInterface |
||
33 | { |
||
34 | /** |
||
35 | * Allowed ip patterns |
||
36 | * |
||
37 | * @var array |
||
38 | * @access protected |
||
39 | */ |
||
40 | protected $allowed; |
||
41 | |||
42 | /** |
||
43 | * Denied ip patterns |
||
44 | * |
||
45 | * @var array |
||
46 | * @access protected |
||
47 | */ |
||
48 | protected $denied; |
||
49 | |||
50 | /** |
||
51 | * Inject allowed or denied ip patterns like '216.110.124.0/24' etc. |
||
52 | * |
||
53 | * @param array $denied |
||
54 | * @param array $allowed |
||
55 | * @access protected |
||
56 | */ |
||
57 | public function __construct(array $denied = [], array $allowed = []) |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | public function validate()/*# : bool */ |
||
82 | |||
83 | /** |
||
84 | * Match ip with patterns |
||
85 | * |
||
86 | * @param string $ip |
||
87 | * @param array $patterns |
||
88 | * @return boolean |
||
89 | * @access protected |
||
90 | */ |
||
91 | protected function matchIp(/*# string */ $ip, array $patterns)/*# : bool */ |
||
101 | |||
102 | /** |
||
103 | * Pre-process ip matching pattern |
||
104 | * |
||
105 | * @param array $patterns |
||
106 | * @return array |
||
107 | * @access protected |
||
108 | */ |
||
109 | protected function processPattern(array $patterns)/*# : array */ |
||
125 | |||
126 | /** |
||
127 | * Convert mask length to mask in decimal |
||
128 | * |
||
129 | * @param int $length |
||
130 | * @return int |
||
131 | * @access protected |
||
132 | */ |
||
133 | protected function getMask(/*# int */ $length = 32)/*# : int */ |
||
138 | } |
||
139 |
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: