Skip to content

Commit

Permalink
Add ability to encode bools and ints
Browse files Browse the repository at this point in the history
Co-Authored-By: Simon Podlipsky <[email protected]>
  • Loading branch information
GrahamCampbell and simPod committed Jul 18, 2024
1 parent 9aed204 commit b1624a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`.

## `GuzzleHttp\Psr7\Query::build`

`public static function build(array $params, int|false $encoding = PHP_QUERY_RFC3986): string`
`public static function build(array $params, int|false $encoding = PHP_QUERY_RFC3986, bool $treatBoolsAsInts = true): string`

Build a query string from an array of key value pairs.

Expand Down
19 changes: 12 additions & 7 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public static function parse(string $str, $urlEncoding = true): array
* string. This function does not modify the provided keys when an array is
* encountered (like `http_build_query()` would).
*
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
* to encode using RFC3986, or PHP_QUERY_RFC1738
* to encode using RFC1738.
* @param array $params Query string parameters.
* @param int|false $encoding Set to false to not encode,
* PHP_QUERY_RFC3986 to encode using
* RFC3986, or PHP_QUERY_RFC1738 to
* encode using RFC1738.
* @param bool $treatBoolsAsInts Set to true to encode as 0/1, and
* false as false/true.
*/
public static function build(array $params, $encoding = PHP_QUERY_RFC3986): string
public static function build(array $params, $encoding = PHP_QUERY_RFC3986, bool $treatBoolsAsInts = true): string
{
if (!$params) {
return '';
Expand All @@ -86,20 +89,22 @@ public static function build(array $params, $encoding = PHP_QUERY_RFC3986): stri
throw new \InvalidArgumentException('Invalid type');
}

$castBool = $treatBooleansAsInts ? static function ($v) { return (int) $v; } : static function ($v) { return $v ? 'true' : 'false'; };

Check failure on line 92 in src/Query.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $treatBooleansAsInts

Check failure on line 92 in src/Query.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Query.php:92:21: UndefinedVariable: Cannot find referenced variable $treatBooleansAsInts (see https://psalm.dev/024)

Check failure on line 92 in src/Query.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $treatBooleansAsInts

Check failure on line 92 in src/Query.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Query.php:92:21: UndefinedVariable: Cannot find referenced variable $treatBooleansAsInts (see https://psalm.dev/024)

$qs = '';
foreach ($params as $k => $v) {
$k = $encoder((string) $k);
if (!is_array($v)) {
$qs .= $k;
$v = is_bool($v) ? (int) $v : $v;
$v = is_bool($v) ? $castBool($v) : $v;
if ($v !== null) {
$qs .= '='.$encoder((string) $v);
}
$qs .= '&';
} else {
foreach ($v as $vv) {
$qs .= $k;
$vv = is_bool($vv) ? (int) $vv : $vv;
$vv = is_bool($vv) ? $castBool($vv) : $vv;
if ($vv !== null) {
$qs .= '='.$encoder((string) $vv);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@ public function testBuildBooleans(): void
'bar' => [false, 'false'],
];
self::assertEquals('foo=1&foo=true&bar=0&bar=false', Psr7\Query::build($data, PHP_QUERY_RFC1738));

$data = [
'foo' => true,
'bar' => false,
];
self::assertEquals('foo=true&bar=false', Psr7\Query::build($data, PHP_QUERY_RFC3986, false));
}
}

0 comments on commit b1624a0

Please sign in to comment.