Passed
Pull Request — master (#33)
by Melech
03:28
created

JsonParamsMessage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getExceptParamsFromRequest() 0 5 1
A getOnlyParamsFromRequest() 0 5 1
A ensureJsonRequest() 0 4 2
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\Routing\Messages;
15
16
use RuntimeException;
17
use Valkyrja\Http\JsonRequest;
18
use Valkyrja\Http\Request;
19
20
/**
21
 * Trait JsonParamsMessage.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
trait JsonParamsMessage
26
{
27
    use Message;
28
29
    /**
30
     * @inheritDoc
31
     */
32
    protected static function getOnlyParamsFromRequest(JsonRequest|Request $request, int|string ...$values): array
33
    {
34
        static::ensureJsonRequest($request);
35
36
        return $request->onlyQueryParams($values);
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    protected static function getExceptParamsFromRequest(JsonRequest|Request $request, int|string ...$values): array
43
    {
44
        static::ensureJsonRequest($request);
45
46
        return $request->exceptQueryParams($values);
47
    }
48
49
    /**
50
     * Ensure the request is a JsonRequest.
51
     *
52
     * @param JsonRequest|Request $request The request
53
     *
54
     * @return void
55
     */
56
    protected static function ensureJsonRequest(JsonRequest|Request $request): void
57
    {
58
        if (! is_a($request, JsonRequest::class)) {
59
            throw new RuntimeException('Json Request is required for this to work.');
60
        }
61
    }
62
}
63