summaryrefslogtreecommitdiff
path: root/src/Concerns
diff options
context:
space:
mode:
Diffstat (limited to 'src/Concerns')
-rw-r--r--src/Concerns/Strategy/ParsesStringParameters.php21
-rw-r--r--src/Concerns/Strategy/RunsMiddleware.php31
-rw-r--r--src/Concerns/Strategy/RunsRoute.php23
-rw-r--r--src/Concerns/Strategy/ThrowsNotFoundException.php17
4 files changed, 92 insertions, 0 deletions
diff --git a/src/Concerns/Strategy/ParsesStringParameters.php b/src/Concerns/Strategy/ParsesStringParameters.php
new file mode 100644
index 0000000..6dbc628
--- /dev/null
+++ b/src/Concerns/Strategy/ParsesStringParameters.php
@@ -0,0 +1,21 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Concerns\Strategy;
+
+use InvalidArgumentException;
+
+trait ParsesStringParameters
+{
+ public function parseParameters(array $parameters): array
+ {
+ foreach ($parameters as $key => $value) {
+ if (!is_string($value)) {
+ throw new InvalidArgumentException("Parameter {$key} is not a string.");
+ }
+ }
+
+ return $parameters;
+ }
+}
diff --git a/src/Concerns/Strategy/RunsMiddleware.php b/src/Concerns/Strategy/RunsMiddleware.php
new file mode 100644
index 0000000..4816a04
--- /dev/null
+++ b/src/Concerns/Strategy/RunsMiddleware.php
@@ -0,0 +1,31 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Concerns\Strategy;
+
+use Lightscale\Router\Contracts\Middleware;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+
+trait RunsMiddleware
+{
+ /**
+ * @param Middleware[] $middlewares
+ * @param callable(RequestInterface): ResponseInterface $handler
+ */
+ public function runMiddleware(
+ RequestInterface $request,
+ array $middlewares,
+ callable $handler,
+ ): ResponseInterface {
+ $handler = count($middlewares) > 0 ? array_reduce(
+ array_reverse($middlewares),
+ fn (callable $handler, Middleware $middleware) => fn (ServerRequestInterface $request) => $middleware->handle($request, $handler),
+ $handler
+ ) : $handler;
+
+ return $handler($request);
+ }
+}
diff --git a/src/Concerns/Strategy/RunsRoute.php b/src/Concerns/Strategy/RunsRoute.php
new file mode 100644
index 0000000..849ae80
--- /dev/null
+++ b/src/Concerns/Strategy/RunsRoute.php
@@ -0,0 +1,23 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Concerns\Strategy;
+
+use Lightscale\Router\Exceptions\InvalidResponseException;
+use Lightscale\Router\RouteCall;
+use Psr\Http\Message\ResponseInterface;
+
+trait RunsRoute
+{
+ public function runRoute(RouteCall $call): ResponseInterface
+ {
+ $result = ($call->route->getHandler())($call);
+
+ if ($result instanceof ResponseInterface) {
+ return $result;
+ }
+
+ throw new InvalidResponseException();
+ }
+}
diff --git a/src/Concerns/Strategy/ThrowsNotFoundException.php b/src/Concerns/Strategy/ThrowsNotFoundException.php
new file mode 100644
index 0000000..996bdb5
--- /dev/null
+++ b/src/Concerns/Strategy/ThrowsNotFoundException.php
@@ -0,0 +1,17 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Concerns\Strategy;
+
+use Lightscale\Router\Exceptions\NotFoundException;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseInterface;
+
+trait ThrowsNotFoundException
+{
+ public function notFound(RequestInterface $_): ResponseInterface
+ {
+ throw new NotFoundException();
+ }
+}