Skip to content

Commit

Permalink
Merge pull request #237 from fbourigault/query-string-boolean-encoding
Browse files Browse the repository at this point in the history
Fix false boolean query string encoding
  • Loading branch information
fbourigault authored Aug 26, 2017
2 parents 0707e4a + 64638fb commit af99282
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Gitlab/HttpClient/Message/QueryStringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class QueryStringBuilder
public static function build($query)
{
if (!is_array($query)) {
return rawurlencode($query);
return static::rawurlencode($query);
}
return implode('&', array_map(function ($value, $key) {
return static::encode($value, $key);
Expand All @@ -32,7 +32,7 @@ public static function build($query)
private static function encode($query, $prefix)
{
if (!is_array($query)) {
return rawurlencode($prefix).'='.rawurlencode($query);
return static::rawurlencode($prefix).'='.static::rawurlencode($query);
}

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

return array_keys($query) === range(0, count($query) - 1);
}

/**
* Encode a value like rawurlencode, but return "0" when false is given.
*
* @param mixed $value
*
* @return string
*/
private static function rawurlencode($value)
{
if ($value === false) {
return '0';
}

return rawurlencode($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function queryStringProvider()
'iids%5B0%5D=88&iids%5B2%5D=86'
];

//Boolean encoding
yield [
['push_events' => false, 'merge_requests_events' => 1],
'push_events=0&merge_requests_events=1'
];

//A deeply nested array.
yield [
[
Expand Down

0 comments on commit af99282

Please sign in to comment.