From 11fffd9a7b90592810226afa2372853e004e10b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 15:06:20 +0100 Subject: [PATCH] [FEATURE] Added option to get/set the filterEmptyParams option on IRoute classes (as requested by: #453). --- src/Pecee/SimpleRouter/Route/IRoute.php | 16 +++++++++++- src/Pecee/SimpleRouter/Route/Route.php | 33 ++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Pecee/SimpleRouter/Route/IRoute.php b/src/Pecee/SimpleRouter/Route/IRoute.php index e7fa944..1af8fe8 100644 --- a/src/Pecee/SimpleRouter/Route/IRoute.php +++ b/src/Pecee/SimpleRouter/Route/IRoute.php @@ -22,8 +22,8 @@ interface IRoute * * @param Request $request * @param Router $router - * @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException * @return string + * @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException */ public function renderRoute(Request $request, Router $router): ?string; @@ -206,4 +206,18 @@ interface IRoute */ public function setMiddlewares(array $middlewares): self; + /** + * If enabled parameters containing null-value will not be passed along to the callback. + * + * @param bool $enabled + * @return static $this + */ + public function setFilterEmptyParams(bool $enabled): self; + + /** + * Status if filtering of empty params is enabled or disabled + * @return bool + */ + public function getFilterEmptyParams(): bool; + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 54858a1..3e9370c 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -77,6 +77,7 @@ abstract class Route implements IRoute $router->debug('Executing callback'); /* When the callback is a function */ + return $router->getClassLoader()->loadClosure($callback, $parameters); } @@ -269,7 +270,7 @@ abstract class Route implements IRoute public function getMethod(): ?string { - if(\is_array($this->callback) === true && \count($this->callback) > 1) { + if (\is_array($this->callback) === true && \count($this->callback) > 1) { return $this->callback[1]; } @@ -284,12 +285,13 @@ abstract class Route implements IRoute public function getClass(): ?string { - if(\is_array($this->callback) === true && \count($this->callback) > 0) { + if (\is_array($this->callback) === true && \count($this->callback) > 0) { return $this->callback[0]; } if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) { $tmp = explode('@', $this->callback); + return $tmp[0]; } @@ -299,12 +301,14 @@ abstract class Route implements IRoute public function setMethod(string $method): IRoute { $this->callback = [$this->getClass(), $method]; + return $this; } public function setClass(string $class): IRoute { $this->callback = [$class, $this->getMethod()]; + return $this; } @@ -439,9 +443,9 @@ abstract class Route implements IRoute * Add regular expression parameter match. * Alias for LoadableRoute::where() * - * @see LoadableRoute::where() * @param array $options * @return static + * @see LoadableRoute::where() */ public function where(array $options) { @@ -489,9 +493,9 @@ abstract class Route implements IRoute /** * Add middleware class-name * - * @deprecated This method is deprecated and will be removed in the near future. * @param IMiddleware|string $middleware * @return static + * @deprecated This method is deprecated and will be removed in the near future. */ public function setMiddleware($middleware) { @@ -558,4 +562,25 @@ abstract class Route implements IRoute return $this->defaultParameterRegex; } + /** + * If enabled parameters containing null-value will not be passed along to the callback. + * + * @param bool $enabled + * @return static $this + */ + public function setFilterEmptyParams(bool $enabled): IRoute + { + $this->filterEmptyParams = $enabled; + return $this; + } + + /** + * Status if filtering of empty params is enabled or disabled + * @return bool + */ + public function getFilterEmptyParams(): bool + { + return $this->filterEmptyParams; + } + } \ No newline at end of file