1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Annotation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException; |
19
|
|
|
use Sunrise\Http\Router\RouteDescriptorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Import functions |
23
|
|
|
*/ |
24
|
|
|
use function array_key_exists; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Annotation for a route description |
28
|
|
|
* |
29
|
|
|
* @Annotation |
30
|
|
|
* |
31
|
|
|
* @Target({"CLASS"}) |
32
|
|
|
*/ |
33
|
|
|
final class Route implements RouteDescriptorInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A route name |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* A route host |
45
|
|
|
* |
46
|
|
|
* @var null|string |
47
|
|
|
*/ |
48
|
|
|
private $host; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A route path |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $path; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* A route methods |
59
|
|
|
* |
60
|
|
|
* @var string[] |
61
|
|
|
*/ |
62
|
|
|
private $methods; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* A route middlewares |
66
|
|
|
* |
67
|
|
|
* @var string[] |
68
|
|
|
*/ |
69
|
|
|
private $middlewares; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* A route attributes |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $attributes; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* A route summary |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $summary; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* A route description |
87
|
|
|
* |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
private $description; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* A route tags |
94
|
|
|
* |
95
|
|
|
* @var string[] |
96
|
|
|
*/ |
97
|
|
|
private $tags; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* A route priority |
101
|
|
|
* |
102
|
|
|
* @var int |
103
|
|
|
*/ |
104
|
|
|
private $priority; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Constructor of the annotation |
108
|
137 |
|
* |
109
|
|
|
* @param array $params |
110
|
137 |
|
* |
111
|
124 |
|
* @throws InvalidDescriptorArgumentException |
112
|
114 |
|
*/ |
113
|
101 |
|
public function __construct(array $params) |
114
|
79 |
|
{ |
115
|
57 |
|
$this->name = $this->extractName($params); |
116
|
48 |
|
$this->host = $this->extractHost($params); |
117
|
40 |
|
$this->path = $this->extractPath($params); |
118
|
32 |
|
$this->methods = $this->extractMethods($params); |
119
|
16 |
|
$this->middlewares = $this->extractMiddlewares($params); |
120
|
7 |
|
$this->attributes = $this->extractAttributes($params); |
121
|
|
|
$this->summary = $this->extractSummary($params); |
122
|
|
|
$this->description = $this->extractDescription($params); |
123
|
|
|
$this->tags = $this->extractTags($params); |
124
|
|
|
$this->priority = $this->extractPriority($params); |
125
|
7 |
|
} |
126
|
|
|
|
127
|
7 |
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function getName() : string |
131
|
|
|
{ |
132
|
|
|
return $this->name; |
133
|
7 |
|
} |
134
|
|
|
|
135
|
7 |
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function getHost() : ?string |
139
|
|
|
{ |
140
|
|
|
return $this->host; |
141
|
7 |
|
} |
142
|
|
|
|
143
|
7 |
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getPath() : string |
147
|
|
|
{ |
148
|
|
|
return $this->path; |
149
|
7 |
|
} |
150
|
|
|
|
151
|
7 |
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function getMethods() : array |
155
|
|
|
{ |
156
|
|
|
return $this->methods; |
157
|
6 |
|
} |
158
|
|
|
|
159
|
6 |
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function getMiddlewares() : array |
163
|
|
|
{ |
164
|
|
|
return $this->middlewares; |
165
|
6 |
|
} |
166
|
|
|
|
167
|
6 |
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function getAttributes() : array |
171
|
|
|
{ |
172
|
|
|
return $this->attributes; |
173
|
6 |
|
} |
174
|
|
|
|
175
|
6 |
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function getSummary() : string |
179
|
|
|
{ |
180
|
|
|
return $this->summary; |
181
|
6 |
|
} |
182
|
|
|
|
183
|
6 |
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function getDescription() : string |
187
|
|
|
{ |
188
|
|
|
return $this->description; |
189
|
6 |
|
} |
190
|
|
|
|
191
|
6 |
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function getTags() : array |
195
|
|
|
{ |
196
|
|
|
return $this->tags; |
197
|
4 |
|
} |
198
|
|
|
|
199
|
4 |
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function getPriority() : int |
203
|
|
|
{ |
204
|
|
|
return $this->priority; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Extracts a route name from the given parameters |
209
|
137 |
|
* |
210
|
|
|
* @param array $params |
211
|
137 |
|
* |
212
|
|
|
* @return string |
213
|
137 |
|
* |
214
|
137 |
|
* @throws InvalidDescriptorArgumentException |
215
|
137 |
|
* If the given parameters contain an invalid route name. |
216
|
|
|
*/ |
217
|
|
|
private function extractName(array $params) : string |
218
|
124 |
|
{ |
219
|
|
|
$name = $params['name'] ?? ''; |
220
|
|
|
|
221
|
|
|
InvalidDescriptorArgumentException::throwIfEmptyString( |
222
|
|
|
$name, |
223
|
|
|
'@Route.name must contain a non-empty string.' |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
return $name; |
227
|
|
|
} |
228
|
124 |
|
|
229
|
|
|
/** |
230
|
124 |
|
* Extracts a route host from the given parameters |
231
|
|
|
* |
232
|
|
|
* @param array $params |
233
|
124 |
|
* |
234
|
113 |
|
* @return null|string |
235
|
|
|
* |
236
|
|
|
* @throws InvalidDescriptorArgumentException |
237
|
13 |
|
* If the given parameters contain an invalid route host. |
238
|
13 |
|
*/ |
239
|
13 |
|
private function extractHost(array $params) : ?string |
240
|
|
|
{ |
241
|
|
|
$host = $params['host'] ?? null; |
242
|
3 |
|
|
243
|
|
|
// isn't required parameter... |
244
|
|
|
if (null === $host) { |
245
|
|
|
return null; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
InvalidDescriptorArgumentException::throwIfEmptyString( |
249
|
|
|
$host, |
250
|
|
|
'@Route.host must contain a non-empty string.' |
251
|
|
|
); |
252
|
114 |
|
|
253
|
|
|
return $host; |
254
|
114 |
|
} |
255
|
|
|
|
256
|
114 |
|
/** |
257
|
114 |
|
* Extracts a route path from the given parameters |
258
|
114 |
|
* |
259
|
|
|
* @param array $params |
260
|
|
|
* |
261
|
101 |
|
* @return string |
262
|
|
|
* |
263
|
|
|
* @throws InvalidDescriptorArgumentException |
264
|
|
|
* If the given parameters contain an invalid route path. |
265
|
|
|
*/ |
266
|
|
|
private function extractPath(array $params) : string |
267
|
|
|
{ |
268
|
|
|
$path = $params['path'] ?? ''; |
269
|
|
|
|
270
|
|
|
InvalidDescriptorArgumentException::throwIfEmptyString( |
271
|
101 |
|
$path, |
272
|
|
|
'@Route.path must contain a non-empty string.' |
273
|
101 |
|
); |
274
|
|
|
|
275
|
101 |
|
return $path; |
276
|
101 |
|
} |
277
|
101 |
|
|
278
|
|
|
/** |
279
|
|
|
* Extracts a route method(s) from the given parameters |
280
|
88 |
|
* |
281
|
88 |
|
* @param array $params |
282
|
88 |
|
* |
283
|
88 |
|
* @return string[] |
284
|
|
|
* |
285
|
|
|
* @throws InvalidDescriptorArgumentException |
286
|
|
|
* If the given parameters contain an invalid route method(s). |
287
|
79 |
|
*/ |
288
|
|
|
private function extractMethods(array $params) : array |
289
|
|
|
{ |
290
|
|
|
if (array_key_exists('method', $params)) { |
291
|
|
|
$params['methods'][] = $params['method']; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$methods = $params['methods'] ?? []; |
295
|
|
|
|
296
|
|
|
InvalidDescriptorArgumentException::throwIfEmptyArray( |
297
|
79 |
|
$methods, |
298
|
|
|
'@Route.methods must contain a non-empty array.' |
299
|
79 |
|
); |
300
|
|
|
|
301
|
|
|
foreach ($methods as $method) { |
302
|
79 |
|
InvalidDescriptorArgumentException::throwIfEmptyString( |
303
|
53 |
|
$method, |
304
|
|
|
'@Route.methods must contain non-empty strings.' |
305
|
|
|
); |
306
|
26 |
|
} |
307
|
26 |
|
|
308
|
26 |
|
return $methods; |
309
|
|
|
} |
310
|
|
|
|
311
|
17 |
|
/** |
312
|
17 |
|
* Extracts a route middlewares from the given parameters |
313
|
17 |
|
* |
314
|
17 |
|
* @param array $params |
315
|
17 |
|
* |
316
|
|
|
* @return string[] |
317
|
|
|
* |
318
|
|
|
* @throws InvalidDescriptorArgumentException |
319
|
4 |
|
* If the given parameters contain an invalid route middlewares. |
320
|
|
|
*/ |
321
|
|
|
private function extractMiddlewares(array $params) : array |
322
|
|
|
{ |
323
|
|
|
$middlewares = $params['middlewares'] ?? null; |
324
|
|
|
|
325
|
|
|
// isn't required parameter... |
326
|
|
|
if (null === $middlewares) { |
327
|
|
|
return []; |
328
|
|
|
} |
329
|
57 |
|
|
330
|
|
|
InvalidDescriptorArgumentException::throwIfNotArray( |
331
|
57 |
|
$middlewares, |
332
|
|
|
'@Route.middlewares must contain an array.' |
333
|
|
|
); |
334
|
57 |
|
|
335
|
46 |
|
foreach ($middlewares as $middleware) { |
336
|
|
|
InvalidDescriptorArgumentException::throwIfNotImplemented( |
337
|
|
|
$middleware, |
338
|
12 |
|
MiddlewareInterface::class, |
339
|
12 |
|
'@Route.middlewares must contain middlewares.' |
340
|
12 |
|
); |
341
|
|
|
} |
342
|
|
|
|
343
|
3 |
|
return $middlewares; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Extracts a route attributes from the given parameters |
348
|
|
|
* |
349
|
|
|
* @param array $params |
350
|
|
|
* |
351
|
|
|
* @return array |
352
|
|
|
* |
353
|
48 |
|
* @throws InvalidDescriptorArgumentException |
354
|
|
|
* If the given parameters contain an invalid route attributes. |
355
|
48 |
|
*/ |
356
|
|
|
private function extractAttributes(array $params) : array |
357
|
|
|
{ |
358
|
48 |
|
$attributes = $params['attributes'] ?? null; |
359
|
39 |
|
|
360
|
|
|
// isn't required parameter... |
361
|
|
|
if (null === $attributes) { |
362
|
11 |
|
return []; |
363
|
11 |
|
} |
364
|
11 |
|
|
365
|
|
|
InvalidDescriptorArgumentException::throwIfNotArray( |
366
|
|
|
$attributes, |
367
|
3 |
|
'@Route.attributes must contain an array.' |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
return $attributes; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Extracts a route summary from the given parameters |
375
|
|
|
* |
376
|
|
|
* @param array $params |
377
|
40 |
|
* |
378
|
|
|
* @return string |
379
|
40 |
|
* |
380
|
|
|
* @throws InvalidDescriptorArgumentException |
381
|
|
|
* If the given parameters contain an invalid route summary. |
382
|
40 |
|
*/ |
383
|
31 |
|
private function extractSummary(array $params) : string |
384
|
|
|
{ |
385
|
|
|
$summary = $params['summary'] ?? null; |
386
|
11 |
|
|
387
|
11 |
|
// isn't required parameter... |
388
|
11 |
|
if (null === $summary) { |
389
|
|
|
return ''; |
390
|
|
|
} |
391
|
3 |
|
|
392
|
|
|
InvalidDescriptorArgumentException::throwIfNotString( |
393
|
|
|
$summary, |
394
|
|
|
'@Route.summary must contain a string.' |
395
|
|
|
); |
396
|
|
|
|
397
|
|
|
return $summary; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
32 |
|
* Extracts a route description from the given parameters |
402
|
|
|
* |
403
|
32 |
|
* @param array $params |
404
|
|
|
* |
405
|
|
|
* @return string |
406
|
32 |
|
* |
407
|
15 |
|
* @throws InvalidDescriptorArgumentException |
408
|
|
|
* If the given parameters contain an invalid route description. |
409
|
|
|
*/ |
410
|
19 |
|
private function extractDescription(array $params) : string |
411
|
19 |
|
{ |
412
|
19 |
|
$description = $params['description'] ?? null; |
413
|
|
|
|
414
|
|
|
// isn't required parameter... |
415
|
11 |
|
if (null === $description) { |
416
|
11 |
|
return ''; |
417
|
11 |
|
} |
418
|
11 |
|
|
419
|
|
|
InvalidDescriptorArgumentException::throwIfNotString( |
420
|
|
|
$description, |
421
|
|
|
'@Route.description must contain a string.' |
422
|
3 |
|
); |
423
|
|
|
|
424
|
|
|
return $description; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Extracts a route tags from the given parameters |
429
|
|
|
* |
430
|
|
|
* @param array $params |
431
|
|
|
* |
432
|
16 |
|
* @return string[] |
433
|
|
|
* |
434
|
16 |
|
* @throws InvalidDescriptorArgumentException |
435
|
|
|
* If the given parameters contain an invalid route tags. |
436
|
|
|
*/ |
437
|
16 |
|
private function extractTags(array $params) : array |
438
|
5 |
|
{ |
439
|
|
|
$tags = $params['tags'] ?? null; |
440
|
|
|
|
441
|
12 |
|
// isn't required parameter... |
442
|
12 |
|
if (null === $tags) { |
443
|
12 |
|
return []; |
444
|
|
|
} |
445
|
|
|
|
446
|
3 |
|
InvalidDescriptorArgumentException::throwIfNotArray( |
447
|
|
|
$tags, |
448
|
|
|
'@Route.tags must contain an array.' |
449
|
|
|
); |
450
|
|
|
|
451
|
|
|
foreach ($tags as $tag) { |
452
|
|
|
InvalidDescriptorArgumentException::throwIfEmptyString( |
453
|
|
|
$tag, |
454
|
|
|
'@Route.tags must contain non-empty strings.' |
455
|
|
|
); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
return $tags; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Extracts a route priority from the given parameters |
463
|
|
|
* |
464
|
|
|
* @param array $params |
465
|
|
|
* |
466
|
|
|
* @return int |
467
|
|
|
* |
468
|
|
|
* @throws InvalidDescriptorArgumentException |
469
|
|
|
* If the given parameters contain an invalid route priority. |
470
|
|
|
*/ |
471
|
|
|
private function extractPriority(array $params) : int |
472
|
|
|
{ |
473
|
|
|
$priority = $params['priority'] ?? null; |
474
|
|
|
|
475
|
|
|
// isn't required parameter... |
476
|
|
|
if (null === $priority) { |
477
|
|
|
return 0; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
InvalidDescriptorArgumentException::throwIfNotInteger( |
481
|
|
|
$priority, |
482
|
|
|
'@Route.priority must contain an integer.' |
483
|
|
|
); |
484
|
|
|
|
485
|
|
|
return $priority; |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|