Skip to content

Commit af99282

Browse files
authored
Merge pull request #237 from fbourigault/query-string-boolean-encoding
Fix false boolean query string encoding
2 parents 0707e4a + 64638fb commit af99282

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Gitlab/HttpClient/Message/QueryStringBuilder.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class QueryStringBuilder
1515
public static function build($query)
1616
{
1717
if (!is_array($query)) {
18-
return rawurlencode($query);
18+
return static::rawurlencode($query);
1919
}
2020
return implode('&', array_map(function ($value, $key) {
2121
return static::encode($value, $key);
@@ -32,7 +32,7 @@ public static function build($query)
3232
private static function encode($query, $prefix)
3333
{
3434
if (!is_array($query)) {
35-
return rawurlencode($prefix).'='.rawurlencode($query);
35+
return static::rawurlencode($prefix).'='.static::rawurlencode($query);
3636
}
3737

3838
$isIndexedArray = static::isIndexedArray($query);
@@ -57,4 +57,20 @@ public static function isIndexedArray(array $query)
5757

5858
return array_keys($query) === range(0, count($query) - 1);
5959
}
60+
61+
/**
62+
* Encode a value like rawurlencode, but return "0" when false is given.
63+
*
64+
* @param mixed $value
65+
*
66+
* @return string
67+
*/
68+
private static function rawurlencode($value)
69+
{
70+
if ($value === false) {
71+
return '0';
72+
}
73+
74+
return rawurlencode($value);
75+
}
6076
}

test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public function queryStringProvider()
4040
'iids%5B0%5D=88&iids%5B2%5D=86'
4141
];
4242

43+
//Boolean encoding
44+
yield [
45+
['push_events' => false, 'merge_requests_events' => 1],
46+
'push_events=0&merge_requests_events=1'
47+
];
48+
4349
//A deeply nested array.
4450
yield [
4551
[

0 commit comments

Comments
 (0)