Exception
2 years ago
AppendStream.php
2 years ago
BufferStream.php
2 years ago
CachingStream.php
2 years ago
DroppingStream.php
2 years ago
FnStream.php
2 years ago
Header.php
2 years ago
HttpFactory.php
2 years ago
InflateStream.php
2 years ago
LazyOpenStream.php
2 years ago
LimitStream.php
2 years ago
Message.php
2 years ago
MessageTrait.php
2 years ago
MimeType.php
2 years ago
MultipartStream.php
2 years ago
NoSeekStream.php
2 years ago
PumpStream.php
2 years ago
Query.php
2 years ago
Request.php
2 years ago
Response.php
2 years ago
Rfc7230.php
2 years ago
ServerRequest.php
2 years ago
Stream.php
2 years ago
StreamDecoratorTrait.php
2 years ago
StreamWrapper.php
2 years ago
UploadedFile.php
2 years ago
Uri.php
2 years ago
UriComparator.php
2 years ago
UriNormalizer.php
2 years ago
UriResolver.php
2 years ago
Utils.php
2 years ago
Query.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 5 | |
| 6 | final class Query |
| 7 | { |
| 8 | /** |
| 9 | * Parse a query string into an associative array. |
| 10 | * |
| 11 | * If multiple values are found for the same key, the value of that key |
| 12 | * value pair will become an array. This function does not parse nested |
| 13 | * PHP style arrays into an associative array (e.g., `foo[a]=1&foo[b]=2` |
| 14 | * will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`. |
| 15 | * |
| 16 | * @param string $str Query string to parse |
| 17 | * @param int|bool $urlEncoding How the query string is encoded |
| 18 | */ |
| 19 | public static function parse(string $str, $urlEncoding = \true) : array |
| 20 | { |
| 21 | $result = []; |
| 22 | if ($str === '') { |
| 23 | return $result; |
| 24 | } |
| 25 | if ($urlEncoding === \true) { |
| 26 | $decoder = function ($value) { |
| 27 | return \rawurldecode(\str_replace('+', ' ', (string) $value)); |
| 28 | }; |
| 29 | } elseif ($urlEncoding === \PHP_QUERY_RFC3986) { |
| 30 | $decoder = 'rawurldecode'; |
| 31 | } elseif ($urlEncoding === \PHP_QUERY_RFC1738) { |
| 32 | $decoder = 'urldecode'; |
| 33 | } else { |
| 34 | $decoder = function ($str) { |
| 35 | return $str; |
| 36 | }; |
| 37 | } |
| 38 | foreach (\explode('&', $str) as $kvp) { |
| 39 | $parts = \explode('=', $kvp, 2); |
| 40 | $key = $decoder($parts[0]); |
| 41 | $value = isset($parts[1]) ? $decoder($parts[1]) : null; |
| 42 | if (!\array_key_exists($key, $result)) { |
| 43 | $result[$key] = $value; |
| 44 | } else { |
| 45 | if (!\is_array($result[$key])) { |
| 46 | $result[$key] = [$result[$key]]; |
| 47 | } |
| 48 | $result[$key][] = $value; |
| 49 | } |
| 50 | } |
| 51 | return $result; |
| 52 | } |
| 53 | /** |
| 54 | * Build a query string from an array of key value pairs. |
| 55 | * |
| 56 | * This function can use the return value of `parse()` to build a query |
| 57 | * string. This function does not modify the provided keys when an array is |
| 58 | * encountered (like `http_build_query()` would). |
| 59 | * |
| 60 | * @param array $params Query string parameters. |
| 61 | * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986 |
| 62 | * to encode using RFC3986, or PHP_QUERY_RFC1738 |
| 63 | * to encode using RFC1738. |
| 64 | */ |
| 65 | public static function build(array $params, $encoding = \PHP_QUERY_RFC3986) : string |
| 66 | { |
| 67 | if (!$params) { |
| 68 | return ''; |
| 69 | } |
| 70 | if ($encoding === \false) { |
| 71 | $encoder = function (string $str) : string { |
| 72 | return $str; |
| 73 | }; |
| 74 | } elseif ($encoding === \PHP_QUERY_RFC3986) { |
| 75 | $encoder = 'rawurlencode'; |
| 76 | } elseif ($encoding === \PHP_QUERY_RFC1738) { |
| 77 | $encoder = 'urlencode'; |
| 78 | } else { |
| 79 | throw new \InvalidArgumentException('Invalid type'); |
| 80 | } |
| 81 | $qs = ''; |
| 82 | foreach ($params as $k => $v) { |
| 83 | $k = $encoder((string) $k); |
| 84 | if (!\is_array($v)) { |
| 85 | $qs .= $k; |
| 86 | $v = \is_bool($v) ? (int) $v : $v; |
| 87 | if ($v !== null) { |
| 88 | $qs .= '=' . $encoder((string) $v); |
| 89 | } |
| 90 | $qs .= '&'; |
| 91 | } else { |
| 92 | foreach ($v as $vv) { |
| 93 | $qs .= $k; |
| 94 | $vv = \is_bool($vv) ? (int) $vv : $vv; |
| 95 | if ($vv !== null) { |
| 96 | $qs .= '=' . $encoder((string) $vv); |
| 97 | } |
| 98 | $qs .= '&'; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return $qs ? (string) \substr($qs, 0, -1) : ''; |
| 103 | } |
| 104 | } |
| 105 |