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
|
|
|
* Annotation for a route description |
23
|
|
|
* |
24
|
|
|
* @Annotation |
25
|
|
|
* |
26
|
|
|
* @Target({"CLASS"}) |
27
|
|
|
*/ |
28
|
|
|
final class Route implements RouteDescriptorInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A route name |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A route host |
40
|
|
|
* |
41
|
|
|
* @var null|string |
42
|
|
|
*/ |
43
|
|
|
private $host; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* A route path |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $path; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* A route methods |
54
|
|
|
* |
55
|
|
|
* @var string[] |
56
|
|
|
*/ |
57
|
|
|
private $methods; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* A route middlewares |
61
|
|
|
* |
62
|
|
|
* @var string[] |
63
|
|
|
*/ |
64
|
|
|
private $middlewares; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* A route attributes |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private $attributes; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* A route summary |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $summary; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* A route description |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $description; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* A route tags |
89
|
|
|
* |
90
|
|
|
* @var string[] |
91
|
|
|
*/ |
92
|
|
|
private $tags; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* A route priority |
96
|
|
|
* |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
private $priority; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Constructor of the annotation |
103
|
|
|
* |
104
|
|
|
* @param array $params |
105
|
|
|
* |
106
|
|
|
* @throws InvalidDescriptorArgumentException |
107
|
|
|
*/ |
108
|
137 |
|
public function __construct(array $params) |
109
|
|
|
{ |
110
|
137 |
|
$this->name = $this->extractNameFromParams($params); |
111
|
124 |
|
$this->host = $this->extractHostFromParams($params); |
112
|
114 |
|
$this->path = $this->extractPathFromParams($params); |
113
|
101 |
|
$this->methods = $this->extractMethodsFromParams($params); |
114
|
79 |
|
$this->middlewares = $this->extractMiddlewaresFromParams($params); |
115
|
57 |
|
$this->attributes = $this->extractAttributesFromParams($params); |
116
|
48 |
|
$this->summary = $this->extractSummaryFromParams($params); |
117
|
40 |
|
$this->description = $this->extractDescriptionFromParams($params); |
118
|
32 |
|
$this->tags = $this->extractTagsFromParams($params); |
119
|
16 |
|
$this->priority = $this->extractPriorityFromParams($params); |
120
|
7 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
7 |
|
public function getName() : string |
126
|
|
|
{ |
127
|
7 |
|
return $this->name; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
7 |
|
public function getHost() : ?string |
134
|
|
|
{ |
135
|
7 |
|
return $this->host; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
7 |
|
public function getPath() : string |
142
|
|
|
{ |
143
|
7 |
|
return $this->path; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
7 |
|
public function getMethods() : array |
150
|
|
|
{ |
151
|
7 |
|
return $this->methods; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
6 |
|
public function getMiddlewares() : array |
158
|
|
|
{ |
159
|
6 |
|
return $this->middlewares; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
*/ |
165
|
6 |
|
public function getAttributes() : array |
166
|
|
|
{ |
167
|
6 |
|
return $this->attributes; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritDoc} |
172
|
|
|
*/ |
173
|
6 |
|
public function getSummary() : string |
174
|
|
|
{ |
175
|
6 |
|
return $this->summary; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritDoc} |
180
|
|
|
*/ |
181
|
6 |
|
public function getDescription() : string |
182
|
|
|
{ |
183
|
6 |
|
return $this->description; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritDoc} |
188
|
|
|
*/ |
189
|
6 |
|
public function getTags() : array |
190
|
|
|
{ |
191
|
6 |
|
return $this->tags; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritDoc} |
196
|
|
|
*/ |
197
|
4 |
|
public function getPriority() : int |
198
|
|
|
{ |
199
|
4 |
|
return $this->priority; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $params |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
* |
207
|
|
|
* @throws InvalidDescriptorArgumentException |
208
|
|
|
*/ |
209
|
137 |
|
private function extractNameFromParams(array $params) : string |
210
|
|
|
{ |
211
|
137 |
|
$name = $params['name'] ?? ''; |
212
|
|
|
|
213
|
137 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyString( |
214
|
137 |
|
$name, |
215
|
137 |
|
'@Route.name must contain a non-empty string.' |
216
|
|
|
); |
217
|
|
|
|
218
|
124 |
|
return $name; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param array $params |
223
|
|
|
* |
224
|
|
|
* @return null|string |
225
|
|
|
* |
226
|
|
|
* @throws InvalidDescriptorArgumentException |
227
|
|
|
*/ |
228
|
124 |
|
private function extractHostFromParams(array $params) : ?string |
229
|
|
|
{ |
230
|
124 |
|
$host = $params['host'] ?? null; |
231
|
|
|
|
232
|
|
|
// isn't required parameter... |
233
|
124 |
|
if (null === $host) { |
234
|
113 |
|
return null; |
235
|
|
|
} |
236
|
|
|
|
237
|
13 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyString( |
238
|
13 |
|
$host, |
239
|
13 |
|
'@Route.host must contain a non-empty string.' |
240
|
|
|
); |
241
|
|
|
|
242
|
3 |
|
return $host; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param array $params |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
* |
250
|
|
|
* @throws InvalidDescriptorArgumentException |
251
|
|
|
*/ |
252
|
114 |
|
private function extractPathFromParams(array $params) : string |
253
|
|
|
{ |
254
|
114 |
|
$path = $params['path'] ?? ''; |
255
|
|
|
|
256
|
114 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyString( |
257
|
114 |
|
$path, |
258
|
114 |
|
'@Route.path must contain a non-empty string.' |
259
|
|
|
); |
260
|
|
|
|
261
|
101 |
|
return $path; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param array $params |
266
|
|
|
* |
267
|
|
|
* @return string[] |
268
|
|
|
* |
269
|
|
|
* @throws InvalidDescriptorArgumentException |
270
|
|
|
*/ |
271
|
101 |
|
private function extractMethodsFromParams(array $params) : array |
272
|
|
|
{ |
273
|
101 |
|
$methods = $params['methods'] ?? []; |
274
|
|
|
|
275
|
101 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyArray( |
276
|
101 |
|
$methods, |
277
|
101 |
|
'@Route.methods must contain a non-empty array.' |
278
|
|
|
); |
279
|
|
|
|
280
|
88 |
|
foreach ($methods as $value) { |
281
|
88 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyString( |
282
|
88 |
|
$value, |
283
|
88 |
|
'@Route.methods must contain non-empty strings.' |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
79 |
|
return $methods; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param array $params |
292
|
|
|
* |
293
|
|
|
* @return string[] |
294
|
|
|
* |
295
|
|
|
* @throws InvalidDescriptorArgumentException |
296
|
|
|
*/ |
297
|
79 |
|
private function extractMiddlewaresFromParams(array $params) : array |
298
|
|
|
{ |
299
|
79 |
|
$middlewares = $params['middlewares'] ?? null; |
300
|
|
|
|
301
|
|
|
// isn't required parameter... |
302
|
79 |
|
if (null === $middlewares) { |
303
|
53 |
|
return []; |
304
|
|
|
} |
305
|
|
|
|
306
|
26 |
|
InvalidDescriptorArgumentException::assertIsArray( |
307
|
26 |
|
$middlewares, |
308
|
26 |
|
'@Route.middlewares must contain an array.' |
309
|
|
|
); |
310
|
|
|
|
311
|
17 |
|
foreach ($middlewares as $value) { |
312
|
17 |
|
InvalidDescriptorArgumentException::assertIsSubclassOf( |
313
|
17 |
|
$value, |
314
|
17 |
|
MiddlewareInterface::class, |
315
|
17 |
|
'@Route.middlewares must contain the class names of existing middlewares.' |
316
|
|
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
4 |
|
return $middlewares; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param array $params |
324
|
|
|
* |
325
|
|
|
* @return array |
326
|
|
|
* |
327
|
|
|
* @throws InvalidDescriptorArgumentException |
328
|
|
|
*/ |
329
|
57 |
|
private function extractAttributesFromParams(array $params) : array |
330
|
|
|
{ |
331
|
57 |
|
$attributes = $params['attributes'] ?? null; |
332
|
|
|
|
333
|
|
|
// isn't required parameter... |
334
|
57 |
|
if (null === $attributes) { |
335
|
46 |
|
return []; |
336
|
|
|
} |
337
|
|
|
|
338
|
12 |
|
InvalidDescriptorArgumentException::assertIsArray( |
339
|
12 |
|
$attributes, |
340
|
12 |
|
'@Route.attributes must contain an array.' |
341
|
|
|
); |
342
|
|
|
|
343
|
3 |
|
return $attributes; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param array $params |
348
|
|
|
* |
349
|
|
|
* @return string |
350
|
|
|
* |
351
|
|
|
* @throws InvalidDescriptorArgumentException |
352
|
|
|
*/ |
353
|
48 |
|
private function extractSummaryFromParams(array $params) : string |
354
|
|
|
{ |
355
|
48 |
|
$summary = $params['summary'] ?? null; |
356
|
|
|
|
357
|
|
|
// isn't required parameter... |
358
|
48 |
|
if (null === $summary) { |
359
|
39 |
|
return ''; |
360
|
|
|
} |
361
|
|
|
|
362
|
11 |
|
InvalidDescriptorArgumentException::assertIsString( |
363
|
11 |
|
$summary, |
364
|
11 |
|
'@Route.summary must contain a string.' |
365
|
|
|
); |
366
|
|
|
|
367
|
3 |
|
return $summary; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param array $params |
372
|
|
|
* |
373
|
|
|
* @return string |
374
|
|
|
* |
375
|
|
|
* @throws InvalidDescriptorArgumentException |
376
|
|
|
*/ |
377
|
40 |
|
private function extractDescriptionFromParams(array $params) : string |
378
|
|
|
{ |
379
|
40 |
|
$description = $params['description'] ?? null; |
380
|
|
|
|
381
|
|
|
// isn't required parameter... |
382
|
40 |
|
if (null === $description) { |
383
|
31 |
|
return ''; |
384
|
|
|
} |
385
|
|
|
|
386
|
11 |
|
InvalidDescriptorArgumentException::assertIsString( |
387
|
11 |
|
$description, |
388
|
11 |
|
'@Route.description must contain a string.' |
389
|
|
|
); |
390
|
|
|
|
391
|
3 |
|
return $description; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param array $params |
396
|
|
|
* |
397
|
|
|
* @return string[] |
398
|
|
|
* |
399
|
|
|
* @throws InvalidDescriptorArgumentException |
400
|
|
|
*/ |
401
|
32 |
|
private function extractTagsFromParams(array $params) : array |
402
|
|
|
{ |
403
|
32 |
|
$tags = $params['tags'] ?? null; |
404
|
|
|
|
405
|
|
|
// isn't required parameter... |
406
|
32 |
|
if (null === $tags) { |
407
|
15 |
|
return []; |
408
|
|
|
} |
409
|
|
|
|
410
|
19 |
|
InvalidDescriptorArgumentException::assertIsArray( |
411
|
19 |
|
$tags, |
412
|
19 |
|
'@Route.tags must contain an array.' |
413
|
|
|
); |
414
|
|
|
|
415
|
11 |
|
foreach ($tags as $value) { |
416
|
11 |
|
InvalidDescriptorArgumentException::assertIsNotEmptyString( |
417
|
11 |
|
$value, |
418
|
11 |
|
'@Route.tags must contain non-empty strings.' |
419
|
|
|
); |
420
|
|
|
} |
421
|
|
|
|
422
|
3 |
|
return $tags; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @param array $params |
427
|
|
|
* |
428
|
|
|
* @return int |
429
|
|
|
* |
430
|
|
|
* @throws InvalidDescriptorArgumentException |
431
|
|
|
*/ |
432
|
16 |
|
private function extractPriorityFromParams(array $params) : int |
433
|
|
|
{ |
434
|
16 |
|
$priority = $params['priority'] ?? null; |
435
|
|
|
|
436
|
|
|
// isn't required parameter... |
437
|
16 |
|
if (null === $priority) { |
438
|
5 |
|
return 0; |
439
|
|
|
} |
440
|
|
|
|
441
|
12 |
|
InvalidDescriptorArgumentException::assertIsInteger( |
442
|
12 |
|
$priority, |
443
|
12 |
|
'@Route.priority must contain an integer.' |
444
|
|
|
); |
445
|
|
|
|
446
|
3 |
|
return $priority; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|