1 | <?php |
||
39 | class QueryParams |
||
40 | { |
||
41 | /** |
||
42 | * Parses the query string from the `QUERY_STRING` in the provided array. |
||
43 | * |
||
44 | * For the URL `file.php?test[]=1&test[]=2&noval&foobar=foo&foobar=bar&abc=123`, |
||
45 | * the returned array will be: |
||
46 | * |
||
47 | * ```php |
||
48 | * [ |
||
49 | * 'test' => ['1', '2'], |
||
50 | * 'noval' => '', |
||
51 | * 'foobar' => ['foo', 'bar'], |
||
52 | * 'abc' => '123' |
||
53 | * ]; |
||
54 | * ``` |
||
55 | * |
||
56 | * @param array<string,mixed> $server The server values array |
||
57 | */ |
||
58 | 2 | public static function get(array $server): array |
|
59 | { |
||
60 | 2 | $params = []; |
|
61 | 2 | if (isset($server['QUERY_STRING'])) { |
|
62 | 2 | $query = ltrim($server['QUERY_STRING'], '?'); |
|
63 | 2 | foreach (explode('&', $query) as $pair) { |
|
64 | 2 | if ($pair) { |
|
65 | 2 | list($name, $value) = self::normalize( |
|
66 | 2 | array_map('urldecode', explode('=', $pair, 2)) |
|
67 | ); |
||
68 | 2 | $params[$name][] = $value; |
|
69 | } |
||
70 | } |
||
71 | } |
||
72 | 2 | return $params ? array_map(function ($v) { |
|
73 | 2 | return count($v) === 1 ? $v[0] : $v; |
|
74 | 2 | }, $params) : $params; |
|
75 | } |
||
76 | |||
77 | 2 | protected static function normalize(array $pair): array |
|
85 | |||
86 | /** |
||
87 | * Parses the query string from the `QUERY_STRING` in the `$_SERVER` superglobal. |
||
88 | * |
||
89 | * For the URL `file.php?test[]=1&test[]=2&noval&foobar=foo&foobar=bar&abc=123`, |
||
90 | * the returned array will be: |
||
91 | * |
||
92 | * ```php |
||
93 | * [ |
||
94 | * 'test' => ['1', '2'], |
||
95 | * 'noval' => '', |
||
96 | * 'foobar' => ['foo', 'bar'], |
||
97 | * 'abc' => '123' |
||
98 | * ]; |
||
99 | * ``` |
||
100 | * @return array<string,mixed> The query string values |
||
101 | */ |
||
102 | 2 | public static function getFromServer(): array |
|
106 | } |
||
107 |