|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Api\App\Route; |
| 6 | + |
| 7 | +use Fig\Http\Message\RequestMethodInterface; |
| 8 | +use Mezzio\Application; |
| 9 | + |
| 10 | +use function in_array; |
| 11 | +use function is_string; |
| 12 | + |
| 13 | +abstract class AbstractRoute implements RouteInterface |
| 14 | +{ |
| 15 | + /** @var class-string[] $excludedMiddlewares */ |
| 16 | + protected array $excludedMiddlewares = []; |
| 17 | + /** @var class-string[] $middlewares */ |
| 18 | + protected array $middlewares = []; |
| 19 | + /** @var non-empty-string $name */ |
| 20 | + protected string $name; |
| 21 | + /** @var non-empty-string $path */ |
| 22 | + protected string $path; |
| 23 | + /** @var non-empty-string $method */ |
| 24 | + protected string $method; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + ?string $path = null, |
| 28 | + null|array|string $middlewares = null, |
| 29 | + ?string $name = null, |
| 30 | + ?string $method = null |
| 31 | + ) { |
| 32 | + $path && $this->setPath($path); |
| 33 | + $middlewares && $this->setMiddlewares($middlewares); |
| 34 | + $name && $this->setName($name); |
| 35 | + $method && $this->setMethod($method); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param class-string $middleware |
| 40 | + */ |
| 41 | + public function addMiddleware(string $middleware): self |
| 42 | + { |
| 43 | + if (! in_array($middleware, $this->middlewares, true)) { |
| 44 | + $this->middlewares[] = $middleware; |
| 45 | + } |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function getMiddlewares(): array |
| 51 | + { |
| 52 | + return $this->middlewares; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param class-string[]|class-string $middlewares |
| 57 | + */ |
| 58 | + public function setMiddlewares(array|string $middlewares): self |
| 59 | + { |
| 60 | + if (is_string($middlewares)) { |
| 61 | + $middlewares = [$middlewares]; |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($middlewares as $middleware) { |
| 65 | + $this->addMiddleware($middleware); |
| 66 | + } |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + public function getName(): string |
| 72 | + { |
| 73 | + return $this->name; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param non-empty-string $name |
| 78 | + */ |
| 79 | + public function setName(string $name): self |
| 80 | + { |
| 81 | + $this->name = $name; |
| 82 | + |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + public function getPath(): string |
| 87 | + { |
| 88 | + return $this->path; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param non-empty-string $path |
| 93 | + */ |
| 94 | + public function setPath(string $path): self |
| 95 | + { |
| 96 | + $this->path = $path; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + public function getMethod(): string |
| 102 | + { |
| 103 | + return $this->method; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param non-empty-string $method |
| 108 | + */ |
| 109 | + public function setMethod(string $method): self |
| 110 | + { |
| 111 | + $this->method = $method; |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + public function register(Application $app): void |
| 117 | + { |
| 118 | + match ($this->method) { |
| 119 | + RequestMethodInterface::METHOD_DELETE => $app->delete($this->path, $this->middlewares, $this->name), |
| 120 | + RequestMethodInterface::METHOD_GET => $app->get($this->path, $this->middlewares, $this->name), |
| 121 | + RequestMethodInterface::METHOD_PATCH => $app->patch($this->path, $this->middlewares, $this->name), |
| 122 | + RequestMethodInterface::METHOD_POST => $app->post($this->path, $this->middlewares, $this->name), |
| 123 | + RequestMethodInterface::METHOD_PUT => $app->put($this->path, $this->middlewares, $this->name), |
| 124 | + default => null, |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param class-string[]|class-string $middlewares |
| 130 | + */ |
| 131 | + public function excludeMiddlewares(array|string $middlewares): self |
| 132 | + { |
| 133 | + if (is_string($middlewares)) { |
| 134 | + $middlewares = [$middlewares]; |
| 135 | + } |
| 136 | + |
| 137 | + foreach ($middlewares as $middleware) { |
| 138 | + if (in_array($middleware, $this->excludedMiddlewares, true)) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + $this->excludedMiddlewares[] = $middleware; |
| 142 | + } |
| 143 | + |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + public function getExcludedMiddlewares(): array |
| 148 | + { |
| 149 | + return $this->excludedMiddlewares; |
| 150 | + } |
| 151 | +} |
0 commit comments