1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace inroutephp\inroute\Runtime; |
6
|
|
|
|
7
|
|
|
use inroutephp\inroute\Annotation\AnnotatedInterface; |
8
|
|
|
use inroutephp\inroute\Exception\LogicException; |
9
|
|
|
|
10
|
|
|
final class Route implements RouteInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AnnotatedInterface |
14
|
|
|
*/ |
15
|
|
|
private $annotations; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $routable = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $httpMethods = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $path = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $attributes = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $serviceId = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $serviceMethod = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string[] |
54
|
|
|
*/ |
55
|
|
|
private $middlewareServiceIds = []; |
56
|
|
|
|
57
|
|
|
public function __construct(string $serviceId, string $serviceMethod, AnnotatedInterface $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
|
|
|
private function setHttpMethods(array $httpMethods): void |
148
|
|
|
{ |
149
|
|
|
$this->httpMethods = array_change_key_case($httpMethods, CASE_UPPER); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getPath(): string |
153
|
|
|
{ |
154
|
|
|
return $this->path; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function withPath(string $path): RouteInterface |
158
|
|
|
{ |
159
|
|
|
$new = clone $this; |
160
|
|
|
$new->setPath($path); |
161
|
|
|
return $new; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function setPath(string $path): void |
165
|
|
|
{ |
166
|
|
|
$this->path = $path; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function hasAttribute(string $name): bool |
170
|
|
|
{ |
171
|
|
|
return isset($this->attributes[$name]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getAttribute(string $name) |
175
|
|
|
{ |
176
|
|
|
return $this->attributes[$name] ?? null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getAttributes(): array |
180
|
|
|
{ |
181
|
|
|
return $this->attributes; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function withAttribute(string $name, $value): RouteInterface |
185
|
|
|
{ |
186
|
|
|
$new = clone $this; |
187
|
|
|
$new->setAttribute($name, $value); |
188
|
|
|
return $new; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function setAttribute(string $name, $value): void |
192
|
|
|
{ |
193
|
|
|
$this->attributes[$name] = $value; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getServiceId(): string |
197
|
|
|
{ |
198
|
|
|
return $this->serviceId; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function withServiceId(string $serviceId): RouteInterface |
202
|
|
|
{ |
203
|
|
|
$new = clone $this; |
204
|
|
|
$new->setServiceId($serviceId); |
205
|
|
|
return $new; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function setServiceId(string $serviceId): void |
209
|
|
|
{ |
210
|
|
|
$this->serviceId = $serviceId; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getServiceMethod(): string |
214
|
|
|
{ |
215
|
|
|
return $this->serviceMethod; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function withServiceMethod(string $serviceMethod): RouteInterface |
219
|
|
|
{ |
220
|
|
|
$new = clone $this; |
221
|
|
|
$new->setServiceMethod($serviceMethod); |
222
|
|
|
return $new; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
private function setServiceMethod(string $serviceMethod): void |
226
|
|
|
{ |
227
|
|
|
$this->serviceMethod = $serviceMethod; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getMiddlewareServiceIds(): array |
231
|
|
|
{ |
232
|
|
|
return $this->middlewareServiceIds; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function withMiddleware(string $serviceId): RouteInterface |
236
|
|
|
{ |
237
|
|
|
$new = clone $this; |
238
|
|
|
$new->setMiddlewareServiceIds(array_merge($this->middlewareServiceIds, [$serviceId])); |
239
|
|
|
return $new; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
private function setMiddlewareServiceIds(array $serviceIds): void |
243
|
|
|
{ |
244
|
|
|
$this->middlewareServiceIds = $serviceIds; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function hasAnnotation(string $annotationId): bool |
248
|
|
|
{ |
249
|
|
|
return $this->getWrappedAnnotations()->hasAnnotation($annotationId); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getAnnotation(string $annotationId) |
253
|
|
|
{ |
254
|
|
|
return $this->getWrappedAnnotations()->getAnnotation($annotationId); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function getAnnotations(string $annotationId = ''): array |
258
|
|
|
{ |
259
|
|
|
return $this->getWrappedAnnotations()->getAnnotations($annotationId); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
private function getWrappedAnnotations(): AnnotatedInterface |
263
|
|
|
{ |
264
|
|
|
if (!isset($this->annotations)) { |
265
|
|
|
throw new LogicException('Unserialized route does not contain annotations'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $this->annotations; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|