1 | <?php |
||
39 | final class Request |
||
40 | { |
||
41 | use ProxyTrait; |
||
42 | |||
43 | /** |
||
44 | * @const string HTTP content types |
||
45 | */ |
||
46 | public const TYPE_ANY = '*/*'; |
||
47 | public const TYPE_HTML = 'text/html'; |
||
48 | public const TYPE_JSON = 'application/json'; |
||
49 | |||
50 | /** |
||
51 | * @var array|null Accepted type |
||
52 | */ |
||
53 | static private $accept; |
||
54 | |||
55 | /** |
||
56 | * Init instance |
||
57 | * |
||
58 | * @throws ComponentException |
||
59 | */ |
||
60 | private static function initInstance() |
||
64 | |||
65 | /** |
||
66 | * Retrieve a member of the $_GET super global |
||
67 | * |
||
68 | * If no $key is passed, returns the entire $_GET array. |
||
69 | * |
||
70 | * @param string $key |
||
71 | * @param string $default Default value to use if key not found |
||
72 | * |
||
73 | * @return string Returns null if key does not exist |
||
74 | */ |
||
75 | 77 | public static function getQuery($key = null, $default = null) |
|
79 | |||
80 | /** |
||
81 | * Retrieve a member of the $_POST super global |
||
82 | * |
||
83 | * If no $key is passed, returns the entire $_POST array. |
||
84 | * |
||
85 | * @param string $key |
||
86 | * @param string $default Default value to use if key not found |
||
87 | * |
||
88 | * @return string Returns null if key does not exist |
||
89 | */ |
||
90 | 67 | public static function getPost($key = null, $default = null) |
|
94 | |||
95 | /** |
||
96 | * Retrieve a member of the $_SERVER super global |
||
97 | * |
||
98 | * If no $key is passed, returns the entire $_SERVER array. |
||
99 | * |
||
100 | * @param string $key |
||
101 | * @param string $default Default value to use if key not found |
||
102 | * |
||
103 | * @return string Returns null if key does not exist |
||
104 | */ |
||
105 | 8 | public static function getServer($key = null, $default = null) |
|
109 | |||
110 | /** |
||
111 | * Retrieve a member of the $_COOKIE super global |
||
112 | * |
||
113 | * If no $key is passed, returns the entire $_COOKIE array. |
||
114 | * |
||
115 | * @param string $key |
||
116 | * @param string $default Default value to use if key not found |
||
117 | * |
||
118 | * @return string Returns null if key does not exist |
||
119 | */ |
||
120 | 1 | public static function getCookie($key = null, $default = null) |
|
124 | |||
125 | /** |
||
126 | * Retrieve a member of the $_ENV super global |
||
127 | * |
||
128 | * If no $key is passed, returns the entire $_ENV array. |
||
129 | * |
||
130 | * @param string $key |
||
131 | * @param string $default Default value to use if key not found |
||
132 | * |
||
133 | * @return string Returns null if key does not exist |
||
134 | */ |
||
135 | 1 | public static function getEnv($key = null, $default = null) |
|
136 | { |
||
137 | 1 | return RequestFactory::get($key, $_ENV, $default); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Search for a header value |
||
142 | * |
||
143 | * @param string $header |
||
144 | * @param mixed $default |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 40 | public static function getHeader($header, $default = null) |
|
152 | |||
153 | /** |
||
154 | * Access values contained in the superglobals as public members |
||
155 | * Order of precedence: 1. GET, 2. POST |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @param null $default |
||
159 | * |
||
160 | * @return string|null |
||
161 | * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
162 | */ |
||
163 | 77 | public static function getParam($key, $default = null) |
|
170 | |||
171 | /** |
||
172 | * Get all params from GET and POST or PUT |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | 36 | public static function getParams() |
|
177 | { |
||
178 | 36 | $body = (array)self::getInstance()->getParsedBody(); |
|
179 | 36 | $query = (array)self::getInstance()->getQueryParams(); |
|
180 | 36 | return array_merge($body, $query); |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get uploaded file |
||
185 | * |
||
186 | * @param string $name |
||
187 | * |
||
188 | * @return \Zend\Diactoros\UploadedFile |
||
189 | */ |
||
190 | public static function getFile($name) |
||
191 | { |
||
192 | return RequestFactory::get($name, self::getInstance()->getUploadedFiles()); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get the client's IP address |
||
197 | * |
||
198 | * @param bool $checkProxy |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 1 | public static function getClientIp($checkProxy = true) |
|
203 | { |
||
204 | 1 | $result = null; |
|
205 | 1 | if ($checkProxy) { |
|
206 | 1 | $result = self::getServer('HTTP_CLIENT_IP') ?? self::getServer('HTTP_X_FORWARDED_FOR') ?? null; |
|
207 | } |
||
208 | 1 | return $result ?? self::getServer('REMOTE_ADDR'); |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get module |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 46 | public static function getModule(): string |
|
220 | |||
221 | /** |
||
222 | * Get controller |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 46 | public static function getController(): string |
|
230 | |||
231 | /** |
||
232 | * Get method |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | 25 | public static function getMethod(): string |
|
240 | |||
241 | /** |
||
242 | * Get Accept MIME Type |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 40 | public static function getAccept(): array |
|
285 | |||
286 | /** |
||
287 | * Reset accept for tests |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | 849 | public static function resetAccept() |
|
292 | { |
||
293 | 849 | self::$accept = null; |
|
294 | 849 | } |
|
295 | |||
296 | /** |
||
297 | * Check Accept header |
||
298 | * |
||
299 | * @param array $allowTypes |
||
300 | * |
||
301 | * @return string|false |
||
302 | */ |
||
303 | 40 | public static function checkAccept(array $allowTypes = []) |
|
323 | |||
324 | /** |
||
325 | * Check CLI |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | 1 | public static function isCli(): bool |
|
330 | { |
||
331 | 1 | return (PHP_SAPI === 'cli'); |
|
332 | } |
||
333 | |||
334 | /** |
||
335 | * Check HTTP |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | 3 | public static function isHttp(): bool |
|
343 | |||
344 | /** |
||
345 | * Is this a GET method request? |
||
346 | * |
||
347 | * @return bool |
||
348 | */ |
||
349 | 2 | public static function isGet(): bool |
|
350 | { |
||
351 | 2 | return (self::getInstance()->getMethod() === RequestMethod::GET); |
|
352 | } |
||
353 | |||
354 | /** |
||
355 | * Is this a POST method request? |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | 2 | public static function isPost(): bool |
|
360 | { |
||
361 | 2 | return (self::getInstance()->getMethod() === RequestMethod::POST); |
|
362 | } |
||
363 | |||
364 | /** |
||
365 | * Is this a PUT method request? |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | 1 | public static function isPut(): bool |
|
370 | { |
||
371 | 1 | return (self::getInstance()->getMethod() === RequestMethod::PUT); |
|
372 | } |
||
373 | |||
374 | /** |
||
375 | * Is this a DELETE method request? |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | 1 | public static function isDelete(): bool |
|
380 | { |
||
381 | 1 | return (self::getInstance()->getMethod() === RequestMethod::DELETE); |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * Is the request a Javascript XMLHttpRequest? |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | 9 | public static function isXmlHttpRequest(): bool |
|
393 | } |
||
394 |