1 | <?php |
||
38 | class IpstackMiddleware implements MiddlewareInterface |
||
39 | { |
||
40 | use LoggerAwareTrait; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Maps ipstack response fields to Request attribute Names. |
||
45 | * |
||
46 | * Array keys are ipstack fields, values are attribute names, |
||
47 | * for example: |
||
48 | * |
||
49 | * array( |
||
50 | * "country_code" => "X-IpstackCountryCode", |
||
51 | * "language" => "X-IpstackLanguage" |
||
52 | * ); |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $ipstack_attributes = array(); |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Specifies the ipstack response fields that will be requested always. |
||
61 | * |
||
62 | * @see https://ipstack.com/documentation#fields |
||
63 | * @var array |
||
64 | */ |
||
65 | public $ipstack_default_fields = array("ip", "country_code", "country_name"); |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Request attribute with IP address as described here: |
||
70 | * http://www.slimframework.com/docs/v3/cookbook/ip-address.html |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | public $ip_address_attribute = "ip_address"; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Request attribute to store the full ipstack information |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public $ipstack_attribute = "ipstack"; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * @var IpstackClientInterface |
||
87 | */ |
||
88 | public $ipstack_client; |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @var integer |
||
93 | */ |
||
94 | public $reponse_error_code = 400; |
||
95 | |||
96 | |||
97 | /** |
||
98 | * @param IpstackClientInterface $ipstack_client IpstackClient |
||
99 | * @param string $ip_address_attribute Optional: Request attribute name with Client IP address |
||
100 | * @param array $request_attributes Optional: Map ipstack fields to request attributes |
||
|
|||
101 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
102 | */ |
||
103 | 48 | public function __construct( IpstackClientInterface $ipstack_client, string $ip_address_attribute = null, array $ipstack_attributes = array(), LoggerInterface $logger = null ) |
|
111 | |||
112 | |||
113 | /** |
||
114 | * PSR-15 "Single pass" pattern |
||
115 | * |
||
116 | * @param ServerRequestInterface $request [description] |
||
117 | * @param RequestHandlerInterface $handler [description] |
||
118 | * @return ResponseInterface |
||
119 | */ |
||
120 | 40 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * Slim3-style "Double Pass" pattern |
||
135 | * |
||
136 | * @param ServerRequestInterface $request |
||
137 | * @param ResponseInterface $response |
||
138 | * @param callable $next |
||
139 | * |
||
140 | * @return ResponseInterface |
||
141 | */ |
||
142 | 40 | public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) |
|
153 | |||
154 | |||
155 | |||
156 | |||
157 | 40 | protected function business( ServerRequestInterface $request ) |
|
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Returns the client's IP, either from request attribute name or REMOTE_ADDR. |
||
181 | * |
||
182 | * @param ServerRequestInterface $request The request |
||
183 | * @return string Client IP address string |
||
184 | */ |
||
185 | 40 | protected function getClientIp(ServerRequestInterface $request) : string |
|
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * Asks the ipstack API about information for the given IP. |
||
211 | * |
||
212 | * If something goes wrong, an array with default values |
||
213 | * will be returned. |
||
214 | * |
||
215 | * @param string $client_ip |
||
216 | * @return array ipstack response excerpt. |
||
217 | */ |
||
218 | 16 | protected function askIpStack( string $client_ip ) : array |
|
253 | |||
254 | |||
255 | |||
256 | /** |
||
257 | * Checks wether a given IP address is not empty and valid IPv4 or IP46. |
||
258 | * |
||
259 | * @param string $client_ip |
||
260 | * @return bool |
||
261 | */ |
||
262 | 40 | protected function assertClientIp( string $client_ip ) : bool |
|
283 | |||
284 | } |
||
285 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.