| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
final class Query |
| 7 |
{ |
| 8 |
private function __construct() |
| 9 |
{ |
| 10 |
} |
| 11 |
/** |
| 12 |
* Parse a query string into an associative array. |
| 13 |
* |
| 14 |
* If multiple values are found for the same key, the value of that |
| 15 |
* key-value pair becomes an array. This function does not parse nested PHP |
| 16 |
* style arrays into an associative array. For example, `foo[a]=1&foo[b]=2` |
| 17 |
* will be parsed into `['foo[a]' => '1', 'foo[b]' => '2']`. |
| 18 |
* |
| 19 |
* @param string $str Query string to parse |
| 20 |
* @param int|bool $urlEncoding How the query string is encoded |
| 21 |
*/ |
| 22 |
public static function parse(string $str, $urlEncoding = \true) : array |
| 23 |
{ |
| 24 |
$result = []; |
| 25 |
if ($str === '') { |
| 26 |
return $result; |
| 27 |
} |
| 28 |
if ($urlEncoding === \true) { |
| 29 |
$decoder = function (string $value) : string { |
| 30 |
return \rawurldecode(\str_replace('+', ' ', $value)); |
| 31 |
}; |
| 32 |
} elseif ($urlEncoding === \PHP_QUERY_RFC3986) { |
| 33 |
$decoder = static function (string $value) : string { |
| 34 |
return \rawurldecode($value); |
| 35 |
}; |
| 36 |
} elseif ($urlEncoding === \PHP_QUERY_RFC1738) { |
| 37 |
$decoder = static function (string $value) : string { |
| 38 |
return \urldecode($value); |
| 39 |
}; |
| 40 |
} else { |
| 41 |
$decoder = function (string $str) : string { |
| 42 |
return $str; |
| 43 |
}; |
| 44 |
} |
| 45 |
foreach (\explode('&', $str) as $kvp) { |
| 46 |
$parts = \explode('=', $kvp, 2); |
| 47 |
$key = $decoder($parts[0]); |
| 48 |
$value = isset($parts[1]) ? $decoder($parts[1]) : null; |
| 49 |
if (!\array_key_exists($key, $result)) { |
| 50 |
$result[$key] = $value; |
| 51 |
} else { |
| 52 |
if (!\is_array($result[$key])) { |
| 53 |
$result[$key] = [$result[$key]]; |
| 54 |
} |
| 55 |
$result[$key][] = $value; |
| 56 |
} |
| 57 |
} |
| 58 |
return $result; |
| 59 |
} |
| 60 |
/** |
| 61 |
* Build a query string from an array of key-value pairs. |
| 62 |
* |
| 63 |
* This function can use the return value of `parse()` to build a query |
| 64 |
* string. This function does not modify the provided keys when an array is |
| 65 |
* encountered, unlike `http_build_query()`. |
| 66 |
* |
| 67 |
* @param array $params Query string parameters. |
| 68 |
* @param int|false $encoding Set to false to not encode, |
| 69 |
* PHP_QUERY_RFC3986 to encode using |
| 70 |
* RFC3986, or PHP_QUERY_RFC1738 to |
| 71 |
* encode using RFC1738. |
| 72 |
* @param bool $treatBoolsAsInts Set to true to encode as 0/1, and |
| 73 |
* false as false/true. |
| 74 |
*/ |
| 75 |
public static function build(array $params, $encoding = \PHP_QUERY_RFC3986, bool $treatBoolsAsInts = \true) : string |
| 76 |
{ |
| 77 |
if (!$params) { |
| 78 |
return ''; |
| 79 |
} |
| 80 |
if ($encoding === \false) { |
| 81 |
$encoder = function (string $str) : string { |
| 82 |
return $str; |
| 83 |
}; |
| 84 |
} elseif ($encoding === \PHP_QUERY_RFC3986) { |
| 85 |
$encoder = static function (string $value) : string { |
| 86 |
return \rawurlencode($value); |
| 87 |
}; |
| 88 |
} elseif ($encoding === \PHP_QUERY_RFC1738) { |
| 89 |
$encoder = static function (string $value) : string { |
| 90 |
return \urlencode($value); |
| 91 |
}; |
| 92 |
} else { |
| 93 |
throw new \InvalidArgumentException('Invalid type'); |
| 94 |
} |
| 95 |
$qs = ''; |
| 96 |
foreach ($params as $k => $v) { |
| 97 |
$k = $encoder((string) $k); |
| 98 |
if (!\is_array($v)) { |
| 99 |
$qs .= $k; |
| 100 |
$v = self::normalizeValue($v, $treatBoolsAsInts); |
| 101 |
if ($v !== null) { |
| 102 |
$qs .= '=' . $encoder($v); |
| 103 |
} |
| 104 |
$qs .= '&'; |
| 105 |
} else { |
| 106 |
foreach ($v as $vv) { |
| 107 |
$qs .= $k; |
| 108 |
$vv = self::normalizeValue($vv, $treatBoolsAsInts); |
| 109 |
if ($vv !== null) { |
| 110 |
$qs .= '=' . $encoder($vv); |
| 111 |
} |
| 112 |
$qs .= '&'; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
return $qs ? (string) \substr($qs, 0, -1) : ''; |
| 117 |
} |
| 118 |
/** |
| 119 |
* @param mixed $value |
| 120 |
*/ |
| 121 |
private static function normalizeValue($value, bool $treatBoolsAsInts) : ?string |
| 122 |
{ |
| 123 |
if ($value === null) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
if (\is_bool($value)) { |
| 127 |
return $treatBoolsAsInts ? (string) (int) $value : ($value ? 'true' : 'false'); |
| 128 |
} |
| 129 |
if (\is_float($value) && !\is_finite($value)) { |
| 130 |
throw new \InvalidArgumentException('Query string values must be finite; non-finite floats are not supported.'); |
| 131 |
} |
| 132 |
if (\is_scalar($value)) { |
| 133 |
return (string) $value; |
| 134 |
} |
| 135 |
if (\is_object($value) && \method_exists($value, '__toString')) { |
| 136 |
return $value->__toString(); |
| 137 |
} |
| 138 |
throw new \InvalidArgumentException('Query string values must be scalar, null, or stringable objects'); |
| 139 |
} |
| 140 |
} |
| 141 |
|