PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / Query.php

Query.php in Media Cloud Sync 1.2.13, at includes/sdk/s3/GuzzleHttp/Psr7/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 Dudlewebs\WPMCS\s3\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