Permalink
Browse files

update Request::queryString() to merge query parameters of the same name

  • Loading branch information...
1 parent f6efe9d commit 0ec8f1d6d06db7e01c156aff9f8ddf742f60b054 @mikegreiling mikegreiling committed Mar 8, 2013
Showing with 24 additions and 11 deletions.
  1. +13 −11 net/http/Request.php
  2. +11 −0 tests/cases/net/http/RequestTest.php
View
@@ -121,21 +121,20 @@ public function body($data = null, $options = array()) {
*/
public function queryString($params = array(), $format = null) {
$result = array();
+ $query = array();
- foreach (array_filter(array($this->query, $params)) as $query) {
- if (is_string($query)) {
- $result[] = $query;
- continue;
- }
- $query = array_filter($query);
-
- if (!$format) {
- $result[] = http_build_query($query);
+ foreach (array_filter(array($this->query, $params)) as $querySet) {
+ if (is_string($querySet)) {
+ $result[] = $querySet;
continue;
}
+ $query = array_merge($query, $querySet);
+ }
+ $query = array_filter($query);
+
+ if ($format) {
$q = null;
-
- foreach ($params as $key => $value) {
+ foreach ($query as $key => $value) {
if (!is_array($value)) {
$q .= String::insert($format, array(
'key' => urlencode($key),
@@ -151,7 +150,10 @@ public function queryString($params = array(), $format = null) {
}
}
$result[] = substr($q, 0, -1);
+ } else {
+ $result[] = http_build_query($query);
}
+
$result = array_filter($result);
return $result ? "?" . join("&", $result) : null;
}
@@ -111,6 +111,17 @@ public function testQueryStringSetup() {
$this->assertEqual($expected, $result);
}
+ public function testQueryStringMerge() {
+ $expected = "?param=foo";
+ $this->request->query = array('param' => 'value');
+ $result = $this->request->queryString(array('param' => 'foo'));
+ $this->assertEqual($expected, $result);
+
+ $expected = "?param=foo&param2=bar";
+ $result = $this->request->queryString(array('param' => 'foo', 'param2' => 'bar'));
+ $this->assertEqual($expected, $result);
+ }
+
public function testToString() {
$expected = join("\r\n", array(
'GET / HTTP/1.1',

0 comments on commit 0ec8f1d

Please sign in to comment.