Skip to content

Commit

Permalink
Implement getQuery() and deprecate query()
Browse files Browse the repository at this point in the history
Update tests, and implement getQuery().
  • Loading branch information
markstory committed Nov 11, 2016
1 parent b92686d commit cf2a2f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
18 changes: 14 additions & 4 deletions src/Http/ServerRequest.php
Expand Up @@ -1389,14 +1389,26 @@ protected function _parseAcceptWithQualifier($header)
*
* @param string|null $name Query string variable name or null to read all.
* @return string|array|null The value being read
* @deprecated 3.4.0 Use getQuery() instead.
*/
public function query($name = null)
{
if ($name === null) {
return $this->query;
}
return $this->getQuery($name);
}

return Hash::get($this->query, $name);
/**
* Read a specific query value or dotted path.
*
* @param string $name The name of the query arg or a dotted path.
* @param mixed $default The default value if the named parameter is not set.
* @return mixed Query data.
*/
public function getQuery($name, $default = null)
{
return Hash::get($this->query, $name, $default);
}

/**
Expand Down Expand Up @@ -1444,13 +1456,11 @@ public function data($name = null, ...$args)
/**
* Safely access the values in $this->params.
*
* As of 3.4.0, the setter mode of this method is *deprecated*.
* Use `withParam` instead.
*
* @param string $name The name of the parameter to get.
* @param mixed ...$args Value to set (deprecated).
* @return mixed|$this The value of the provided parameter. Will
* return false if the parameter doesn't exist or is falsey.
* @deprecated 3.4.0 Use getParam() and withParam() instead.
*/
public function param($name, ...$args)
{
Expand Down
50 changes: 25 additions & 25 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -2396,40 +2396,40 @@ public function testQuery()
];
$request = new Request($array);

$result = $request->query('foo');
$this->assertSame('bar', $result);

$result = $request->query('zero');
$this->assertSame('0', $result);

$result = $request->query('imaginary');
$this->assertNull($result);

$result = $request->query();
$this->assertSame($array['query'], $result);
$this->assertSame('bar', $request->query('foo'));
$this->assertSame('0', $request->query('zero'));
$this->assertNull($request->query('imaginary'));
$this->assertSame($array['query'], $request->query());
}

/**
* Test the query() method with arrays passed via $_GET
* Test the getQuery() method
*
* @return void
*/
public function testQueryWithArray()
public function testGetQuery()
{
$get['test'] = ['foo', 'bar'];

$request = new Request([
'query' => $get
]);

$result = $request->query('test');
$this->assertEquals(['foo', 'bar'], $result);
$array = [
'query' => [
'foo' => 'bar',
'zero' => '0',
'test' => [
'foo', 'bar'
]
]
];
$request = new Request($array);

$result = $request->query('test.1');
$this->assertEquals('bar', $result);
$this->assertSame('bar', $request->getQuery('foo'));
$this->assertSame('0', $request->getQuery('zero'));
$this->assertNull($request->getQuery('imaginary'));
$this->assertSame('default', $request->getQuery('imaginary', 'default'));
$this->assertFalse($request->getQuery('imaginary', false));

$result = $request->query('test.2');
$this->assertNull($result);
$this->assertSame(['foo', 'bar'], $request->getQuery('test'));
$this->assertSame('bar', $request->getQuery('test.1'));
$this->assertNull($request->getQuery('test.2'));
$this->assertSame('default', $request->getQuery('test.2', 'default'));
}

/**
Expand Down

0 comments on commit cf2a2f9

Please sign in to comment.