Completed
Push — master ( 85f55f...06344c )
by Marcin
02:15
created

RequestParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 16.06.2018
5
 * Time: 13:45
6
 */
7
declare(strict_types=1);
8
9
namespace mrcnpdlk\Lib\UrlSearchParser;
10
11
12
use mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter;
13
use mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort;
14
15
class RequestParser
16
{
17
    public const SORT_IDENTIFIER   = 'sort';
18
    public const FILTER_IDENTIFIER = 'filter';
19
20
    public const LIMIT_IDENTIFIER  = 'limit';
21
    public const OFFSET_IDENTIFIER = 'offset';
22
    public const PAGE_IDENTIFIER   = 'page';
23
    public const PHRASE_IDENTIFIER = 'phrase';
24
25
26
    private $queryParams = [];
27
    /**
28
     * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort
29
     */
30
    private $sort;
31
    /**
32
     * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
33
     */
34
    private $filter;
35
    /**
36
     * @var integer|null
37
     */
38
    private $limit;
39
    /**
40
     * @var integer|null
41
     */
42
    private $page;
43
    /**
44
     * @var integer|null
45
     */
46
    private $offset;
47
    /**
48
     * @var string|null
49
     */
50
    private $phrase;
51
52
    /**
53
     * RequestParser constructor.
54
     *
55
     * @param string $query
56
     *
57
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException
58
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
59
     */
60 10
    public function __construct(string $query)
61
    {
62 10
        $this->parse($query);
63 4
    }
64
65
    /**
66
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
67
     */
68 2
    public function getFilter(): Filter
69
    {
70 2
        return $this->filter;
71
    }
72
73
    /**
74
     * @param int|null $default
75
     *
76
     * @return int|null
77
     */
78 2
    public function getLimit(int $default = null): ?int
79
    {
80 2
        return $this->limit ?? $default;
81
    }
82
83
    /**
84
     * @param int|null $default
85
     *
86
     * @return int|null
87
     */
88 2
    public function getOffset(int $default = null): ?int
89
    {
90 2
        return $this->offset ?? $default;
91
    }
92
93
    /**
94
     * @param int|null $default
95
     *
96
     * @return int|null
97
     */
98 2
    public function getPage(int $default = null): ?int
99
    {
100 2
        return $this->page ?? $default;
101
    }
102
103
    /**
104
     * @return null|string
105
     */
106 2
    public function getPhrase(): ?string
107
    {
108 2
        return $this->phrase;
109
    }
110
111
    /**
112
     * @param string     $param
113
     * @param string     $type
114
     * @param mixed|null $default
115
     *
116
     * @return mixed|null
117
     */
118 10
    public function getQueryParam(string $param, string $type = 'string', $default = null)
119
    {
120 10
        if (isset($this->queryParams[$param])) {
121 10
            $type = strtolower($type);
122 10
            if (!\in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array'])) {
123 1
                throw new \InvalidArgumentException(sprintf('Unsupported type [%s]', $type));
124
            }
125 9
            $var = $this->queryParams[$param];
126 9
            if (!settype($var, strtolower($type))) {
127
                throw new \RuntimeException(sprintf('Cannot set type [%s]', $type));
128
            }
129
130
131 9
            return $var;
132
        }
133
134 8
        return $default ?? null;
135
    }
136
137
    /**
138
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort
139
     */
140 2
    public function getSort(): Sort
141
    {
142 2
        return $this->sort;
143
    }
144
145
    /**
146
     * @param string $query
147
     *
148
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException
149
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
150
     */
151 10
    private function parse(string $query): void
152
    {
153 10
        parse_str($query, $this->queryParams);
154
155 10
        $this->sort   = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string'));
156 8
        $this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', []));
157 4
        $this->limit  = $this->getQueryParam('limit', 'int');
158 4
        $this->offset = $this->getQueryParam('offset', 'int');
159 4
        $this->page   = $this->getQueryParam('page', 'int');
160 4
        $this->phrase = $this->getQueryParam('phrase', 'string');
161 4
    }
162
}
163