Passed
Push — master ( 68a4f0...620b93 )
by Divine Niiquaye
02:09
created

Route::getName()   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
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Annotation;
19
20
use Flight\Routing\Exceptions\InvalidAnnotationException;
21
22
/**
23
 * Annotation class for @Route().
24
 *
25
 * @Annotation
26
 *
27
 * ```php
28
 * <?php
29
 *  /**
30
 *   * @Route("/blog" name="blog", methods={"GET", "HEAD"}, defaults={"_locale" = "en"})
31
 *   * /
32
 *  class Blog
33
 *  {
34
 *     /**
35
 *      * @Route("/{_locale}", name="index", methods={"GET", "HEAD"})
36
 *      * /
37
 *     public function index()
38
 *     {
39
 *     }
40
 *     /**
41
 *      * @Route("/{_locale}/{id}", name="post", methods={"GET", "HEAD"}, patterns={"id" = "[0-9]+"})
42
 *      * /
43
 *     public function show($id)
44
 *     {
45
 *     }
46
 *  }
47
 * ```
48
 *
49
 * @Target({"CLASS", "METHOD"})
50
 */
51
final class Route
52
{
53
    /** @var string @Required */
54
    private $path;
55
56
    /** @var null|string @Required */
57
    private $name;
58
59
    /** @var string[] @Required */
60
    private $methods;
61
62
    /** @var null|string */
63
    private $domain;
64
65
    /** @var string[] */
66
    private $schemes;
67
68
    /** @var string[] */
69
    private $middlewares;
70
71
    /** @var array<string,string> */
72
    private $patterns;
73
74
    /** @var array<string,mixed> */
75
    private $defaults;
76
77
    /**
78
     * @param array<string,mixed><string,mixed> $params
79
     */
80 107
    public function __construct(array $params)
81
    {
82 107
        if (isset($params['value'])) {
83 1
            $params['path'] = $params['value'];
84 1
            unset($params['value']);
85
        }
86
87 107
        $params = \array_merge([
88 107
            'middlewares' => [],
89
            'patterns'    => [],
90
            'defaults'    => [],
91
            'schemes'     => [],
92
            'methods'     => [],
93
            'domain'      => null,
94
            'name'        => null,
95 107
        ], $params);
96
97 107
        $this->assertParamsContainValidName($params);
98 96
        $this->assertParamsContainValidPath($params);
99 85
        $this->assertParamsContainValidMethods($params);
100 66
        $this->assertParamsContainValidSchemes($params);
101 48
        $this->assertParamsContainValidMiddlewares($params);
102 30
        $this->assertParamsContainValidPatterns($params);
103 12
        $this->assertParamsContainValidDefaults($params);
104
105 3
        $this->name        = $params['name'];
106 3
        $this->path        = $params['path'];
107 3
        $this->methods     = $params['methods'];
108 3
        $this->domain      = $params['domain'];
109 3
        $this->middlewares = $params['middlewares'];
110 3
        $this->schemes     = $params['schemes'];
111 3
        $this->patterns    = $params['patterns'];
112 3
        $this->defaults    = $params['defaults'];
113 3
    }
114
115
    /**
116
     * @return string
117
     */
118 2
    public function getPath(): string
119
    {
120 2
        return $this->path;
121
    }
122
123
    /**
124
     * @return null|string
125
     */
126 2
    public function getDomain(): ?string
127
    {
128 2
        return $this->domain;
129
    }
130
131
    /**
132
     * @return null|string
133
     */
134 3
    public function getName(): ?string
135
    {
136 3
        return $this->name;
137
    }
138
139
    /**
140
     * @return array<string,string>
141
     */
142 2
    public function getPatterns(): array
143
    {
144 2
        return $this->patterns;
145
    }
146
147
    /**
148
     * @return string[]
149
     */
150 2
    public function getMethods(): array
151
    {
152 2
        return $this->methods;
153
    }
154
155
    /**
156
     * @return string[]
157
     */
158 2
    public function getMiddlewares(): array
159
    {
160 2
        return $this->middlewares;
161
    }
162
163
    /**
164
     * @return string
165
     */
166 2
    public function getSchemes(): array
167
    {
168 2
        return $this->schemes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schemes returns the type string[] which is incompatible with the documented return type string.
Loading history...
169
    }
170
171
    /**
172
     * @return array<string,mixed>
173
     */
174 2
    public function getDefaults(): array
175
    {
176 2
        return $this->defaults;
177
    }
178
179
    /**
180
     * @param array<string,mixed> $params
181
     *
182
     * @throws InvalidAnnotationException
183
     */
184 107
    private function assertParamsContainValidName(array $params): void
185
    {
186 107
        if (null === $params['name']) {
187 1
            return;
188
        }
189
190 106
        if (empty($params['name']) || !\is_string($params['name'])) {
191 11
            throw new InvalidAnnotationException(\sprintf(
192 11
                '@Route.name must %s.',
193 11
                empty($params['name']) ? 'be not an empty string' : 'contain only a string'
194
            ));
195
        }
196 95
    }
197
198
    /**
199
     * @param array<string,mixed> $params
200
     *
201
     * @throws InvalidAnnotationException
202
     */
203 96
    private function assertParamsContainValidPath(array $params): void
204
    {
205 96
        if (empty($params['path']) || !\is_string($params['path'])) {
206 11
            throw new InvalidAnnotationException('@Route.path must be not an empty string.');
207
        }
208 85
    }
209
210
    /**
211
     * @param array<string,mixed> $params
212
     *
213
     * @throws InvalidAnnotationException
214
     */
215 85
    private function assertParamsContainValidMethods(array $params): void
216
    {
217 85
        if (!\is_array($params['methods'])) {
218 10
            throw new InvalidAnnotationException('@Route.methods must contain only an array.');
219
        }
220
221 75
        foreach ($params['methods'] as $method) {
222 75
            if (!\is_string($method)) {
223 9
                throw new InvalidAnnotationException('@Route.methods must contain only strings.');
224
            }
225
        }
226 66
    }
227
228
    /**
229
     * @param array<string,mixed> $params
230
     *
231
     * @throws InvalidAnnotationException
232
     */
233 48
    private function assertParamsContainValidMiddlewares(array $params): void
234
    {
235 48
        if (!\is_array($params['middlewares'])) {
236 9
            throw new InvalidAnnotationException('@Route.middlewares must be an array.');
237
        }
238
239 39
        foreach ($params['middlewares'] as $middleware) {
240 10
            if (!\is_string($middleware)) {
241 9
                throw new InvalidAnnotationException('@Route.middlewares must contain only strings.');
242
            }
243
        }
244 30
    }
245
246
    /**
247
     * @param array<string,mixed> $params
248
     *
249
     * @throws InvalidAnnotationException
250
     */
251 30
    private function assertParamsContainValidPatterns(array $params): void
252
    {
253 30
        if (!\is_array($params['patterns'])) {
254 9
            throw new InvalidAnnotationException('@Route.patterns must be an array.');
255
        }
256
257 21
        foreach ($params['patterns'] as $pattern) {
258 10
            if (!\is_string($pattern)) {
259 9
                throw new InvalidAnnotationException('@Route.patterns must contain only strings.');
260
            }
261
        }
262 12
    }
263
264
    /**
265
     * @param array<string,mixed> $params
266
     *
267
     * @throws InvalidAnnotationException
268
     */
269 66
    private function assertParamsContainValidSchemes(array $params): void
270
    {
271 66
        if (!\is_array($params['schemes'])) {
272 9
            throw new InvalidAnnotationException('@Route.schemes must be an array.');
273
        }
274
275 57
        foreach ($params['schemes'] as $scheme) {
276 10
            if (!\is_string($scheme)) {
277 9
                throw new InvalidAnnotationException('@Route.schemes must contain only strings.');
278
            }
279
        }
280 48
    }
281
282
    /**
283
     * @param array<string,mixed> $params
284
     *
285
     * @throws InvalidAnnotationException
286
     */
287 12
    private function assertParamsContainValidDefaults(array $params): void
288
    {
289 12
        if (!\is_array($params['defaults'])) {
290 9
            throw new InvalidAnnotationException('@Route.defaults must be an array.');
291
        }
292 3
    }
293
}
294