1 | <?php |
||
21 | class Request |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Class properties |
||
26 | */ |
||
27 | |||
28 | /** |
||
29 | * Unique identifier for the request |
||
30 | * |
||
31 | * @type string |
||
32 | */ |
||
33 | protected $id; |
||
34 | |||
35 | /** |
||
36 | * GET (query) parameters |
||
37 | * |
||
38 | * @type DataCollection |
||
39 | */ |
||
40 | protected $params_get; |
||
41 | |||
42 | /** |
||
43 | * POST parameters |
||
44 | * |
||
45 | * @type DataCollection |
||
46 | */ |
||
47 | protected $params_post; |
||
48 | |||
49 | /** |
||
50 | * Named parameters |
||
51 | * |
||
52 | * @type DataCollection |
||
53 | */ |
||
54 | protected $params_named; |
||
55 | |||
56 | /** |
||
57 | * Client cookie data |
||
58 | * |
||
59 | * @type DataCollection |
||
60 | */ |
||
61 | protected $cookies; |
||
62 | |||
63 | /** |
||
64 | * Server created attributes |
||
65 | * |
||
66 | * @type ServerDataCollection |
||
67 | */ |
||
68 | protected $server; |
||
69 | |||
70 | /** |
||
71 | * HTTP request headers |
||
72 | * |
||
73 | * @type HeaderDataCollection |
||
74 | */ |
||
75 | protected $headers; |
||
76 | |||
77 | /** |
||
78 | * Uploaded temporary files |
||
79 | * |
||
80 | * @type DataCollection |
||
81 | */ |
||
82 | protected $files; |
||
83 | |||
84 | /** |
||
85 | * The request body |
||
86 | * |
||
87 | * @type string |
||
88 | */ |
||
89 | protected $body; |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Methods |
||
94 | */ |
||
95 | |||
96 | /** |
||
97 | * Constructor |
||
98 | * |
||
99 | * Create a new Request object and define all of its request data |
||
100 | * |
||
101 | * @param array $params_get |
||
102 | * @param array $params_post |
||
103 | * @param array $cookies |
||
104 | * @param array $server |
||
105 | * @param array $files |
||
106 | * @param string $body |
||
107 | */ |
||
108 | public function __construct( |
||
128 | |||
129 | /** |
||
130 | * Create a new request object using the built-in "superglobals" |
||
131 | * |
||
132 | * @link http://php.net/manual/en/language.variables.superglobals.php |
||
133 | * @return Request |
||
134 | */ |
||
135 | public static function createFromGlobals() |
||
147 | |||
148 | /** |
||
149 | * Gets a unique ID for the request |
||
150 | * |
||
151 | * Generates one on the first call |
||
152 | * |
||
153 | * @param boolean $hash Whether or not to hash the ID on creation |
||
154 | * @return string |
||
155 | */ |
||
156 | public function id($hash = true) |
||
168 | |||
169 | /** |
||
170 | * Returns the GET parameters collection |
||
171 | * |
||
172 | * @return \Klein\DataCollection\DataCollection |
||
173 | */ |
||
174 | public function paramsGet() |
||
178 | |||
179 | /** |
||
180 | * Returns the POST parameters collection |
||
181 | * |
||
182 | * @return \Klein\DataCollection\DataCollection |
||
183 | */ |
||
184 | public function paramsPost() |
||
188 | |||
189 | /** |
||
190 | * Returns the named parameters collection |
||
191 | * |
||
192 | * @return \Klein\DataCollection\DataCollection |
||
193 | */ |
||
194 | public function paramsNamed() |
||
198 | |||
199 | /** |
||
200 | * Returns the cookies collection |
||
201 | * |
||
202 | * @return \Klein\DataCollection\DataCollection |
||
203 | */ |
||
204 | public function cookies() |
||
208 | |||
209 | /** |
||
210 | * Returns the server collection |
||
211 | * |
||
212 | * @return \Klein\DataCollection\DataCollection |
||
213 | */ |
||
214 | public function server() |
||
218 | |||
219 | /** |
||
220 | * Returns the headers collection |
||
221 | * |
||
222 | * @return \Klein\DataCollection\HeaderDataCollection |
||
223 | */ |
||
224 | public function headers() |
||
228 | |||
229 | /** |
||
230 | * Returns the files collection |
||
231 | * |
||
232 | * @return \Klein\DataCollection\DataCollection |
||
233 | */ |
||
234 | public function files() |
||
238 | |||
239 | /** |
||
240 | * Gets the request body |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function body() |
||
253 | |||
254 | /** |
||
255 | * Returns all parameters (GET, POST, named, and cookies) that match the mask |
||
256 | * |
||
257 | * Takes an optional mask param that contains the names of any params |
||
258 | * you'd like this method to exclude in the returned array |
||
259 | * |
||
260 | * @see \Klein\DataCollection\DataCollection::all() |
||
261 | * @param array $mask The parameter mask array |
||
262 | * @param boolean $fill_with_nulls Whether or not to fill the returned array |
||
263 | * with null values to match the given mask |
||
264 | * @return array |
||
265 | */ |
||
266 | public function params($mask = null, $fill_with_nulls = true) |
||
287 | |||
288 | /** |
||
289 | * Return a request parameter, or $default if it doesn't exist |
||
290 | * |
||
291 | * @param string $key The name of the parameter to return |
||
292 | * @param mixed $default The default value of the parameter if it contains no value |
||
293 | * @return string |
||
294 | */ |
||
295 | public function param($key, $default = null) |
||
302 | |||
303 | /** |
||
304 | * Magic "__isset" method |
||
305 | * |
||
306 | * Allows the ability to arbitrarily check the existence of a parameter |
||
307 | * from this instance while treating it as an instance property |
||
308 | * |
||
309 | * @param string $param The name of the parameter |
||
310 | * @return boolean |
||
311 | */ |
||
312 | public function __isset($param) |
||
319 | |||
320 | /** |
||
321 | * Magic "__get" method |
||
322 | * |
||
323 | * Allows the ability to arbitrarily request a parameter from this instance |
||
324 | * while treating it as an instance property |
||
325 | * |
||
326 | * @param string $param The name of the parameter |
||
327 | * @return string |
||
328 | */ |
||
329 | public function __get($param) |
||
333 | |||
334 | /** |
||
335 | * Magic "__set" method |
||
336 | * |
||
337 | * Allows the ability to arbitrarily set a parameter from this instance |
||
338 | * while treating it as an instance property |
||
339 | * |
||
340 | * NOTE: This currently sets the "named" parameters, since that's the |
||
341 | * one collection that we have the most sane control over |
||
342 | * |
||
343 | * @param string $param The name of the parameter |
||
344 | * @param mixed $value The value of the parameter |
||
345 | * @return void |
||
346 | */ |
||
347 | public function __set($param, $value) |
||
351 | |||
352 | /** |
||
353 | * Magic "__unset" method |
||
354 | * |
||
355 | * Allows the ability to arbitrarily remove a parameter from this instance |
||
356 | * while treating it as an instance property |
||
357 | * |
||
358 | * @param string $param The name of the parameter |
||
359 | * @return void |
||
360 | */ |
||
361 | public function __unset($param) |
||
365 | |||
366 | /** |
||
367 | * Is the request secure? |
||
368 | * |
||
369 | * @return boolean |
||
370 | */ |
||
371 | public function isSecure() |
||
375 | |||
376 | /** |
||
377 | * Gets the request IP address |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | public function ip() |
||
385 | |||
386 | /** |
||
387 | * Gets the request user agent |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public function userAgent() |
||
395 | |||
396 | /** |
||
397 | * Gets the request URI |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | public function uri() |
||
405 | |||
406 | /** |
||
407 | * Get the request's pathname |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | public function pathname() |
||
420 | |||
421 | /** |
||
422 | * Gets the request method, or checks it against $is |
||
423 | * |
||
424 | * <code> |
||
425 | * // POST request example |
||
426 | * $request->method() // returns 'POST' |
||
427 | * $request->method('post') // returns true |
||
428 | * $request->method('get') // returns false |
||
429 | * </code> |
||
430 | * |
||
431 | * @param string $is The method to check the current request method against |
||
432 | * @param boolean $allow_override Whether or not to allow HTTP method overriding via header or params |
||
433 | * @return string|boolean |
||
434 | */ |
||
435 | public function method($is = null, $allow_override = true) |
||
458 | |||
459 | /** |
||
460 | * Adds to or modifies the current query string |
||
461 | * |
||
462 | * @param string $key The name of the query param |
||
463 | * @param mixed $value The value of the query param |
||
464 | * @return string |
||
465 | */ |
||
466 | public function query($key, $value = null) |
||
489 | } |
||
490 |
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: