1 | <?php |
||
25 | class Request extends HttpRequest implements ServerRequestInterface |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $server; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $cookies; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $queryParams; |
||
42 | |||
43 | /** |
||
44 | * @var UploadedFile[] |
||
45 | */ |
||
46 | private $uploadedFiles; |
||
47 | |||
48 | /** |
||
49 | * @var mixed |
||
50 | */ |
||
51 | private $parsedBody; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $attributes = []; |
||
57 | |||
58 | /** |
||
59 | * Creates an HTTP Server Request Message |
||
60 | * |
||
61 | * @param string $method |
||
62 | * @param string|StreamInterface $body |
||
63 | * @param null|string|UriInterface $target |
||
64 | * @param array $headers |
||
65 | */ |
||
66 | public function __construct($method = null, $target = null, $body = '', array $headers = []) |
||
79 | |||
80 | /** |
||
81 | * Retrieve server parameters. |
||
82 | * |
||
83 | * Retrieves data related to the incoming request environment, |
||
84 | * typically derived from PHP's $_SERVER superglobal. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getServerParams() |
||
95 | |||
96 | /** |
||
97 | * Retrieve cookies. |
||
98 | * |
||
99 | * Retrieves cookies sent by the client to the server. |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getCookieParams() |
||
110 | |||
111 | /** |
||
112 | * Return an instance with the specified cookies. |
||
113 | * |
||
114 | * @param array $cookies Array of key/value pairs representing cookies. |
||
115 | * @return Request |
||
116 | */ |
||
117 | public function withCookieParams(array $cookies) |
||
123 | |||
124 | /** |
||
125 | * Retrieve query string arguments. |
||
126 | * |
||
127 | * Retrieves the deserialized query string arguments, if any. |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getQueryParams() |
||
138 | |||
139 | /** |
||
140 | * Return an instance with the specified query string arguments. |
||
141 | * |
||
142 | * @param array $query Array of query string arguments, typically from |
||
143 | * $_GET. |
||
144 | * |
||
145 | * @return Request |
||
146 | */ |
||
147 | public function withQueryParams(array $query) |
||
153 | |||
154 | /** |
||
155 | * Retrieve normalized file upload data. |
||
156 | * |
||
157 | * @return UploadedFile[] |
||
158 | */ |
||
159 | public function getUploadedFiles() |
||
166 | |||
167 | /** |
||
168 | * Create a new instance with the specified uploaded files. |
||
169 | * |
||
170 | * @param array $uploadedFiles An array tree of UploadedFileInterface instances. |
||
171 | * @return Request |
||
172 | * |
||
173 | * @throws InvalidArgumentException if an invalid structure is provided. |
||
174 | */ |
||
175 | public function withUploadedFiles(array $uploadedFiles) |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Detects the query params from server and/or request URI |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | private function detectQueryParams() |
||
201 | |||
202 | /** |
||
203 | * Creates a stream from php input stream |
||
204 | * |
||
205 | * @return StreamInterface |
||
206 | */ |
||
207 | private function getPhpInputStream() |
||
212 | |||
213 | /** |
||
214 | * Check if provided files array is valid |
||
215 | * |
||
216 | * @param array $files |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | private function checkUploadedFiles(array $files) |
||
238 | |||
239 | /** |
||
240 | * Loads the headers form request |
||
241 | */ |
||
242 | private function loadHeaders() |
||
258 | |||
259 | /** |
||
260 | * Retrieve any parameters provided in the request body. |
||
261 | * |
||
262 | * @return null|array|object The deserialized body parameters, if any. |
||
263 | * These will typically be an array or object. |
||
264 | */ |
||
265 | public function getParsedBody() |
||
273 | |||
274 | /** |
||
275 | * Return an instance with the specified body parameters. |
||
276 | * |
||
277 | * @param null|array|object $data The deserialized body data. This will |
||
278 | * typically be in an array or object. |
||
279 | * |
||
280 | * @return Request |
||
281 | * @throws InvalidArgumentException if an unsupported argument type is |
||
282 | * provided. |
||
283 | */ |
||
284 | public function withParsedBody($data) |
||
301 | |||
302 | /** |
||
303 | * Retrieve attributes derived from the request. |
||
304 | * |
||
305 | * The request "attributes" may be used to allow injection of any |
||
306 | * parameters derived from the request: e.g., the results of path |
||
307 | * match operations; the results of decrypting cookies; the results of |
||
308 | * deserializing non-form-encoded message bodies; etc. Attributes |
||
309 | * will be application and request specific, and CAN be mutable. |
||
310 | * |
||
311 | * @return mixed[] Attributes derived from the request. |
||
312 | */ |
||
313 | public function getAttributes() |
||
317 | |||
318 | /** |
||
319 | * Return an instance with the specified derived request attribute. |
||
320 | * |
||
321 | * This method allows setting a single derived request attribute as |
||
322 | * described in getAttributes(). |
||
323 | * |
||
324 | * @see getAttributes() |
||
325 | * @param string $name The attribute name. |
||
326 | * @param mixed $value The value of the attribute. |
||
327 | * |
||
328 | * @return Request |
||
329 | */ |
||
330 | public function withAttribute($name, $value) |
||
336 | |||
337 | /** |
||
338 | * Retrieve a single derived request attribute. |
||
339 | * |
||
340 | * Retrieves a single derived request attribute as described in |
||
341 | * getAttributes(). If the attribute has not been previously set, returns |
||
342 | * the default value as provided. |
||
343 | * |
||
344 | * @see getAttributes() |
||
345 | * @param string $name The attribute name. |
||
346 | * @param mixed $default Default value to return if the attribute does not exist. |
||
347 | * @return mixed |
||
348 | */ |
||
349 | public function getAttribute($name, $default = null) |
||
356 | |||
357 | /** |
||
358 | * Return an instance that removes the specified derived request attribute. |
||
359 | * |
||
360 | * This method allows removing a single derived request attribute as |
||
361 | * described in getAttributes(). |
||
362 | * |
||
363 | * @see getAttributes() |
||
364 | * @param string $name The attribute name. |
||
365 | * |
||
366 | * @return Request |
||
367 | */ |
||
368 | public function withoutAttribute($name) |
||
376 | |||
377 | } |
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: