Skip to content

Commit 7304365

Browse files
author
Kyra Farrow
committed
[HttpClient] Allow arrays as query parameters
1 parent ae6ebfe commit 7304365

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

HttpClientTrait.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,20 @@ private static function mergeQueryString(?string $queryString, array $queryArray
481481
}
482482
}
483483

484-
foreach ($queryArray as $k => $v) {
485-
if (is_scalar($v)) {
486-
$queryArray[$k] = rawurlencode($k).'='.rawurlencode($v);
487-
} elseif (null === $v) {
488-
unset($queryArray[$k]);
489-
490-
if ($replace) {
484+
if ($replace) {
485+
foreach ($queryArray as $k => $v) {
486+
if (null === $v) {
491487
unset($query[$k]);
492488
}
493-
} else {
494-
throw new InvalidArgumentException(sprintf('Unsupported value for query parameter "%s": scalar or null expected, %s given.', $k, \gettype($v)));
489+
}
490+
}
491+
492+
$queryString = http_build_query($queryArray, '', '&', PHP_QUERY_RFC3986);
493+
$queryArray = [];
494+
495+
if ($queryString) {
496+
foreach (explode('&', $queryString) as $v) {
497+
$queryArray[rawurldecode(explode('=', $v, 2)[0])] = $v;
495498
}
496499
}
497500

Tests/HttpClientTraitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public function provideParseUrl()
141141
yield [[null, null, 'bar', '?a=1&c=c', null], 'bar?a=a&b=b', ['b' => null, 'c' => 'c', 'a' => 1]];
142142
yield [[null, null, 'bar', '?a=b+c&b=b', null], 'bar?a=b+c', ['b' => 'b']];
143143
yield [[null, null, 'bar', '?a=b%2B%20c', null], 'bar?a=b+c', ['a' => 'b+ c']];
144+
yield [[null, null, 'bar', '?a%5Bb%5D=c', null], 'bar', ['a' => ['b' => 'c']]];
145+
yield [[null, null, 'bar', '?a%5Bb%5Bc%5D=d', null], 'bar?a[b[c]=d', []];
146+
yield [[null, null, 'bar', '?a%5Bb%5D%5Bc%5D=dd', null], 'bar?a[b][c]=d&e[f]=g', ['a' => ['b' => ['c' => 'dd']], 'e[f]' => null]];
147+
yield [[null, null, 'bar', '?a=b&a%5Bb%20c%5D=d&e%3Df=%E2%9C%93', null], 'bar?a=b', ['a' => ['b c' => 'd'], 'e=f' => '']];
144148
}
145149

146150
/**

0 commit comments

Comments
 (0)