Completed
Branch master (005f57)
by Timo
02:33
created

UrlHelper::parameterizeQuery()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 4
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
use RuntimeException;
6
7
/**
8
 * Class UrlHelper
9
 * @package hamburgscleanest\DataTables\Helpers
10
 *
11
 * TODO: Facade?
12
 */
13
class UrlHelper {
14
15
    /**
16
     * @param string|null $queryString
17
     *
18
     * @return array
19
     * @throws \RuntimeException
20
     */
21 3
    public static function parameterizeQuery(? string $queryString = null)
22
    {
23 3
        if (empty($queryString))
24
        {
25 1
            return [];
26
        }
27
28 2
        $parameters = [];
29 2
        foreach (\explode('&', $queryString) as $query)
30
        {
31 2
            $queryParts = \explode('=', $query);
32 2
            if (\count($queryParts) !== 2)
33
            {
34 1
                throw new RuntimeException('Malformed query parameters.');
35
            }
36
37 1
            $parameters[$queryParts[0]] = $queryParts[1];
38
        }
39
40 1
        return $parameters;
41
    }
42
}