diff --git a/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php b/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php index b916d29d5..03e16a326 100644 --- a/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php +++ b/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php @@ -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); @@ -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); @@ -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); + } } diff --git a/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php b/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php index db563cdce..4dc2e9c3e 100644 --- a/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php +++ b/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php @@ -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 [ [