[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

View File

@@ -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;
}

View File

@@ -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;
}
}