Completed
Push — master ( 29feb0...d9398e )
by Timo
07:22
created

UrlHelper::queryParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
use Illuminate\Http\Request;
6
use RuntimeException;
7
8
/**
9
 * Class UrlHelper
10
 * @package hamburgscleanest\DataTables\Helpers
11
 */
12
class UrlHelper {
13
14
    /** @var Request */
15
    private $_request;
16
17
    /**
18
     * UrlHelper constructor.
19
     * @param Request $request
20
     */
21 17
    public function __construct(Request $request)
22
    {
23 17
        $this->_request = $request;
24 17
    }
25
26
    /**
27
     * Array of all the query parameters for the current request.
28
     *
29
     * @return array
30
     * @throws \RuntimeException
31
     */
32 11
    public function queryParameters() : array
33
    {
34 11
        $queryString = $this->_request->getQueryString();
35
36 11
        return empty($queryString) ? [] : $this->_extractQueryParameters($queryString);
37
    }
38 9
39
    /**
40
     * @param string $queryString
41 2
     * @return array
42 2
     * @throws \RuntimeException
43
     */
44 2
    private function _extractQueryParameters(string $queryString) : array
45 2
    {
46
        $parameters = [];
47 1
        foreach (\explode('&', $queryString) as $query)
48
        {
49
            $queryParts = \explode('=', $query);
50 1
            if (\count($queryParts) !== 2)
51
            {
52
                throw new RuntimeException('Malformed query parameters.');
53 1
            }
54
55
            $parameters[$queryParts[0]] = $queryParts[1];
56
        }
57
58
        return $parameters;
59
    }
60
}