1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Psr7. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Http\Message; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use Psr\Http\Message\StreamInterface; |
18
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HTTP Psr7 ServerRequest implementation. |
22
|
|
|
*/ |
23
|
|
|
class ServerRequest extends Request implements ServerRequestInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Retrieve server parameters. |
27
|
|
|
* |
28
|
|
|
* Retrieves data related to the incoming request environment, |
29
|
|
|
* typically derived from PHP's $_SERVER superglobal. The data IS NOT |
30
|
|
|
* REQUIRED to originate from $_SERVER. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getServerParams(): array |
35
|
|
|
{ |
36
|
|
|
} |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieve cookies. |
40
|
|
|
* |
41
|
|
|
* Retrieves cookies sent by the client to the server. |
42
|
|
|
* |
43
|
|
|
* The data MUST be compatible with the structure of the $_COOKIE |
44
|
|
|
* superglobal. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getCookieParams(): array |
49
|
|
|
{ |
50
|
|
|
} |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return an instance with the specified cookies. |
54
|
|
|
* |
55
|
|
|
* The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST |
56
|
|
|
* be compatible with the structure of $_COOKIE. Typically, this data will |
57
|
|
|
* be injected at instantiation. |
58
|
|
|
* |
59
|
|
|
* This method MUST NOT update the related Cookie header of the request |
60
|
|
|
* instance, nor related values in the server params. |
61
|
|
|
* |
62
|
|
|
* This method MUST be implemented in such a way as to retain the |
63
|
|
|
* immutability of the message, and MUST return an instance that has the |
64
|
|
|
* updated cookie values. |
65
|
|
|
* |
66
|
|
|
* @param array $cookies Array of key/value pairs representing cookies. |
67
|
|
|
* |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
|
|
public function withCookieParams(array $cookies): ServerRequestInterface |
71
|
|
|
{ |
72
|
|
|
} |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieve query string arguments. |
76
|
|
|
* |
77
|
|
|
* Retrieves the deserialized query string arguments, if any. |
78
|
|
|
* |
79
|
|
|
* Note: the query params might not be in sync with the URI or server |
80
|
|
|
* params. If you need to ensure you are only getting the original |
81
|
|
|
* values, you may need to parse the query string from `getUri()->getQuery()` |
82
|
|
|
* or from the `QUERY_STRING` server param. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getQueryParams(): array |
87
|
|
|
{ |
88
|
|
|
} |
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return an instance with the specified query string arguments. |
92
|
|
|
* |
93
|
|
|
* These values SHOULD remain immutable over the course of the incoming |
94
|
|
|
* request. They MAY be injected during instantiation, such as from PHP's |
95
|
|
|
* $_GET superglobal, or MAY be derived from some other value such as the |
96
|
|
|
* URI. In cases where the arguments are parsed from the URI, the data |
97
|
|
|
* MUST be compatible with what PHP's parse_str() would return for |
98
|
|
|
* purposes of how duplicate query parameters are handled, and how nested |
99
|
|
|
* sets are handled. |
100
|
|
|
* |
101
|
|
|
* Setting query string arguments MUST NOT change the URI stored by the |
102
|
|
|
* request, nor the values in the server params. |
103
|
|
|
* |
104
|
|
|
* This method MUST be implemented in such a way as to retain the |
105
|
|
|
* immutability of the message, and MUST return an instance that has the |
106
|
|
|
* updated query string arguments. |
107
|
|
|
* |
108
|
|
|
* @param array $query Array of query string arguments, typically from $_GET. |
109
|
|
|
* |
110
|
|
|
* @return static |
111
|
|
|
*/ |
112
|
|
|
public function withQueryParams(array $query): ServerRequestInterface |
113
|
|
|
{ |
114
|
|
|
} |
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retrieve normalized file upload data. |
118
|
|
|
* |
119
|
|
|
* This method returns upload metadata in a normalized tree, with each leaf |
120
|
|
|
* an instance of Psr\Http\Message\UploadedFileInterface. |
121
|
|
|
* |
122
|
|
|
* These values MAY be prepared from $_FILES or the message body during |
123
|
|
|
* instantiation, or MAY be injected via withUploadedFiles(). |
124
|
|
|
* |
125
|
|
|
* @return array An array tree of UploadedFileInterface instances; an empty |
126
|
|
|
* array MUST be returned if no data is present. |
127
|
|
|
*/ |
128
|
|
|
public function getUploadedFiles(): array |
129
|
|
|
{ |
130
|
|
|
} |
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a new instance with the specified uploaded files. |
134
|
|
|
* |
135
|
|
|
* This method MUST be implemented in such a way as to retain the |
136
|
|
|
* immutability of the message, and MUST return an instance that has the |
137
|
|
|
* updated body parameters. |
138
|
|
|
* |
139
|
|
|
* @param array $uploadedFiles An array tree of UploadedFileInterface instances. |
140
|
|
|
* |
141
|
|
|
* @return static |
142
|
|
|
* |
143
|
|
|
* @throws InvalidArgumentException if an invalid structure is provided. |
144
|
|
|
*/ |
145
|
|
|
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
146
|
|
|
{ |
147
|
|
|
} |
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Retrieve any parameters provided in the request body. |
151
|
|
|
* |
152
|
|
|
* If the request Content-Type is either application/x-www-form-urlencoded |
153
|
|
|
* or multipart/form-data, and the request method is POST, this method MUST |
154
|
|
|
* return the contents of $_POST. |
155
|
|
|
* |
156
|
|
|
* Otherwise, this method may return any results of deserializing |
157
|
|
|
* the request body content; as parsing returns structured content, the |
158
|
|
|
* potential types MUST be arrays or objects only. A null value indicates |
159
|
|
|
* the absence of body content. |
160
|
|
|
* |
161
|
|
|
* @return null|array|object The deserialized body parameters, if any. |
162
|
|
|
* These will typically be an array or object. |
163
|
|
|
*/ |
164
|
|
|
public function getParsedBody() |
165
|
|
|
{ |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return an instance with the specified body parameters. |
170
|
|
|
* |
171
|
|
|
* These MAY be injected during instantiation. |
172
|
|
|
* |
173
|
|
|
* If the request Content-Type is either application/x-www-form-urlencoded |
174
|
|
|
* or multipart/form-data, and the request method is POST, use this method |
175
|
|
|
* ONLY to inject the contents of $_POST. |
176
|
|
|
* |
177
|
|
|
* The data IS NOT REQUIRED to come from $_POST, but MUST be the results of |
178
|
|
|
* deserializing the request body content. Deserialization/parsing returns |
179
|
|
|
* structured data, and, as such, this method ONLY accepts arrays or objects, |
180
|
|
|
* or a null value if nothing was available to parse. |
181
|
|
|
* |
182
|
|
|
* As an example, if content negotiation determines that the request data |
183
|
|
|
* is a JSON payload, this method could be used to create a request |
184
|
|
|
* instance with the deserialized parameters. |
185
|
|
|
* |
186
|
|
|
* This method MUST be implemented in such a way as to retain the |
187
|
|
|
* immutability of the message, and MUST return an instance that has the |
188
|
|
|
* updated body parameters. |
189
|
|
|
* |
190
|
|
|
* @param null|array|object $data The deserialized body data. This will |
191
|
|
|
* typically be in an array or object. |
192
|
|
|
* |
193
|
|
|
* @return static |
194
|
|
|
* |
195
|
|
|
* @throws InvalidArgumentException if an unsupported argument type is provided. |
196
|
|
|
*/ |
197
|
|
|
public function withParsedBody($data) |
198
|
|
|
{ |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Retrieve attributes derived from the request. |
203
|
|
|
* |
204
|
|
|
* The request "attributes" may be used to allow injection of any |
205
|
|
|
* parameters derived from the request: e.g., the results of path |
206
|
|
|
* match operations; the results of decrypting cookies; the results of |
207
|
|
|
* deserializing non-form-encoded message bodies; etc. Attributes |
208
|
|
|
* will be application and request specific, and CAN be mutable. |
209
|
|
|
* |
210
|
|
|
* @return array Attributes derived from the request. |
211
|
|
|
*/ |
212
|
|
|
public function getAttributes(): array |
213
|
|
|
{ |
214
|
|
|
} |
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Retrieve a single derived request attribute. |
218
|
|
|
* |
219
|
|
|
* Retrieves a single derived request attribute as described in |
220
|
|
|
* getAttributes(). If the attribute has not been previously set, returns |
221
|
|
|
* the default value as provided. |
222
|
|
|
* |
223
|
|
|
* This method obviates the need for a hasAttribute() method, as it allows |
224
|
|
|
* specifying a default value to return if the attribute is not found. |
225
|
|
|
* |
226
|
|
|
* @see getAttributes() |
227
|
|
|
* |
228
|
|
|
* @param string $name The attribute name. |
229
|
|
|
* @param mixed $default Default value to return if the attribute does not exist. |
230
|
|
|
* |
231
|
|
|
* @return mixed |
232
|
|
|
*/ |
233
|
|
|
public function getAttribute(string $name, $default = null) |
234
|
|
|
{ |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return an instance with the specified derived request attribute. |
239
|
|
|
* |
240
|
|
|
* This method allows setting a single derived request attribute as |
241
|
|
|
* described in getAttributes(). |
242
|
|
|
* |
243
|
|
|
* This method MUST be implemented in such a way as to retain the |
244
|
|
|
* immutability of the message, and MUST return an instance that has the |
245
|
|
|
* updated attribute. |
246
|
|
|
* |
247
|
|
|
* @see getAttributes() |
248
|
|
|
* |
249
|
|
|
* @param string $name The attribute name. |
250
|
|
|
* @param mixed $value The value of the attribute. |
251
|
|
|
* |
252
|
|
|
* @return static |
253
|
|
|
*/ |
254
|
|
|
public function withAttribute(string $name, $value): ServerRequestInterface |
255
|
|
|
{ |
256
|
|
|
} |
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return an instance that removes the specified derived request attribute. |
260
|
|
|
* |
261
|
|
|
* This method allows removing a single derived request attribute as |
262
|
|
|
* described in getAttributes(). |
263
|
|
|
* |
264
|
|
|
* This method MUST be implemented in such a way as to retain the |
265
|
|
|
* immutability of the message, and MUST return an instance that removes |
266
|
|
|
* the attribute. |
267
|
|
|
* |
268
|
|
|
* @see getAttributes() |
269
|
|
|
* |
270
|
|
|
* @param string $name The attribute name. |
271
|
|
|
* |
272
|
|
|
* @return static |
273
|
|
|
*/ |
274
|
|
|
public function withoutAttribute(string $name): ServerRequestInterface |
275
|
|
|
{ |
276
|
|
|
} |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: