|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace inroutephp\inroute\Runtime; |
|
6
|
|
|
|
|
7
|
|
|
final class Route implements RouteInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array<string, mixed> |
|
11
|
|
|
*/ |
|
12
|
|
|
private $annotations; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $name = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $routable = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array<string> |
|
26
|
|
|
*/ |
|
27
|
|
|
private $httpMethods = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $path = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array<string, mixed> |
|
36
|
|
|
*/ |
|
37
|
|
|
private $attributes = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $serviceId = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $serviceMethod = ''; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array<string> |
|
51
|
|
|
*/ |
|
52
|
|
|
private $middlewareServiceIds = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array<object> $annotations |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(string $serviceId, string $serviceMethod, array $annotations) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($serviceId) { |
|
60
|
|
|
$this->name = $serviceId; |
|
61
|
|
|
$this->serviceId = $serviceId; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($serviceMethod) { |
|
65
|
|
|
$this->name .= ":$serviceMethod"; |
|
66
|
|
|
$this->serviceMethod = $serviceMethod; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->annotations = $annotations; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function __sleep(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
'name', |
|
76
|
|
|
'routable', |
|
77
|
|
|
'httpMethods', |
|
78
|
|
|
'path', |
|
79
|
|
|
'attributes', |
|
80
|
|
|
'serviceId', |
|
81
|
|
|
'serviceMethod', |
|
82
|
|
|
'middlewareServiceIds', |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getName(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->name; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function withName(string $name): RouteInterface |
|
92
|
|
|
{ |
|
93
|
|
|
$new = clone $this; |
|
94
|
|
|
$new->setName($name); |
|
95
|
|
|
return $new; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function setName(string $name): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->name = $name; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function isRoutable(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->routable; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function withRoutable(bool $routable): RouteInterface |
|
109
|
|
|
{ |
|
110
|
|
|
$new = clone $this; |
|
111
|
|
|
$new->setRoutable($routable); |
|
112
|
|
|
return $new; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function setRoutable(bool $routable): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->routable = $routable; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getHttpMethods(): array |
|
121
|
|
|
{ |
|
122
|
|
|
return array_values($this->httpMethods); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function withHttpMethod(string $httpMethod): RouteInterface |
|
126
|
|
|
{ |
|
127
|
|
|
$new = clone $this; |
|
128
|
|
|
$new->setHttpMethods(array_merge($this->httpMethods, [strtoupper($httpMethod)])); |
|
129
|
|
|
return $new; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function withoutHttpMethod(string $httpMethod): RouteInterface |
|
133
|
|
|
{ |
|
134
|
|
|
$new = clone $this; |
|
135
|
|
|
|
|
136
|
|
|
$id = array_search(strtoupper($httpMethod), $this->httpMethods); |
|
137
|
|
|
|
|
138
|
|
|
if (false !== $id) { |
|
139
|
|
|
$newHttpMethods = $this->httpMethods; |
|
140
|
|
|
unset($newHttpMethods[$id]); |
|
141
|
|
|
$new->setHttpMethods($newHttpMethods); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $new; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array<string> $httpMethods |
|
149
|
|
|
*/ |
|
150
|
|
|
private function setHttpMethods(array $httpMethods): void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->httpMethods = array_change_key_case($httpMethods, CASE_UPPER); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getPath(): string |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->path; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function withPath(string $path): RouteInterface |
|
161
|
|
|
{ |
|
162
|
|
|
$new = clone $this; |
|
163
|
|
|
$new->setPath($path); |
|
164
|
|
|
return $new; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
private function setPath(string $path): void |
|
168
|
|
|
{ |
|
169
|
|
|
$this->path = $path; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function hasAttribute(string $name): bool |
|
173
|
|
|
{ |
|
174
|
|
|
return isset($this->attributes[$name]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function getAttribute(string $name) |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->attributes[$name] ?? null; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getAttributes(): array |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->attributes; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function withAttribute(string $name, $value): RouteInterface |
|
188
|
|
|
{ |
|
189
|
|
|
$new = clone $this; |
|
190
|
|
|
$new->setAttribute($name, $value); |
|
191
|
|
|
return $new; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param mixed $value |
|
196
|
|
|
*/ |
|
197
|
|
|
private function setAttribute(string $name, $value): void |
|
198
|
|
|
{ |
|
199
|
|
|
$this->attributes[$name] = $value; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function getServiceId(): string |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->serviceId; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function withServiceId(string $serviceId): RouteInterface |
|
208
|
|
|
{ |
|
209
|
|
|
$new = clone $this; |
|
210
|
|
|
$new->setServiceId($serviceId); |
|
211
|
|
|
return $new; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
private function setServiceId(string $serviceId): void |
|
215
|
|
|
{ |
|
216
|
|
|
$this->serviceId = $serviceId; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function getServiceMethod(): string |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->serviceMethod; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function withServiceMethod(string $serviceMethod): RouteInterface |
|
225
|
|
|
{ |
|
226
|
|
|
$new = clone $this; |
|
227
|
|
|
$new->setServiceMethod($serviceMethod); |
|
228
|
|
|
return $new; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
private function setServiceMethod(string $serviceMethod): void |
|
232
|
|
|
{ |
|
233
|
|
|
$this->serviceMethod = $serviceMethod; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function getMiddlewareServiceIds(): array |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->middlewareServiceIds; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function withMiddleware(string $serviceId): RouteInterface |
|
242
|
|
|
{ |
|
243
|
|
|
$new = clone $this; |
|
244
|
|
|
$new->setMiddlewareServiceIds(array_merge($this->middlewareServiceIds, [$serviceId])); |
|
245
|
|
|
return $new; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param array<string> $serviceIds |
|
250
|
|
|
*/ |
|
251
|
|
|
private function setMiddlewareServiceIds(array $serviceIds): void |
|
252
|
|
|
{ |
|
253
|
|
|
$this->middlewareServiceIds = $serviceIds; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function hasAnnotation(string $annotationId): bool |
|
257
|
|
|
{ |
|
258
|
|
|
foreach ($this->annotations as $annotation) { |
|
259
|
|
|
if ($annotation instanceof $annotationId) { |
|
260
|
|
|
return true; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return false; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function getAnnotation(string $annotationId): ?object |
|
268
|
|
|
{ |
|
269
|
|
|
foreach ($this->annotations as $annotation) { |
|
270
|
|
|
if ($annotation instanceof $annotationId) { |
|
271
|
|
|
return $annotation; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return null; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function getAnnotations(string $annotationId = ''): array |
|
279
|
|
|
{ |
|
280
|
|
|
if (!$annotationId) { |
|
281
|
|
|
return $this->annotations; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$annotations = []; |
|
285
|
|
|
|
|
286
|
|
|
foreach ($this->annotations as $annotation) { |
|
287
|
|
|
if ($annotation instanceof $annotationId) { |
|
288
|
|
|
$annotations[] = $annotation; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $annotations; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|