1 | <?php |
||
8 | class Util |
||
9 | { |
||
10 | /** |
||
11 | * @param $input |
||
12 | * |
||
13 | * @return array|mixed|string |
||
14 | */ |
||
15 | public static function urlencodeRfc3986($input) |
||
16 | { |
||
17 | $output = ''; |
||
18 | if (is_array($input)) { |
||
19 | $output = array_map([__NAMESPACE__ . '\Util', 'urlencodeRfc3986'], $input); |
||
20 | } elseif (is_scalar($input)) { |
||
21 | $output = rawurlencode($input); |
||
22 | } |
||
23 | return $output; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param string $string |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public static function urldecodeRfc3986($string) |
||
35 | |||
36 | /** |
||
37 | * This function takes a input like a=b&a=c&d=e and returns the parsed |
||
38 | * parameters like this |
||
39 | * array('a' => array('b','c'), 'd' => 'e') |
||
40 | * |
||
41 | * @param string $input |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public static function parseParameters($input) |
||
46 | { |
||
47 | if (!is_string($input)) { |
||
48 | return []; |
||
49 | } |
||
50 | |||
51 | $pairs = explode('&', $input); |
||
52 | |||
53 | $parameters = []; |
||
54 | foreach ($pairs as $pair) { |
||
55 | $split = explode('=', $pair, 2); |
||
56 | $parameter = Util::urldecodeRfc3986($split[0]); |
||
57 | $value = isset($split[1]) ? Util::urldecodeRfc3986($split[1]) : ''; |
||
58 | |||
59 | if (isset($parameters[$parameter])) { |
||
60 | // We have already recieved parameter(s) with this name, so add to the list |
||
61 | // of parameters with this name |
||
62 | |||
63 | if (is_scalar($parameters[$parameter])) { |
||
64 | // This is the first duplicate, so transform scalar (string) into an array |
||
65 | // so we can add the duplicates |
||
66 | $parameters[$parameter] = [$parameters[$parameter]]; |
||
67 | } |
||
68 | |||
69 | $parameters[$parameter][] = $value; |
||
70 | } else { |
||
71 | $parameters[$parameter] = $value; |
||
72 | } |
||
73 | } |
||
74 | return $parameters; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param array $params |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public static function buildHttpQuery(array $params) |
||
115 | } |
||
116 |