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