ServerRequest   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 405
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 39
eloc 99
dl 0
loc 405
rs 9.28
c 0
b 0
f 0

35 Methods

Rating   Name   Duplication   Size   Complexity  
A exceptAttributes() 0 4 1
A getCookieParam() 0 4 1
A hasQueryParam() 0 5 2
A hasParsedBodyParam() 0 5 2
A withAttribute() 0 8 1
A getParsedBodyParam() 0 4 1
A getQueryParam() 0 4 1
A exceptQueryParams() 0 4 1
A getAttribute() 0 4 1
A isXmlHttpRequest() 0 4 1
A exceptParsedBody() 0 4 1
A onlyParams() 0 6 1
A withCookieParams() 0 8 1
A getAttributes() 0 4 1
A hasCookieParam() 0 5 2
A withAddedParsedBodyParam() 0 8 1
A exceptParams() 0 6 1
A withAddedCookieParam() 0 8 1
A getServerParam() 0 4 1
A withAddedUploadedFile() 0 8 1
A withAddedQueryParam() 0 8 1
A getServerParams() 0 4 1
A getQueryParams() 0 4 1
A onlyAttributes() 0 4 1
A onlyParsedBody() 0 4 1
A withoutAttribute() 0 8 1
A getParsedBody() 0 4 1
A __construct() 0 15 1
A withParsedBody() 0 8 1
A getCookieParams() 0 4 1
A hasServerParam() 0 5 2
A withUploadedFiles() 0 8 1
A onlyQueryParams() 0 4 1
A getUploadedFiles() 0 4 1
A withQueryParams() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Message\Request;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Http\Message\Constant\HeaderName;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Constant\HeaderName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Valkyrja\Http\Message\Enum\ProtocolVersion;
19
use Valkyrja\Http\Message\Enum\RequestMethod;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Enum\RequestMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Valkyrja\Http\Message\Exception\InvalidArgumentException;
21
use Valkyrja\Http\Message\File\Contract\UploadedFile;
22
use Valkyrja\Http\Message\Request\Contract\ServerRequest as Contract;
23
use Valkyrja\Http\Message\Stream\Contract\Stream;
24
use Valkyrja\Http\Message\Stream\Enum\PhpWrapper;
25
use Valkyrja\Http\Message\Stream\Stream as HttpStream;
26
use Valkyrja\Http\Message\Uri\Contract\Uri;
27
use Valkyrja\Http\Message\Uri\Uri as HttpUri;
28
29
use function array_filter;
30
use function array_key_exists;
31
use function in_array;
32
33
use const ARRAY_FILTER_USE_KEY;
34
35
/**
36
 * Class ServerRequest.
37
 *
38
 * @author Melech Mizrachi
39
 */
