|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
use Psr\Http\Message\StreamInterface; |
|
16
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
17
|
|
|
use Sunrise\Http\Message\Exception\InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
use function array_key_exists; |
|
20
|
|
|
use function array_walk_recursive; |
|
21
|
|
|
use function is_array; |
|
22
|
|
|
use function is_object; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-suppress ClassMustBeFinal |
|
26
|
|
|
*/ |
|
27
|
|
|
class ServerRequest extends Request implements ServerRequestInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
private array $serverParams; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
|
|
private array $queryParams; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
private array $cookieParams; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
private array $uploadedFiles = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array<array-key, mixed>|object|null |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
private $parsedBody = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array<array-key, mixed> |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
private array $attributes; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor of the class |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $uri |
|
63
|
|
|
* @param array<string, string|string[]>|null $headers |
|
64
|
|
|
* |
|
65
|
|
|
* @param array<array-key, mixed> $serverParams |
|
|
|
|
|
|
66
|
|
|
* @param array<array-key, mixed> $queryParams |
|
67
|
|
|
* @param array<array-key, mixed> $cookieParams |
|
68
|
|
|
* @param array<array-key, mixed> $uploadedFiles |
|
69
|
|
|
* @param array<array-key, mixed>|object|null $parsedBody |
|
70
|
|
|
* @param array<array-key, mixed> $attributes |
|
71
|
|
|
* |
|
72
|
|
|
* @throws InvalidArgumentException |
|
73
|
|
|
*/ |
|
74
|
299 |
|
public function __construct( |
|
75
|
|
|
?string $protocolVersion = null, |
|
76
|
|
|
?string $method = null, |
|
77
|
|
|
$uri = null, |
|
78
|
|
|
?array $headers = null, |
|
79
|
|
|
?StreamInterface $body = null, |
|
80
|
|
|
array $serverParams = [], |
|
81
|
|
|
array $queryParams = [], |
|
82
|
|
|
array $cookieParams = [], |
|
83
|
|
|
array $uploadedFiles = [], |
|
84
|
|
|
$parsedBody = null, |
|
85
|
|
|
array $attributes = [] |
|
86
|
|
|
) { |
|
87
|
299 |
|
parent::__construct($method, $uri, $headers, $body); |
|
88
|
|
|
|
|
89
|
279 |
|
if ($protocolVersion !== null) { |
|
90
|
69 |
|
$this->setProtocolVersion($protocolVersion); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
265 |
|
if ($uploadedFiles !== []) { |
|
94
|
4 |
|
$this->setUploadedFiles($uploadedFiles); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
264 |
|
if ($parsedBody !== null) { |
|
98
|
39 |
|
$this->setParsedBody($parsedBody); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
263 |
|
$this->serverParams = $serverParams; |
|
102
|
263 |
|
$this->queryParams = $queryParams; |
|
103
|
263 |
|
$this->cookieParams = $cookieParams; |
|
104
|
263 |
|
$this->attributes = $attributes; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritDoc} |
|
109
|
|
|
* |
|
110
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
31 |
|
public function getServerParams(): array |
|
113
|
|
|
{ |
|
114
|
31 |
|
return $this->serverParams; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritDoc} |
|
119
|
|
|
* |
|
120
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
|
|
121
|
|
|
*/ |
|
122
|
7 |
|
public function getQueryParams(): array |
|
123
|
|
|
{ |
|
124
|
7 |
|
return $this->queryParams; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritDoc} |
|
129
|
|
|
* |
|
130
|
|
|
* @param array<array-key, mixed> $query |
|
|
|
|
|
|
131
|
|
|
* |
|
132
|
|
|
* @return static |
|
133
|
|
|
*/ |
|
134
|
3 |
|
public function withQueryParams(array $query): ServerRequestInterface |
|
135
|
|
|
{ |
|
136
|
3 |
|
$clone = clone $this; |
|
137
|
3 |
|
$clone->queryParams = $query; |
|
138
|
|
|
|
|
139
|
3 |
|
return $clone; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritDoc} |
|
144
|
|
|
* |
|
145
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
|
|
146
|
|
|
*/ |
|
147
|
8 |
|
public function getCookieParams(): array |
|
148
|
|
|
{ |
|
149
|
8 |
|
return $this->cookieParams; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritDoc} |
|
154
|
|
|
* |
|
155
|
|
|
* @param array<array-key, mixed> $cookies |
|
|
|
|
|
|
156
|
|
|
* |
|
157
|
|
|
* @return static |
|
158
|
|
|
*/ |
|
159
|
3 |
|
public function withCookieParams(array $cookies): ServerRequestInterface |
|
160
|
|
|
{ |
|
161
|
3 |
|
$clone = clone $this; |
|
162
|
3 |
|
$clone->cookieParams = $cookies; |
|
163
|
|
|
|
|
164
|
3 |
|
return $clone; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* {@inheritDoc} |
|
169
|
|
|
* |
|
170
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
|
|
171
|
|
|
*/ |
|
172
|
7 |
|
public function getUploadedFiles(): array |
|
173
|
|
|
{ |
|
174
|
7 |
|
return $this->uploadedFiles; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritDoc} |
|
179
|
|
|
* |
|
180
|
|
|
* @param array<array-key, mixed> $uploadedFiles |
|
|
|
|
|
|
181
|
|
|
* |
|
182
|
|
|
* @return static |
|
183
|
|
|
* |
|
184
|
|
|
* @throws InvalidArgumentException |
|
185
|
|
|
*/ |
|
186
|
4 |
|
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
|
187
|
|
|
{ |
|
188
|
4 |
|
$clone = clone $this; |
|
189
|
4 |
|
$clone->setUploadedFiles($uploadedFiles); |
|
190
|
|
|
|
|
191
|
3 |
|
return $clone; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* {@inheritDoc} |
|
196
|
|
|
* |
|
197
|
|
|
* @return array<array-key, mixed>|object|null |
|
|
|
|
|
|
198
|
|
|
*/ |
|
199
|
17 |
|
public function getParsedBody() |
|
200
|
|
|
{ |
|
201
|
17 |
|
return $this->parsedBody; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* {@inheritDoc} |
|
206
|
|
|
* |
|
207
|
|
|
* @param array<array-key, mixed>|object|null $data |
|
|
|
|
|
|
208
|
|
|
* |
|
209
|
|
|
* @return static |
|
210
|
|
|
* |
|
211
|
|
|
* @throws InvalidArgumentException |
|
212
|
|
|
*/ |
|
213
|
12 |
|
public function withParsedBody($data): ServerRequestInterface |
|
214
|
|
|
{ |
|
215
|
12 |
|
$clone = clone $this; |
|
216
|
12 |
|
$clone->setParsedBody($data); |
|
217
|
|
|
|
|
218
|
7 |
|
return $clone; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritDoc} |
|
223
|
|
|
* |
|
224
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
|
|
225
|
|
|
*/ |
|
226
|
5 |
|
public function getAttributes(): array |
|
227
|
|
|
{ |
|
228
|
5 |
|
return $this->attributes; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* {@inheritDoc} |
|
233
|
|
|
* |
|
234
|
|
|
* @param array-key $name |
|
|
|
|
|
|
235
|
|
|
* @param mixed $default |
|
236
|
|
|
* |
|
237
|
|
|
* @return mixed |
|
238
|
|
|
*/ |
|
239
|
6 |
|
public function getAttribute($name, $default = null) |
|
240
|
|
|
{ |
|
241
|
6 |
|
if (!array_key_exists($name, $this->attributes)) { |
|
242
|
4 |
|
return $default; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
4 |
|
return $this->attributes[$name]; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* {@inheritDoc} |
|
250
|
|
|
* |
|
251
|
|
|
* @param array-key $name |
|
|
|
|
|
|
252
|
|
|
* @param mixed $value |
|
253
|
|
|
* |
|
254
|
|
|
* @return static |
|
255
|
|
|
*/ |
|
256
|
7 |
|
public function withAttribute($name, $value): ServerRequestInterface |
|
257
|
|
|
{ |
|
258
|
7 |
|
$clone = clone $this; |
|
259
|
7 |
|
$clone->attributes[$name] = $value; |
|
260
|
|
|
|
|
261
|
7 |
|
return $clone; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* {@inheritDoc} |
|
266
|
|
|
* |
|
267
|
|
|
* @param array-key $name |
|
|
|
|
|
|
268
|
|
|
* |
|
269
|
|
|
* @return static |
|
270
|
|
|
*/ |
|
271
|
2 |
|
public function withoutAttribute($name): ServerRequestInterface |
|
272
|
|
|
{ |
|
273
|
2 |
|
$clone = clone $this; |
|
274
|
2 |
|
unset($clone->attributes[$name]); |
|
275
|
|
|
|
|
276
|
2 |
|
return $clone; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Sets the given uploaded files to the request |
|
281
|
|
|
* |
|
282
|
|
|
* @param array<array-key, mixed> $files |
|
|
|
|
|
|
283
|
|
|
* |
|
284
|
|
|
* @throws InvalidArgumentException |
|
285
|
|
|
*/ |
|
286
|
8 |
|
final protected function setUploadedFiles(array $files): void |
|
287
|
|
|
{ |
|
288
|
8 |
|
$this->validateUploadedFiles($files); |
|
289
|
|
|
|
|
290
|
6 |
|
$this->uploadedFiles = $files; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Sets the given parsed body to the request |
|
295
|
|
|
* |
|
296
|
|
|
* @param array<array-key, mixed>|object|null $data |
|
|
|
|
|
|
297
|
|
|
* |
|
298
|
|
|
* @throws InvalidArgumentException |
|
299
|
|
|
*/ |
|
300
|
51 |
|
final protected function setParsedBody($data): void |
|
301
|
|
|
{ |
|
302
|
51 |
|
$this->validateParsedBody($data); |
|
303
|
|
|
|
|
304
|
45 |
|
$this->parsedBody = $data; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Validates the given uploaded files |
|
309
|
|
|
* |
|
310
|
|
|
* @param array<array-key, mixed> $files |
|
|
|
|
|
|
311
|
|
|
* |
|
312
|
|
|
* @throws InvalidArgumentException |
|
313
|
|
|
*/ |
|
314
|
8 |
|
private function validateUploadedFiles(array $files): void |
|
315
|
|
|
{ |
|
316
|
8 |
|
if ($files === []) { |
|
317
|
1 |
|
return; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
8 |
|
array_walk_recursive($files, /** @param mixed $file */ static function ($file): void { |
|
321
|
8 |
|
if (!($file instanceof UploadedFileInterface)) { |
|
322
|
2 |
|
throw new InvalidArgumentException('Invalid uploaded file'); |
|
323
|
|
|
} |
|
324
|
8 |
|
}); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Validates the given parsed body |
|
329
|
|
|
* |
|
330
|
|
|
* @param mixed $data |
|
331
|
|
|
* |
|
332
|
|
|
* @throws InvalidArgumentException |
|
333
|
|
|
*/ |
|
334
|
51 |
|
private function validateParsedBody($data): void |
|
335
|
|
|
{ |
|
336
|
51 |
|
if ($data === null || is_array($data) || is_object($data)) { |
|
337
|
45 |
|
return; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
6 |
|
throw new InvalidArgumentException('Invalid parsed body'); |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|