bavix /
router
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Bavix\Router; |
||||
| 4 | |||||
| 5 | class Resolver implements GroupResolution |
||||
| 6 | { |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * @var callable |
||||
| 10 | */ |
||||
| 11 | protected $collections; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * @var callable |
||||
| 15 | */ |
||||
| 16 | protected $patterns; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Resolver constructor. |
||||
| 20 | * |
||||
| 21 | * @param callable $patterns |
||||
| 22 | * @param callable $collections |
||||
| 23 | */ |
||||
| 24 | public function __construct(callable $patterns, callable $collections) |
||||
| 25 | { |
||||
| 26 | $this->collections = $collections; |
||||
| 27 | $this->patterns = $patterns; |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @param array $methods |
||||
| 32 | * @param string $path |
||||
| 33 | * @param null|string $name |
||||
| 34 | * |
||||
| 35 | * @return Pattern |
||||
| 36 | */ |
||||
| 37 | public function methods(array $methods, string $path, ?string $name): Pattern |
||||
| 38 | { |
||||
| 39 | return $this->pushPattern($this->pattern($path, $name))->methods($methods); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * @param Pattern $pattern |
||||
| 44 | * |
||||
| 45 | * @return Pattern |
||||
| 46 | */ |
||||
| 47 | protected function pushPattern(Pattern $pattern): Pattern |
||||
| 48 | { |
||||
| 49 | return \call_user_func($this->patterns, $pattern); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @param string $path |
||||
| 54 | * @param null|string $name |
||||
| 55 | * @return Pattern |
||||
| 56 | */ |
||||
| 57 | protected function pattern(string $path, ?string $name): Pattern |
||||
| 58 | { |
||||
| 59 | return new Pattern($path, $name); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE |
||||
| 64 | * |
||||
| 65 | * @param string $path |
||||
| 66 | * @param null|string $name |
||||
| 67 | * |
||||
| 68 | * @return Pattern |
||||
| 69 | */ |
||||
| 70 | public function any(string $path, ?string $name = null): Pattern |
||||
| 71 | { |
||||
| 72 | return $this->pushPattern($this->pattern($path, $name))->any(); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * @param string $path |
||||
| 77 | * @param null|string $name |
||||
| 78 | * |
||||
| 79 | * @return Pattern |
||||
| 80 | */ |
||||
| 81 | public function get(string $path, ?string $name = null): Pattern |
||||
| 82 | { |
||||
| 83 | return $this->pushPattern($this->pattern($path, $name))->get(); |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * @param string $path |
||||
| 88 | * @param null|string $name |
||||
| 89 | * |
||||
| 90 | * @return Pattern |
||||
| 91 | */ |
||||
| 92 | public function post(string $path, ?string $name = null): Pattern |
||||
| 93 | { |
||||
| 94 | return $this->pushPattern($this->pattern($path, $name))->post(); |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | /** |
||||
| 98 | * @param string $path |
||||
| 99 | * @param null|string $name |
||||
| 100 | * |
||||
| 101 | * @return Pattern |
||||
| 102 | */ |
||||
| 103 | public function put(string $path, ?string $name = null): Pattern |
||||
| 104 | { |
||||
| 105 | return $this->pushPattern($this->pattern($path, $name))->put(); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * @param string $path |
||||
| 110 | * @param null|string $name |
||||
| 111 | * |
||||
| 112 | * @return Pattern |
||||
| 113 | */ |
||||
| 114 | public function patch(string $path, ?string $name = null): Pattern |
||||
| 115 | { |
||||
| 116 | return $this->pushPattern($this->pattern($path, $name))->patch(); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @param string $path |
||||
| 121 | * @param null|string $name |
||||
| 122 | * |
||||
| 123 | * @return Pattern |
||||
| 124 | */ |
||||
| 125 | public function delete(string $path, ?string $name = null): Pattern |
||||
| 126 | { |
||||
| 127 | return $this->pushPattern($this->pattern($path, $name))->delete(); |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | /** |
||||
| 131 | * @param string $entityName |
||||
| 132 | * @param null|string $name |
||||
| 133 | * @return Pattern |
||||
| 134 | */ |
||||
| 135 | public function index(string $entityName, ?string $name = null): Pattern |
||||
| 136 | { |
||||
| 137 | return $this->pushPattern($this->_index($entityName, $name)); |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | /** |
||||
| 141 | * entityName -> /users |
||||
| 142 | * |
||||
| 143 | * GET users.index /users |
||||
| 144 | * |
||||
| 145 | * @param string $entityName |
||||
| 146 | * @param null|string $name |
||||
| 147 | * @return Pattern |
||||
| 148 | */ |
||||
| 149 | protected function _index(string $entityName, ?string $name = null): Pattern |
||||
| 150 | { |
||||
| 151 | return $this->pattern( |
||||
| 152 | $entityName, |
||||
| 153 | $this->name($name, 'index') |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 154 | )->get(); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * @param string $name |
||||
| 159 | * @param string $index |
||||
| 160 | * @return string |
||||
| 161 | */ |
||||
| 162 | protected function name(string $name, string $index): string |
||||
| 163 | { |
||||
| 164 | return $name . '.' . $index; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * @param string $entityName |
||||
| 169 | * @param null|string $name |
||||
| 170 | * @return Pattern |
||||
| 171 | */ |
||||
| 172 | public function create(string $entityName, ?string $name = null): Pattern |
||||
| 173 | { |
||||
| 174 | return $this->pushPattern($this->_create($entityName, $name)); |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | /** |
||||
| 178 | * entityName -> /users |
||||
| 179 | * |
||||
| 180 | * GET users.create /users/create |
||||
| 181 | * |
||||
| 182 | * @param string $entityName |
||||
| 183 | * @param null|string $name |
||||
| 184 | * @return Pattern |
||||
| 185 | */ |
||||
| 186 | protected function _create(string $entityName, ?string $name = null): Pattern |
||||
| 187 | { |
||||
| 188 | return $this->pattern( |
||||
| 189 | $this->action($entityName, 'create'), |
||||
| 190 | $this->name($name, 'create') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 191 | )->get(); |
||||
| 192 | } |
||||
| 193 | |||||
| 194 | /** |
||||
| 195 | * @param string $entityName |
||||
| 196 | * @param string $action |
||||
| 197 | * @return string |
||||
| 198 | */ |
||||
| 199 | protected function action(string $entityName, string $action): string |
||||
| 200 | { |
||||
| 201 | return $entityName . '/' . $action; |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * @param string $entityName |
||||
| 206 | * @param null|string $name |
||||
| 207 | * @return Pattern |
||||
| 208 | */ |
||||
| 209 | public function store(string $entityName, ?string $name = null): Pattern |
||||
| 210 | { |
||||
| 211 | return $this->pushPattern($this->_store($entityName, $name)); |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * entityName -> /users |
||||
| 216 | * |
||||
| 217 | * POST users.store /users |
||||
| 218 | * |
||||
| 219 | * @param string $entityName |
||||
| 220 | * @param null|string $name |
||||
| 221 | * @return Pattern |
||||
| 222 | */ |
||||
| 223 | protected function _store(string $entityName, ?string $name = null): Pattern |
||||
| 224 | { |
||||
| 225 | return $this->pattern( |
||||
| 226 | $entityName, |
||||
| 227 | $this->name($name, 'store') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 228 | )->post(); |
||||
| 229 | } |
||||
| 230 | |||||
| 231 | /** |
||||
| 232 | * @param string $entityName |
||||
| 233 | * @param null|string $name |
||||
| 234 | * @param null|string $id |
||||
| 235 | * @return Pattern |
||||
| 236 | */ |
||||
| 237 | public function show(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 238 | { |
||||
| 239 | return $this->pushPattern($this->_show($entityName, $name, $id)); |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | /** |
||||
| 243 | * entityName -> /users |
||||
| 244 | * |
||||
| 245 | * GET users.show /users/{id} |
||||
| 246 | * |
||||
| 247 | * @param string $entityName |
||||
| 248 | * @param null|string $name |
||||
| 249 | * @param null|string $id |
||||
| 250 | * @return Pattern |
||||
| 251 | */ |
||||
| 252 | protected function _show(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 253 | { |
||||
| 254 | return $this->pattern( |
||||
| 255 | $this->id($entityName, $id), |
||||
|
0 ignored issues
–
show
It seems like
$id can also be of type null; however, parameter $id of Bavix\Router\Resolver::id() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 256 | $this->name($name, 'show') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 257 | )->get(); |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | /** |
||||
| 261 | * @param string $entityName |
||||
| 262 | * @param string $id |
||||
| 263 | * @return string |
||||
| 264 | */ |
||||
| 265 | protected function id(string $entityName, string $id): string |
||||
| 266 | { |
||||
| 267 | return $this->action($entityName, '<' . $id . '>'); |
||||
| 268 | } |
||||
| 269 | |||||
| 270 | /** |
||||
| 271 | * @param string $entityName |
||||
| 272 | * @param null|string $name |
||||
| 273 | * @param null|string $id |
||||
| 274 | * @return Pattern |
||||
| 275 | */ |
||||
| 276 | public function edit(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 277 | { |
||||
| 278 | return $this->pushPattern($this->_edit($entityName, $name, $id)); |
||||
| 279 | } |
||||
| 280 | |||||
| 281 | /** |
||||
| 282 | * entityName -> /users |
||||
| 283 | * |
||||
| 284 | * GET users.edit /users/{id}/edit |
||||
| 285 | * |
||||
| 286 | * @param string $entityName |
||||
| 287 | * @param null|string $name |
||||
| 288 | * @param null|string $id |
||||
| 289 | * @return Pattern |
||||
| 290 | */ |
||||
| 291 | protected function _edit(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 292 | { |
||||
| 293 | return $this->pattern( |
||||
| 294 | $this->idAction($entityName, $id, 'edit'), |
||||
|
0 ignored issues
–
show
It seems like
$id can also be of type null; however, parameter $id of Bavix\Router\Resolver::idAction() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 295 | $this->name($name, 'edit') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 296 | )->get(); |
||||
| 297 | } |
||||
| 298 | |||||
| 299 | /** |
||||
| 300 | * @param string $entityName |
||||
| 301 | * @param string $id |
||||
| 302 | * @param string $action |
||||
| 303 | * @return string |
||||
| 304 | */ |
||||
| 305 | protected function idAction(string $entityName, string $id, string $action): string |
||||
| 306 | { |
||||
| 307 | return $this->action($this->id($entityName, $id), $action); |
||||
| 308 | } |
||||
| 309 | |||||
| 310 | /** |
||||
| 311 | * @param string $entityName |
||||
| 312 | * @param null|string $name |
||||
| 313 | * @param null|string $id |
||||
| 314 | * @return Pattern |
||||
| 315 | */ |
||||
| 316 | public function update(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 317 | { |
||||
| 318 | return $this->pushPattern($this->_update($entityName, $name, $id)); |
||||
| 319 | } |
||||
| 320 | |||||
| 321 | /** |
||||
| 322 | * entityName -> /users |
||||
| 323 | * |
||||
| 324 | * PUT/PATCH users.update /users/{id}/edit |
||||
| 325 | * |
||||
| 326 | * @param string $entityName |
||||
| 327 | * @param null|string $name |
||||
| 328 | * @param null|string $id |
||||
| 329 | * @return Pattern |
||||
| 330 | */ |
||||
| 331 | protected function _update(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 332 | { |
||||
| 333 | return $this->pattern( |
||||
| 334 | $this->idAction($entityName, $id, 'edit'), |
||||
|
0 ignored issues
–
show
It seems like
$id can also be of type null; however, parameter $id of Bavix\Router\Resolver::idAction() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 335 | $this->name($name, 'update') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 336 | )->setMethods(['PUT', 'PATCH']); |
||||
| 337 | } |
||||
| 338 | |||||
| 339 | /** |
||||
| 340 | * @param string $entityName |
||||
| 341 | * @param null|string $name |
||||
| 342 | * @param null|string $id |
||||
| 343 | * @return Pattern |
||||
| 344 | */ |
||||
| 345 | public function destroy(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 346 | { |
||||
| 347 | return $this->pushPattern($this->_destroy($entityName, $name, $id)); |
||||
| 348 | } |
||||
| 349 | |||||
| 350 | /** |
||||
| 351 | * entityName -> /users |
||||
| 352 | * |
||||
| 353 | * DELETE users.destroy /users/{id} |
||||
| 354 | * |
||||
| 355 | * @param string $entityName |
||||
| 356 | * @param null|string $name |
||||
| 357 | * @param null|string $id |
||||
| 358 | * @return Pattern |
||||
| 359 | */ |
||||
| 360 | protected function _destroy(string $entityName, ?string $name = null, ?string $id = null): Pattern |
||||
| 361 | { |
||||
| 362 | return $this->pattern( |
||||
| 363 | $this->id($entityName, $id), |
||||
|
0 ignored issues
–
show
It seems like
$id can also be of type null; however, parameter $id of Bavix\Router\Resolver::id() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 364 | $this->name($name, 'destroy') |
||||
|
0 ignored issues
–
show
It seems like
$name can also be of type null; however, parameter $name of Bavix\Router\Resolver::name() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 365 | )->delete(); |
||||
| 366 | } |
||||
| 367 | |||||
| 368 | /** |
||||
| 369 | * @param string $entityName |
||||
| 370 | * @param null|string $name |
||||
| 371 | * @param null|string $id |
||||
| 372 | * |
||||
| 373 | * @return ResourceCollection |
||||
| 374 | */ |
||||
| 375 | public function apiResource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection |
||||
| 376 | { |
||||
| 377 | return $this->resource($entityName, $name, $id)->only([ |
||||
| 378 | 'index', 'store', 'show', 'update', 'destroy' |
||||
| 379 | ]); |
||||
| 380 | } |
||||
| 381 | |||||
| 382 | /** |
||||
| 383 | * entityName -> /users |
||||
| 384 | * |
||||
| 385 | * GET users.index /users |
||||
| 386 | * GET users.create /users/create |
||||
| 387 | * POST users.store /users |
||||
| 388 | * GET users.show /users/{id} |
||||
| 389 | * GET users.edit /users/{id}/edit |
||||
| 390 | * PUT/PATCH users.update /users/{id}/edit |
||||
| 391 | * DELETE users.destroy /users/{id} |
||||
| 392 | * |
||||
| 393 | * @param string $entityName |
||||
| 394 | * @param null|string $name |
||||
| 395 | * @param null|string $id |
||||
| 396 | * |
||||
| 397 | * @return ResourceCollection |
||||
| 398 | */ |
||||
| 399 | public function resource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection |
||||
| 400 | { |
||||
| 401 | $entityName = \rtrim($entityName, '/'); |
||||
| 402 | $name = $name ?: \ltrim($entityName, '/'); |
||||
| 403 | $id = $id ?: $name; |
||||
| 404 | |||||
| 405 | return $this->pushCollection(new ResourceCollection([ |
||||
| 406 | 'index' => $this->_index($entityName, $name), |
||||
| 407 | 'create' => $this->_create($entityName, $name), |
||||
| 408 | 'store' => $this->_store($entityName, $name), |
||||
| 409 | 'show' => $this->_show($entityName, $name, $id), |
||||
| 410 | 'edit' => $this->_edit($entityName, $name, $id), |
||||
| 411 | 'update' => $this->_update($entityName, $name, $id), |
||||
| 412 | 'destroy' => $this->_destroy($entityName, $name, $id) |
||||
| 413 | ])); |
||||
| 414 | } |
||||
| 415 | |||||
| 416 | /** |
||||
| 417 | * @param ResourceCollection $collection |
||||
| 418 | * |
||||
| 419 | * @return ResourceCollection |
||||
| 420 | */ |
||||
| 421 | protected function pushCollection(ResourceCollection $collection): ResourceCollection |
||||
| 422 | { |
||||
| 423 | return \call_user_func($this->collections, $collection); |
||||
| 424 | } |
||||
| 425 | |||||
| 426 | } |
||||
| 427 |