QueryParams   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 68
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 8 3
A getFromServer() 0 4 1
B get() 0 18 6
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Http;
22
23
/**
24
 * Parses a query string from a URI and gets its values accurately.
25
 *
26
 * The problem here is that PHP will overwrite entries in the `$_GET`
27
 * superglobal if they share the same name. With PHP, a request to
28
 * `file.php?foobar=foo&foobar=bar` will result in `$_GET` set to
29
 * `['foobar' => 'bar']`.
30
 *
31
 * Other platforms allow list-like access to these parameters. This class will
32
 * account for both of the following cases:
33
 * - `param=foo&param=bar`
34
 * - `param[]=foo&param[]=bar`
35
 *
36
 * @copyright 2015-2018 LibreWorks contributors
37
 * @license   Apache-2.0
38
 */
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
78
    {
79 2
        $name = $pair[0];
80 2
        if (substr($name, -2, 2) == '[]') {
81 1
            $name = substr($name, 0, -2);
82
        }
83 2
        return [$name, count($pair) > 1 ? $pair[1] : ''];
84
    }
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
103
    {
104 2
        return self::get($_SERVER);
105
    }
106
}
107