ServerRequest::hasQueryParam()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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