Route::getName()   A
last analyzed

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_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
11
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
    private ?string $host;
30
31
    /**
32
     * @var string[]
33
     */
34
    private array $schemes;
35
36
    private ?int $httpPort;
37
    private ?int $httpsPort;
38
39
    /**
40
     * @var array<string, mixed>
41
     */
42
    private array $options;
43
44
    /**
45
     * @param string|string[]      $method
46
     * @param string|string[]      $middleware
47
     * @param string|string[]      $schemes
48
     * @param array<string, mixed> $options
49
     */
50 14
    public function __construct(
51
        string $pattern,
52
        string|array $method = [RequestMethod::GET],
53
        ?string $name = null,
54
        string|array $middleware = [],
55
        ?string $host = null,
56
        string|array $schemes = [],
57
        ?int $httpPort = null,
58
        ?int $httpsPort = null,
59
        array $options = [],
60
    ) {
61 14
        $this->pattern    = $pattern;
62 14
        $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...
63 14
        $this->name       = $name;
64 14
        $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...
65 14
        $this->host       = $host;
66 14
        $this->schemes    = is_string($schemes) === true ? [$schemes] : $schemes;
0 ignored issues
show
introduced by
The condition is_string($schemes) === true is always false.
Loading history...
67 14
        $this->httpPort   = $httpPort;
68 14
        $this->httpsPort  = $httpsPort;
69 14
        $this->options    = $options;
70
    }
71
72
    /**
73
     * @param array{
74
     *     pattern: string,
75
     *     methods: string|list<string>,
76
     *     name: null|string,
77
     *     middleware: string|list<string>,
78
     *     host: null|string,
79
     *     schemes: string|list<string>,
80
     *     httpPort: null|int,
81
     *     httpsPort: null|int,
82
     *     options: array<string, mixed>
83
     * } $payload
84
     */
85 4
    public static function fromPayload(array $payload) : self
86
    {
87 4
        return new self(
88 4
            $payload['pattern'],
89 4
            $payload['methods'],
90 4
            $payload['name'],
91 4
            $payload['middleware'],
92 4
            $payload['host'],
93 4
            $payload['schemes'],
94 4
            $payload['httpPort'],
95 4
            $payload['httpsPort'],
96 4
            $payload['options']
97 4
        );
98
    }
99
100 12
    public function getPattern() : string
101
    {
102 12
        return $this->pattern;
103
    }
104
105
    /**
106
     * @return string[]
107
     */
108 5
    public function getMethods() : array
109
    {
110 5
        return $this->methods;
111
    }
112
113 5
    public function getName() : ?string
114
    {
115 5
        return $this->name;
116
    }
117
118
    /**
119
     * @return string[]
120
     */
121 5
    public function getMiddleware() : array
122
    {
123 5
        return $this->middleware;
124
    }
125
126 1
    public function getHost() : ?string
127
    {
128 1
        return $this->host;
129
    }
130
131
    /**
132
     * @return string[]
133
     */
134 1
    public function getSchemes() : array
135
    {
136 1
        return $this->schemes;
137
    }
138
139 1
    public function getHttpPort() : ?int
140
    {
141 1
        return $this->httpPort;
142
    }
143
144 1
    public function getHttpsPort() : ?int
145
    {
146 1
        return $this->httpsPort;
147
    }
148
149
    /**
150
     * @return array<string, mixed>
151
     */
152 1
    public function getOptions() : array
153
    {
154 1
        return $this->options;
155
    }
156
157
    /**
158
     * @return array<string,mixed>
159
     */
160 6
    public function jsonSerialize() : array
161
    {
162 6
        return [
163 6
            'pattern'    => $this->getPattern(),
164 6
            'methods'    => $this->methods,
165 6
            'name'       => $this->name,
166 6
            'middleware' => $this->middleware,
167 6
            'host'       => $this->host,
168 6
            'schemes'    => $this->schemes,
169 6
            'httpPort'   => $this->httpPort,
170 6
            'httpsPort'  => $this->httpsPort,
171 6
            'options'    => $this->options,
172 6
        ];
173
    }
174
}
175