Fixed parameter default-value being overwritten (issue: #347).

This commit is contained in:
Simon Sessingo
2018-01-06 03:27:27 +01:00
parent f2d106c649
commit 4d2b584936
2 changed files with 26 additions and 14 deletions
+12 -14
View File
@@ -33,7 +33,7 @@ abstract class Route implements IRoute
*
* @var bool
*/
protected $filterEmptyParams = false;
protected $filterEmptyParams = true;
/**
* Default regular expression used for parsing parameters.
@@ -86,12 +86,20 @@ abstract class Route implements IRoute
return null;
}
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
/* Render callback function */
if (is_callable($callback) === true) {
/* When the callback is a function */
return call_user_func_array($callback, $this->getParameters());
return call_user_func_array($callback, $parameters);
}
/* When the callback is a class + method */
@@ -108,16 +116,6 @@ abstract class Route implements IRoute
throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404);
}
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
return call_user_func_array([$class, $method], $parameters);
}
+14
View File
@@ -125,6 +125,20 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
TestRouter::debug('/my/custom-path', 'get');
}
public function testParameterDefaultValue() {
$defaultVariable = null;
TestRouter::get('/my/{path?}', function($path = 'working') use(&$defaultVariable) {
$defaultVariable = $path;
});
TestRouter::debug('/my/');
$this->assertEquals('working', $defaultVariable);
}
public function testDefaultParameterRegex()
{
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);