Skip to content
Permalink
Browse files

Implement getParam()

Add getParam() which allows us to fully deprecate param().
  • Loading branch information...
markstory committed Nov 11, 2016
1 parent d39336f commit b92686dc4c2a50c9528777734fecb1a615d3d7e0
Showing with 38 additions and 6 deletions.
  1. +17 −5 src/Http/ServerRequest.php
  2. +21 −1 tests/TestCase/Network/RequestTest.php
@@ -1459,11 +1459,7 @@ public function param($name, ...$args)
return $this;
}
if (!isset($this->params[$name])) {
return Hash::get($this->params, $name, false);
}
return $this->params[$name];
return $this->getParam($name);
}
/**
@@ -1742,6 +1738,22 @@ public function withParam($name, $value)
return $copy;
}
/**
* Safely access the values in $this->params.
*
* @param string $name The name of the parameter to get.
* @param mixed $default The default value if $name is not set.
* @return mixed
*/
public function getParam($name, $default = false)
{
if (!isset($this->params[$name])) {
return Hash::get($this->params, $name, $default);
}
return $this->params[$name];
}
/**
* Return an instance with the specified request attribute.
*
@@ -2587,7 +2587,7 @@ public function testDataWritingFalsey()
*
* @dataProvider paramReadingDataProvider
*/
public function testParamReading($toRead, $expected)
public function testGetParam($toRead, $expected)
{
$request = new Request('/');
$request->addParams([
@@ -2603,6 +2603,26 @@ public function testParamReading($toRead, $expected)
'zero' => '0',
]);
$this->assertSame($expected, $request->param($toRead));
$this->assertSame($expected, $request->getParam($toRead));
}
/**
* Test getParam returning a default value.
*
* @return void
*/
public function testGetParamDefault()
{
$request = new Request([
'params' => [
'controller' => 'Articles',
'null' => null,
]
]);
$this->assertSame('Articles', $request->getParam('controller', 'default'));
$this->assertSame('default', $request->getParam('null', 'default'));
$this->assertNull($request->getParam('unset', null));
$this->assertFalse($request->getParam('unset'));
}
/**

0 comments on commit b92686d

Please sign in to comment.
You can’t perform that action at this time.