| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | final class Query |
| 6 | 7 | { |
| @@ -13,12 +14,10 @@ | ||
| 13 | 14 | * will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`. |
| 14 | 15 | * |
| 15 | 16 | * @param string $str Query string to parse |
| 16 | 17 | * @param int|bool $urlEncoding How the query string is encoded |
| 17 | - * | |
| 18 | - * @return array | |
| 19 | 18 | */ |
| 20 | - public static function parse($str, $urlEncoding = \true) | |
| 19 | + public static function parse(string $str, $urlEncoding = \true) : array | |
| 21 | 20 | { |
| 22 | 21 | $result = []; |
| 23 | 22 | if ($str === '') { |
| 24 | 23 | return $result; |
| @@ -24,9 +23,9 @@ | ||
| 24 | 23 | return $result; |
| 25 | 24 | } |
| 26 | 25 | if ($urlEncoding === \true) { |
| 27 | 26 | $decoder = function ($value) { |
| 28 | - return \rawurldecode(\str_replace('+', ' ', $value)); | |
| 27 | + return \rawurldecode(\str_replace('+', ' ', (string) $value)); | |
| 29 | 28 | }; |
| 30 | 29 | } elseif ($urlEncoding === \PHP_QUERY_RFC3986) { |
| 31 | 30 | $decoder = 'rawurldecode'; |
| 32 | 31 | } elseif ($urlEncoding === \PHP_QUERY_RFC1738) { |
| @@ -39,9 +38,9 @@ | ||
| 39 | 38 | foreach (\explode('&', $str) as $kvp) { |
| 40 | 39 | $parts = \explode('=', $kvp, 2); |
| 41 | 40 | $key = $decoder($parts[0]); |
| 42 | 41 | $value = isset($parts[1]) ? $decoder($parts[1]) : null; |
| 43 | - if (!isset($result[$key])) { | |
| 42 | + if (!\array_key_exists($key, $result)) { | |
| 44 | 43 | $result[$key] = $value; |
| 45 | 44 | } else { |
| 46 | 45 | if (!\is_array($result[$key])) { |
| 47 | 46 | $result[$key] = [$result[$key]]; |
| @@ -57,22 +56,23 @@ | ||
| 57 | 56 | * This function can use the return value of `parse()` to build a query |
| 58 | 57 | * string. This function does not modify the provided keys when an array is |
| 59 | 58 | * encountered (like `http_build_query()` would). |
| 60 | 59 | * |
| 61 | - * @param array $params Query string parameters. | |
| 62 | - * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986 | |
| 63 | - * to encode using RFC3986, or PHP_QUERY_RFC1738 | |
| 64 | - * to encode using RFC1738. | |
| 65 | - * | |
| 66 | - * @return string | |
| 60 | + * @param array $params Query string parameters. | |
| 61 | + * @param int|false $encoding Set to false to not encode, | |
| 62 | + * PHP_QUERY_RFC3986 to encode using | |
| 63 | + * RFC3986, or PHP_QUERY_RFC1738 to | |
| 64 | + * encode using RFC1738. | |
| 65 | + * @param bool $treatBoolsAsInts Set to true to encode as 0/1, and | |
| 66 | + * false as false/true. | |
| 67 | 67 | */ |
| 68 | - public static function build(array $params, $encoding = \PHP_QUERY_RFC3986) | |
| 68 | + public static function build(array $params, $encoding = \PHP_QUERY_RFC3986, bool $treatBoolsAsInts = \true) : string | |
| 69 | 69 | { |
| 70 | 70 | if (!$params) { |
| 71 | 71 | return ''; |
| 72 | 72 | } |
| 73 | 73 | if ($encoding === \false) { |
| 74 | - $encoder = function ($str) { | |
| 74 | + $encoder = function (string $str) : string { | |
| 75 | 75 | return $str; |
| 76 | 76 | }; |
| 77 | 77 | } elseif ($encoding === \PHP_QUERY_RFC3986) { |
| 78 | 78 | $encoder = 'rawurlencode'; |
| @@ -80,22 +80,29 @@ | ||
| 80 | 80 | $encoder = 'urlencode'; |
| 81 | 81 | } else { |
| 82 | 82 | throw new \InvalidArgumentException('Invalid type'); |
| 83 | 83 | } |
| 84 | + $castBool = $treatBoolsAsInts ? static function ($v) { | |
| 85 | + return (int) $v; | |
| 86 | + } : static function ($v) { | |
| 87 | + return $v ? 'true' : 'false'; | |
| 88 | + }; | |
| 84 | 89 | $qs = ''; |
| 85 | 90 | foreach ($params as $k => $v) { |
| 86 | - $k = $encoder($k); | |
| 91 | + $k = $encoder((string) $k); | |
| 87 | 92 | if (!\is_array($v)) { |
| 88 | 93 | $qs .= $k; |
| 94 | + $v = \is_bool($v) ? $castBool($v) : $v; | |
| 89 | 95 | if ($v !== null) { |
| 90 | - $qs .= '=' . $encoder($v); | |
| 96 | + $qs .= '=' . $encoder((string) $v); | |
| 91 | 97 | } |
| 92 | 98 | $qs .= '&'; |
| 93 | 99 | } else { |
| 94 | 100 | foreach ($v as $vv) { |
| 95 | 101 | $qs .= $k; |
| 102 | + $vv = \is_bool($vv) ? $castBool($vv) : $vv; | |
| 96 | 103 | if ($vv !== null) { |
| 97 | - $qs .= '=' . $encoder($vv); | |
| 104 | + $qs .= '=' . $encoder((string) $vv); | |
| 98 | 105 | } |
| 99 | 106 | $qs .= '&'; |
| 100 | 107 | } |
| 101 | 108 | } |