PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.20
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.20
4.1.20 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 All 174 releases
searchpro / BerqWP / vendor-prefixed / vendor / guzzlehttp / psr7 / src / Query.php

Query.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.20, at BerqWP/vendor-prefixed/vendor/guzzlehttp/psr7/src/Query.php

113 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace BerqWP_Deps\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,
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 */
68 public static function build(array $params, $encoding = \PHP_QUERY_RFC3986, bool $treatBoolsAsInts = \true): string
69 {
70 if (!$params) {
71 return '';
72 }
73 if ($encoding === \false) {
74 $encoder = function (string $str): string {
75 return $str;
76 };
77 } elseif ($encoding === \PHP_QUERY_RFC3986) {
78 $encoder = 'rawurlencode';
79 } elseif ($encoding === \PHP_QUERY_RFC1738) {
80 $encoder = 'urlencode';
81 } else {
82 throw new \InvalidArgumentException('Invalid type');
83 }
84 $castBool = $treatBoolsAsInts ? static function ($v) {
85 return (int) $v;
86 } : static function ($v) {
87 return $v ? 'true' : 'false';
88 };
89 $qs = '';
90 foreach ($params as $k => $v) {
91 $k = $encoder((string) $k);
92 if (!is_array($v)) {
93 $qs .= $k;
94 $v = is_bool($v) ? $castBool($v) : $v;
95 if ($v !== null) {
96 $qs .= '=' . $encoder((string) $v);
97 }
98 $qs .= '&';
99 } else {
100 foreach ($v as $vv) {
101 $qs .= $k;
102 $vv = is_bool($vv) ? $castBool($vv) : $vv;
103 if ($vv !== null) {
104 $qs .= '=' . $encoder((string) $vv);
105 }
106 $qs .= '&';
107 }
108 }
109 }
110 return $qs ? (string) substr($qs, 0, -1) : '';
111 }
112 }
113