Passed
Push — main ( a6206c...f35a3d )
by Jeroen
10:10
created

Route::getMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider\Api;
6
7
use Attribute;
8
use JsonSerializable;
9
10
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
11 1
final class Route implements JsonSerializable
12
{
13
    private string $pattern;
14
15
    /**
16
     * @var string[]
17
     */
18
    private array $methods;
19
20
    private ?string $name;
21
22
    /**
23
     * Set of middleware FQCNs.
24
     *
25
     * @var string[]
26
     */
27
    private array $middleware;
28
29
    /**
30
     * @param string|string[] $method
31
     * @param string|string[] $middleware
32
     */
33 16
    public function __construct(
34
        string $pattern,
35
        string | array $method = [RequestMethod::GET],
36
        string $name = null,
37
        string | array $middleware = []
38
    ) {
39 16
        $this->pattern    = $pattern;
40 16
        $this->methods    = is_string($method) === true ? [$method] : $method;
0 ignored issues
show
introduced by
The condition is_string($method) === true is always false.
Loading history...
41 16
        $this->name       = $name;
42 16
        $this->middleware = is_string($middleware) === true ? [$middleware] : $middleware;
0 ignored issues
show
introduced by
The condition is_string($middleware) === true is always false.
Loading history...
43 16
    }
44
45
    /**
46
     * @param array<string,mixed> $payload
47
     */
48 4
    public static function fromPayload(array $payload): self
49
    {
50 4
        return new self(
51 4
            $payload['pattern'],
52 4
            $payload['methods'],
53 4
            $payload['name'],
54 4
            $payload['middleware']
55
        );
56
    }
57
58 14
    public function getPattern(): string
59
    {
60 14
        return $this->pattern;
61
    }
62
63
    /**
64
     * @return string[]
65
     */
66 7
    public function getMethods(): array
67
    {
68 7
        return $this->methods;
69
    }
70
71 7
    public function getName(): ?string
72
    {
73 7
        return $this->name;
74
    }
75
76
    /**
77
     * @return string[]
78
     */
79 7
    public function getMiddleware(): array
80
    {
81 7
        return $this->middleware;
82
    }
83
84
    /**
85
     * @return array<string,mixed>
86
     */
87 6
    public function jsonSerialize(): array
88
    {
89
        return [
90 6
            'pattern'    => $this->getPattern(),
91 6
            'methods'    => $this->methods,
92 6
            'name'       => $this->name,
93 6
            'middleware' => $this->middleware,
94
        ];
95
    }
96
}
97