Skip to content

Commit

Permalink
Added support multiple query params.
Browse files Browse the repository at this point in the history
  • Loading branch information
romeOz committed Nov 22, 2015
1 parent 05977a1 commit 06182c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ Quick Start
```php
use rock\request\Request;

// example url: http://site.com/foo/?page=1
// example url: http://site.com/foo/?page=1&foo[bar]=test

// returns relative URL
(new Request)->getUrl(); // output: /foo/?page=1
(new Request)->getUrl(); // output: /foo/?page=1&foo[bar]=test

// returns host
(new Request)->getHost(); // output: site.com

// returns query params
(new Request)->rawGet(); // output: ['page' => 1, 'foo' => ['bar' => 'test']]

// returns query params as multiple
(new Request)->rawGet(['foo', 'bar']); // output: test

// alternative approach
(new Request)->rawGet('foo.bar'); // output: test
```

####Sanitize
Expand Down
7 changes: 3 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use rock\base\BaseException;
use rock\base\ObjectInterface;
use rock\base\ObjectTrait;
use rock\helpers\ArrayHelper;
use rock\helpers\Helper;
use rock\helpers\Instance;
use rock\log\Log;
Expand Down Expand Up @@ -534,9 +535,7 @@ public function setQueryParams($params)
*/
public function getQueryParam($name, $default = null)
{
$params = $this->getQueryParams();

return isset($params[$name]) ? $params[$name] : $default;
return ArrayHelper::getValue($this->getQueryParams(), $name, $default);
}

/**
Expand Down Expand Up @@ -1435,7 +1434,7 @@ protected function rawPostInternal($name = null, $default = null)
if (!isset($name)) {
return $params;
}
return isset($params[$name]) ? $params[$name] : $default;
return ArrayHelper::getValue($params, $name, $default);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ public function testNumeric()
$this->assertEquals(['baz' => 6], $result['baz']);
}

public function testMultipleParams()
{
$request = $this->getRequest([
'queryString' => 'foo[bar]=<b>test</b>'
]);
$this->assertSame(['bar'=>'<b>test</b>'],$request->rawGet('foo'));
$this->assertSame('<b>test</b>',$request->rawGet('foo.bar'));
$this->assertSame('<b>test</b>',$request->rawGet(['foo', 'bar']));
$this->assertSame('unknown',$request->rawGet(['foo', 'bar', 'baz'], 'unknown'));

// sanitize
$this->assertSame('test',$request->get(['foo', 'bar']));
}

public function testAsJson()
{
$_SERVER["CONTENT_TYPE"] = 'application/json; charset=UTF-8';
Expand Down

0 comments on commit 06182c3

Please sign in to comment.