Skip to content

Commit

Permalink
Implement getParam()
Browse files Browse the repository at this point in the history
Add getParam() which allows us to fully deprecate param().
  • Loading branch information
markstory committed Nov 11, 2016
1 parent d39336f commit b92686d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/Http/ServerRequest.php
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
22 changes: 21 additions & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -2587,7 +2587,7 @@ public function testDataWritingFalsey()
*
* @dataProvider paramReadingDataProvider
*/
public function testParamReading($toRead, $expected)
public function testGetParam($toRead, $expected)
{
$request = new Request('/');
$request->addParams([
Expand All @@ -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'));
}

/**
Expand Down

0 comments on commit b92686d

Please sign in to comment.