PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / Query.php

Query.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/psr7/src/Query.php

129 lines 4.7 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 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,
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) : self::normalizeNonFiniteFloat($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) : self::normalizeNonFiniteFloat($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 * Converts non-finite floats to the strings PHP coerces them to, as
114 * implicit coercion of NAN emits a warning on PHP 8.5.
115 *
116 * @param mixed $value
117 *
118 * @return mixed
119 */
120 private static function normalizeNonFiniteFloat($value)
121 {
122 if (\is_float($value) && !\is_finite($value)) {
123 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.12', 'Passing a non-finite float to Query::build() is deprecated; guzzlehttp/psr7 3.0 rejects non-finite floats.');
124 return \is_nan($value) ? 'NAN' : ($value > 0 ? 'INF' : '-INF');
125 }
126 return $value;
127 }
128 }
129