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