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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router;
15
16
use Sunrise\Coder\MediaTypeInterface;
17
18
/**
19
 * @since 1.0.0
20
 */
21
final class Route implements RouteInterface
22
{
23 52
    public function __construct(
24
        private readonly string $name,
25
        private readonly string $path,
26
        private readonly mixed $requestHandler,
27
        /** @var array<string, string> */
28
        private readonly array $patterns = [],
29
        /** @var array<array-key, string> */
30
        private readonly array $methods = [],
31
        /** @var array<string, mixed> */
32
        private array $attributes = [],
33
        /** @var array<array-key, mixed> */
34
        private readonly array $middlewares = [],
35
        /** @var array<array-key, MediaTypeInterface> */
36
        private readonly array $consumes = [],
37
        /** @var array<array-key, MediaTypeInterface> */
38
        private readonly array $produces = [],
39
        /** @var array<array-key, string> */
40
        private readonly array $tags = [],
41
        private readonly string $summary = '',
42
        private readonly string $description = '',
43
        private readonly bool $isDeprecated = false,
44
        private readonly bool $isApiRoute = false,
45
        /** @var non-empty-string|null */
46
        private readonly ?string $pattern = null,
47
    ) {
48 52
    }
49
50 8
    public function getName(): string
51
    {
52 8
        return $this->name;
53
    }
54
55 9
    public function getPath(): string
56
    {
57 9
        return $this->path;
58
    }
59
60 9
    public function getRequestHandler(): mixed
61
    {
62 9
        return $this->requestHandler;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 8
    public function getPatterns(): array
69
    {
70 8
        return $this->patterns;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 33
    public function getMethods(): array
77
    {
78 33
        return $this->methods;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 2
    public function getAttributes(): array
85
    {
86 2
        return $this->attributes;
87
    }
88
89 1
    public function hasAttribute(string $name): bool
90
    {
91 1
        return isset($this->attributes[$name]);
92
    }
93
94 2
    public function getAttribute(string $name, mixed $default = null): mixed
95
    {
96 2
        return $this->attributes[$name] ?? $default;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 1
    public function withAddedAttributes(array $attributes): static
103
    {
104 1
        $clone = clone $this;
105 1
        foreach ($attributes as $name => $value) {
106 1
            $clone->attributes[$name] = $value;
107
        }
108
109 1
        return $clone;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115 2
    public function getMiddlewares(): array
116
    {
117 2
        return $this->middlewares;
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123 5
    public function getConsumedMediaTypes(): array
124
    {
125 5
        return $this->consumes;
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131 5
    public function getProducedMediaTypes(): array
132
    {
133 5
        return $this->produces;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139 8
    public function getTags(): array
140
    {
141 8
        return $this->tags;
142
    }
143
144 8
    public function getSummary(): string
145
    {
146 8
        return $this->summary;
147
    }
148
149 5
    public function getDescription(): string
150
    {
151 5
        return $this->description;
152
    }
153
154 5
    public function isDeprecated(): bool
155
    {
156 5
        return $this->isDeprecated;
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162 13
    public function isApiRoute(): bool
163
    {
164 13
        return $this->isApiRoute;
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170 4
    public function getPattern(): ?string
171
    {
172 4
        return $this->pattern;
173
    }
174
}
175