40
class ServerRequest extends Request implements Contract
41
{
42
    /**
43
     * The attributes.
44
     *
45
     * @var array<string, mixed>
46
     */
47
    protected array $attributes = [];
48
49
    /**
50
     * ServerRequest constructor.
51
     *
52
     * @param Uri                        $uri        [optional] The uri
53
     * @param RequestMethod              $method     [optional] The method
54
     * @param Stream                     $body       [optional] The body stream
55
     * @param array<string, string[]>    $headers    [optional] The headers
56
     * @param array<string, mixed>       $server     [optional] The server
57
     * @param array<string, string|null> $cookies    [optional] The cookies
58
     * @param array<array-key, mixed>    $query      [optional] The query string
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
59
     * @param array<array-key, mixed>    $parsedBody [optional] The parsed body
60
     * @param ProtocolVersion            $protocol   [optional] The protocol version
61
     * @param UploadedFile[]|array       $files      [optional] The files
62
     *
63
     * @throws InvalidArgumentException
64
     */
65
    public function __construct(
66
        Uri $uri = new HttpUri(),
67
        RequestMethod $method = RequestMethod::GET,
68
        Stream $body = new HttpStream(stream: PhpWrapper::input),
69
        array $headers = [],
70
        protected array $server = [],
71
        protected array $cookies = [],
72
        protected array $query = [],
73
        protected array $parsedBody = [],
74
        ProtocolVersion $protocol = ProtocolVersion::V1_1,
75
        protected array $files = []
76
    ) {
77
        $this->protocolVersion = $protocol;
78
79
        parent::__construct($uri, $method, $body, $headers);
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    #[Override]
86
    public function getServerParams(): array
87
    {
88
        return $this->server;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    #[Override]
95
    public function getServerParam(string $name, mixed $default = null): mixed
96
    {
97
        return $this->server[$name] ?? $default;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    #[Override]
104
    public function hasServerParam(string $name): bool
105
    {
106
        return isset($this->server[$name])
107
            || array_key_exists($name, $this->server);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    #[Override]
114
    public function getCookieParams(): array
115
    {
116
        return $this->cookies;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    #[Override]
123
    public function withCookieParams(array $cookies): static
124
    {
125
        $new = clone $this;
126
127
        $new->cookies = $cookies;
128
129
        return $new;
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    #[Override]
136
    public function withAddedCookieParam(string $name, string|null $value = null): static
137
    {
138
        $new = clone $this;
139
140
        $new->cookies[$name] = $value;
141
142
        return $new;
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    #[Override]
149
    public function getCookieParam(string $name, string|null $default = null): string|null
150
    {
151
        return $this->cookies[$name] ?? $default;
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    #[Override]
158
    public function hasCookieParam(string $name): bool
159
    {
160
        return isset($this->cookies[$name])
161
            || array_key_exists($name, $this->cookies);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    #[Override]
168
    public function getQueryParams(): array
169
    {
170
        return $this->query;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    #[Override]
177
    public function onlyQueryParams(string|int ...$names): array
178
    {
179
        return $this->onlyParams($this->query, ...$names);
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    #[Override]
186
    public function exceptQueryParams(string|int ...$names): array
187
    {
188
        return $this->exceptParams($this->query, ...$names);
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    #[Override]
195
    public function withQueryParams(array $query): static
196
    {
197
        $new = clone $this;
198
199
        $new->query = $query;
200
201
        return $new;
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    #[Override]
208
    public function withAddedQueryParam(string|int $name, mixed $value): static
209
    {
210
        $new = clone $this;
211
212
        $new->query[$name] = $value;
213
214
        return $new;
215
    }
216
217
    /**
218
     * @inheritDoc
219
     */
220
    #[Override]
221
    public function getQueryParam(string|int $name, mixed $default = null): mixed
222
    {
223
        return $this->query[$name] ?? $default;
224
    }
225
226
    /**
227
     * @inheritDoc
228
     */
229
    #[Override]
230
    public function hasQueryParam(string|int $name): bool
231
    {
232
        return isset($this->query[$name])
233
            || array_key_exists($name, $this->query);
234
    }
235
236
    /**
237
     * @inheritDoc
238
     */
239
    #[Override]
240
    public function getUploadedFiles(): array
241
    {
242
        return $this->files;
243
    }
244
245
    /**
246
     * @inheritDoc
247
     */
248
    #[Override]
249
    public function withUploadedFiles(array $uploadedFiles): static
250
    {
251
        $new = clone $this;
252
253
        $new->files = $uploadedFiles;
254
255
        return $new;
256
    }
257
258
    /**
259
     * @inheritDoc
260
     */
261
    #[Override]
262
    public function withAddedUploadedFile(UploadedFile $uploadedFile): static
263
    {
264
        $new = clone $this;
265
266
        $new->files[] = $uploadedFile;
267
268
        return $new;
269
    }
270
271
    /**
272
     * @inheritDoc
273
     */
274
    #[Override]
275
    public function getParsedBody(): array
276
    {
277
        return $this->parsedBody;
278
    }
279
280
    /**
281
     * @inheritDoc
282
     */
283
    #[Override]
284
    public function onlyParsedBody(string|int ...$names): array
285
    {
286
        return $this->onlyParams($this->parsedBody, ...$names);
287
    }
288
289
    /**
290
     * @inheritDoc
291
     */
292
    #[Override]
293
    public function exceptParsedBody(string|int ...$names): array
294
    {
295
        return $this->exceptParams($this->parsedBody, ...$names);
296
    }
297
298
    /**
299
     * @inheritDoc
300
     */
301
    #[Override]
302
    public function withParsedBody(array $data): static
303
    {
304
        $new = clone $this;
305
306
        $new->parsedBody = $data;
307
308
        return $new;
309
    }
310
311
    /**
312
     * @inheritDoc
313
     */
314
    #[Override]
315
    public function withAddedParsedBodyParam(string|int $name, mixed $value): static
316
    {
317
        $new = clone $this;
318
319
        $new->parsedBody[$name] = $value;
320
321
        return $new;
322
    }
323
324
    /**
325
     * @inheritDoc
326
     */
327
    #[Override]
328
    public function getParsedBodyParam(string|int $name, mixed $default = null): mixed
329
    {
330
        return $this->parsedBody[$name] ?? $default;
331
    }
332
333
    /**
334
     * @inheritDoc
335
     */
336
    #[Override]
337
    public function hasParsedBodyParam(string|int $name): bool
338
    {
339
        return isset($this->parsedBody[$name])
340
            || array_key_exists($name, $this->parsedBody);
341
    }
342
343
    /**
344
     * @inheritDoc
345
     */
346
    #[Override]
347
    public function getAttributes(): array
348
    {
349
        return $this->attributes;
350
    }
351
352
    /**
353
     * @inheritDoc
354
     */
355
    #[Override]
356
    public function onlyAttributes(string ...$names): array
357
    {
358
        return $this->onlyParams($this->attributes, ...$names);
359
    }
360
361
    /**
362
     * @inheritDoc
363
     */
364
    #[Override]
365
    public function exceptAttributes(string ...$names): array
366
    {
367
        return $this->exceptParams($this->attributes, ...$names);
368
    }
369
370
    /**
371
     * @inheritDoc
372
     */
373
    #[Override]
374
    public function getAttribute(string $name, mixed $default = null): mixed
375
    {
376
        return $this->attributes[$name] ?? $default;
377
    }
378
379
    /**
380
     * @inheritDoc
381
     */
382
    #[Override]
383
    public function withAttribute(string $name, mixed $value): static
384
    {
385
        $new = clone $this;
386
387
        $new->attributes[$name] = $value;
388
389
        return $new;
390
    }
391
392
    /**
393
     * @inheritDoc
394
     */
395
    #[Override]
396
    public function withoutAttribute(string $name): static
397
    {
398
        $new = clone $this;
399
400
        unset($new->attributes[$name]);
401
402
        return $new;
403
    }
404
405
    /**
406
     * @inheritDoc
407
     */
408
    #[Override]
409
    public function isXmlHttpRequest(): bool
410
    {
411
        return $this->getHeaderLine(HeaderName::X_REQUESTED_WITH) === 'XMLHttpRequest';
412
    }
413
414
    /**
415
     * Retrieve only the specified params.
416
     *
417
     * @param array<array-key, mixed> $params   The params to sort through
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
418
     * @param string|int              ...$names The query param names to retrieve
419
     *
420
     * @return array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
421
     */
422
    protected function onlyParams(array $params, string|int ...$names): array
423
    {
424
        return array_filter(
425
            $params,
426
            static fn (string|int $name): bool => in_array($name, $names, true),
427
            ARRAY_FILTER_USE_KEY
428
        );
429
    }
430
431
    /**
432
     * Retrieve all params except the ones specified.
433
     *
434
     * @param array<array-key, mixed> $params   The params to sort through
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
435
     * @param string|int              ...$names The query param names to not retrieve
436
     *
437
     * @return array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
438
     */
439
    protected function exceptParams(array $params, string|int ...$names): array
440
    {
441
        return array_filter(
442
            $params,
443
            static fn (string|int $name): bool => ! in_array($name, $names, true),
444
            ARRAY_FILTER_USE_KEY
445
        );
446
    }
447
}
448