PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 18.2, at vendor_prefixed/guzzlehttp/psr7/src/Query.php

106 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Psr7;
4
5 final class Query
6 {
7 /**
8 * Parse a query string into an associative array.
9 *
10 * If multiple values are found for the same key, the value of that key
11 * value pair will become an array. This function does not parse nested
12 * PHP style arrays into an associative array (e.g., `foo[a]=1&foo[b]=2`
13 * will be parsed into `['foo[a]' => '1', 'foo[b]' => '2'])`.
14 *
15 * @param string $str Query string to parse
16 * @param int|bool $urlEncoding How the query string is encoded
17 *
18 * @return array
19 */
20 public static function parse($str, $urlEncoding = \true)
21 {
22 $result = [];
23 if ($str === '') {
24 return $result;
25 }
26 if ($urlEncoding === \true) {
27 $decoder = function ($value) {
28 return \rawurldecode(\str_replace('+', ' ', $value));
29 };
30 } elseif ($urlEncoding === \PHP_QUERY_RFC3986) {
31 $decoder = 'rawurldecode';
32 } elseif ($urlEncoding === \PHP_QUERY_RFC1738) {
33 $decoder = 'urldecode';
34 } else {
35 $decoder = function ($str) {
36 return $str;
37 };
38 }
39 foreach (\explode('&', $str) as $kvp) {
40 $parts = \explode('=', $kvp, 2);
41 $key = $decoder($parts[0]);
42 $value = isset($parts[1]) ? $decoder($parts[1]) : null;
43 if (!isset($result[$key])) {
44 $result[$key] = $value;
45 } else {
46 if (!\is_array($result[$key])) {
47 $result[$key] = [$result[$key]];
48 }
49 $result[$key][] = $value;
50 }
51 }
52 return $result;
53 }
54 /**
55 * Build a query string from an array of key value pairs.
56 *
57 * This function can use the return value of `parse()` to build a query
58 * string. This function does not modify the provided keys when an array is
59 * encountered (like `http_build_query()` would).
60 *
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
67 */
68 public static function build(array $params, $encoding = \PHP_QUERY_RFC3986)
69 {
70 if (!$params) {
71 return '';
72 }
73 if ($encoding === \false) {
74 $encoder = function ($str) {
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 $qs = '';
85 foreach ($params as $k => $v) {
86 $k = $encoder($k);
87 if (!\is_array($v)) {
88 $qs .= $k;
89 if ($v !== null) {
90 $qs .= '=' . $encoder($v);
91 }
92 $qs .= '&';
93 } else {
94 foreach ($v as $vv) {
95 $qs .= $k;
96 if ($vv !== null) {
97 $qs .= '=' . $encoder($vv);
98 }
99 $qs .= '&';
100 }
101 }
102 }
103 return $qs ? (string) \substr($qs, 0, -1) : '';
104 }
105 }
106