PluginProbe
Media Cloud Sync / 1.2.7
Media Cloud Sync v1.2.7
1.4.2 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 All 36 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / Query.php

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