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 is_array; |
25
|
|
|
use function is_int; |
26
|
|
|
use function is_string; |
27
|
|
|
use function is_subclass_of; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Annotation for a route description |
31
|
|
|
* |
32
|
|
|
* @Annotation |
33
|
|
|
* |
34
|
|
|
* @Target({"CLASS"}) |
35
|
|
|
*/ |
36
|
|
|
final class Route implements RouteDescriptorInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null|string |
46
|
|
|
*/ |
47
|
|
|
private $host; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $path; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string[] |
56
|
|
|
*/ |
57
|
|
|
private $methods; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string[] |
61
|
|
|
*/ |
62
|
|
|
private $middlewares; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private $attributes; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $summary; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
private $description; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string[] |
81
|
|
|
*/ |
82
|
|
|
private $tags; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
private $priority; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Constructor of the annotation |
91
|
|
|
* @param array $params |
92
|
|
|
* @throws InvalidDescriptorArgumentException |
93
|
|
|
*/ |
94
|
136 |
|
public function __construct(array $params) |
95
|
|
|
{ |
96
|
136 |
|
$this->name = $this->extractNameFromParams($params); |
97
|
123 |
|
$this->host = $this->extractHostFromParams($params); |
|
|
|
|
98
|
113 |
|
$this->path = $this->extractPathFromParams($params); |
99
|
100 |
|
$this->methods = $this->extractMethodsFromParams($params); |
100
|
78 |
|
$this->middlewares = $this->extractMiddlewaresFromParams($params); |
101
|
56 |
|
$this->attributes = $this->extractAttributesFromParams($params); |
102
|
47 |
|
$this->summary = $this->extractSummaryFromParams($params); |
103
|
39 |
|
$this->description = $this->extractDescriptionFromParams($params); |
104
|
32 |
|
$this->tags = $this->extractTagsFromParams($params); |
105
|
16 |
|
$this->priority = $this->extractPriorityFromParams($params); |
106
|
7 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
7 |
|
public function getName() : string |
112
|
|
|
{ |
113
|
7 |
|
return $this->name; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
7 |
|
public function getHost() : ?string |
120
|
|
|
{ |
121
|
7 |
|
return $this->host; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
7 |
|
public function getPath() : string |
128
|
|
|
{ |
129
|
7 |
|
return $this->path; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
7 |
|
public function getMethods() : array |
136
|
|
|
{ |
137
|
7 |
|
return $this->methods; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
6 |
|
public function getMiddlewares() : array |
144
|
|
|
{ |
145
|
6 |
|
return $this->middlewares; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritDoc} |
150
|
|
|
*/ |
151
|
6 |
|
public function getAttributes() : array |
152
|
|
|
{ |
153
|
6 |
|
return $this->attributes; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
6 |
|
public function getSummary() : string |
160
|
|
|
{ |
161
|
6 |
|
return $this->summary; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
6 |
|
public function getDescription() : string |
168
|
|
|
{ |
169
|
6 |
|
return $this->description; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
6 |
|
public function getTags() : array |
176
|
|
|
{ |
177
|
6 |
|
return $this->tags; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
*/ |
183
|
4 |
|
public function getPriority() : int |
184
|
|
|
{ |
185
|
4 |
|
return $this->priority; |
186
|
|
|
} |
187
|
|
|
|
188
|
136 |
|
private function extractNameFromParams(array $params) : string |
189
|
|
|
{ |
190
|
136 |
|
$name = $params['name'] ?? ''; |
191
|
136 |
|
if ('' === $name || !is_string($name)) { |
192
|
13 |
|
throw new InvalidDescriptorArgumentException('@Route.name must contain a non-empty string.'); |
193
|
|
|
} |
194
|
|
|
|
195
|
123 |
|
return $name; |
196
|
|
|
} |
197
|
|
|
|
198
|
123 |
|
private function extractHostFromParams(array $params) : ?string |
199
|
|
|
{ |
200
|
123 |
|
$host = $params['host'] ?? null; |
201
|
123 |
|
if (isset($host) && ('' === $host || !is_string($host))) { |
202
|
10 |
|
throw new InvalidDescriptorArgumentException('@Route.host must contain a non-empty string.'); |
203
|
|
|
} |
204
|
|
|
|
205
|
113 |
|
return $host; |
206
|
|
|
} |
207
|
|
|
|
208
|
113 |
|
private function extractPathFromParams(array $params) : string |
209
|
|
|
{ |
210
|
113 |
|
$path = $params['path'] ?? ''; |
211
|
113 |
|
if ('' === $path || !is_string($path)) { |
212
|
13 |
|
throw new InvalidDescriptorArgumentException('@Route.path must contain a non-empty string.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
100 |
|
return $path; |
216
|
|
|
} |
217
|
|
|
|
218
|
100 |
|
private function extractMethodsFromParams(array $params) : array |
219
|
|
|
{ |
220
|
100 |
|
$methods = $params['methods'] ?? []; |
221
|
100 |
|
if ([] === $methods || !is_array($methods)) { |
222
|
13 |
|
throw new InvalidDescriptorArgumentException('@Route.methods must contain a non-empty array.'); |
223
|
|
|
} |
224
|
|
|
|
225
|
87 |
|
foreach ($methods as $value) { |
226
|
87 |
|
if (!is_string($value)) { |
227
|
9 |
|
throw new InvalidDescriptorArgumentException('@Route.methods must contain strings.'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
78 |
|
return $methods; |
232
|
|
|
} |
233
|
|
|
|
234
|
78 |
|
private function extractMiddlewaresFromParams(array $params) : array |
235
|
|
|
{ |
236
|
78 |
|
$middlewares = $params['middlewares'] ?? []; |
237
|
78 |
|
if (!is_array($middlewares)) { |
238
|
9 |
|
throw new InvalidDescriptorArgumentException('@Route.middlewares must contain an array.'); |
239
|
|
|
} |
240
|
|
|
|
241
|
69 |
|
foreach ($middlewares as $value) { |
242
|
17 |
|
if (!is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) { |
243
|
13 |
|
throw new InvalidDescriptorArgumentException( |
244
|
13 |
|
'@Route.middlewares must contain the class names of existing middlewares.' |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
56 |
|
return $middlewares; |
250
|
|
|
} |
251
|
|
|
|
252
|
56 |
|
private function extractAttributesFromParams(array $params) : array |
253
|
|
|
{ |
254
|
56 |
|
$attributes = $params['attributes'] ?? []; |
255
|
56 |
|
if (!is_array($attributes)) { |
256
|
9 |
|
throw new InvalidDescriptorArgumentException('@Route.attributes must contain an array.'); |
257
|
|
|
} |
258
|
|
|
|
259
|
47 |
|
return $attributes; |
260
|
|
|
} |
261
|
|
|
|
262
|
47 |
|
private function extractSummaryFromParams(array $params) : string |
263
|
|
|
{ |
264
|
47 |
|
$summary = $params['summary'] ?? ''; |
265
|
47 |
|
if (!is_string($summary)) { |
266
|
8 |
|
throw new InvalidDescriptorArgumentException('@Route.summary must contain a string.'); |
267
|
|
|
} |
268
|
|
|
|
269
|
39 |
|
return $summary; |
270
|
|
|
} |
271
|
|
|
|
272
|
39 |
|
private function extractDescriptionFromParams(array $params) : string |
273
|
|
|
{ |
274
|
39 |
|
$description = $params['description'] ?? ''; |
275
|
39 |
|
if (!is_string($description)) { |
276
|
7 |
|
throw new InvalidDescriptorArgumentException('@Route.description must contain a string.'); |
277
|
|
|
} |
278
|
|
|
|
279
|
32 |
|
return $description; |
280
|
|
|
} |
281
|
|
|
|
282
|
32 |
|
private function extractTagsFromParams(array $params) : array |
283
|
|
|
{ |
284
|
32 |
|
$tags = $params['tags'] ?? []; |
285
|
32 |
|
if (!is_array($tags)) { |
286
|
8 |
|
throw new InvalidDescriptorArgumentException('@Route.tags must contain an array.'); |
287
|
|
|
} |
288
|
|
|
|
289
|
24 |
|
foreach ($tags as $value) { |
290
|
11 |
|
if (!is_string($value)) { |
291
|
8 |
|
throw new InvalidDescriptorArgumentException('@Route.tags must contain strings.'); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
16 |
|
return $tags; |
296
|
|
|
} |
297
|
|
|
|
298
|
16 |
|
private function extractPriorityFromParams(array $params) : int |
299
|
|
|
{ |
300
|
16 |
|
$priority = $params['priority'] ?? 0; |
301
|
16 |
|
if (!is_int($priority)) { |
302
|
9 |
|
throw new InvalidDescriptorArgumentException('@Route.priority must contain an integer.'); |
303
|
|
|
} |
304
|
|
|
|
305
|
7 |
|
return $priority; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.