[FEATURE] Added option to get/set the filterEmptyParams option on IRoute classes (as requested by: #453).

This commit is contained in:
Simon Sessingø
2021-03-22 15:06:20 +01:00
parent 87e9c19edb
commit 11fffd9a7b
2 changed files with 44 additions and 5 deletions
+15 -1
View File
@@ -22,8 +22,8 @@ interface IRoute
* *
* @param Request $request * @param Request $request
* @param Router $router * @param Router $router
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
* @return string * @return string
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
*/ */
public function renderRoute(Request $request, Router $router): ?string; public function renderRoute(Request $request, Router $router): ?string;
@@ -206,4 +206,18 @@ interface IRoute
*/ */
public function setMiddlewares(array $middlewares): self; 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;
} }
+27 -2
View File
@@ -77,6 +77,7 @@ abstract class Route implements IRoute
$router->debug('Executing callback'); $router->debug('Executing callback');
/* When the callback is a function */ /* When the callback is a function */
return $router->getClassLoader()->loadClosure($callback, $parameters); return $router->getClassLoader()->loadClosure($callback, $parameters);
} }
@@ -290,6 +291,7 @@ abstract class Route implements IRoute
if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) { if (\is_string($this->callback) === true && strpos($this->callback, '@') !== false) {
$tmp = explode('@', $this->callback); $tmp = explode('@', $this->callback);
return $tmp[0]; return $tmp[0];
} }
@@ -299,12 +301,14 @@ abstract class Route implements IRoute
public function setMethod(string $method): IRoute public function setMethod(string $method): IRoute
{ {
$this->callback = [$this->getClass(), $method]; $this->callback = [$this->getClass(), $method];
return $this; return $this;
} }
public function setClass(string $class): IRoute public function setClass(string $class): IRoute
{ {
$this->callback = [$class, $this->getMethod()]; $this->callback = [$class, $this->getMethod()];
return $this; return $this;
} }
@@ -439,9 +443,9 @@ abstract class Route implements IRoute
* Add regular expression parameter match. * Add regular expression parameter match.
* Alias for LoadableRoute::where() * Alias for LoadableRoute::where()
* *
* @see LoadableRoute::where()
* @param array $options * @param array $options
* @return static * @return static
* @see LoadableRoute::where()
*/ */
public function where(array $options) public function where(array $options)
{ {
@@ -489,9 +493,9 @@ abstract class Route implements IRoute
/** /**
* Add middleware class-name * Add middleware class-name
* *
* @deprecated This method is deprecated and will be removed in the near future.
* @param IMiddleware|string $middleware * @param IMiddleware|string $middleware
* @return static * @return static
* @deprecated This method is deprecated and will be removed in the near future.
*/ */
public function setMiddleware($middleware) public function setMiddleware($middleware)
{ {
@@ -558,4 +562,25 @@ abstract class Route implements IRoute
return $this->defaultParameterRegex; 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;
}
} }