Passed
Push — master ( c9645e...37753a )
by Marcin
03:38
created

RequestParser::getLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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
use mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException;
15
16
class RequestParser
17
{
18
    public const SORT_IDENTIFIER   = 'sort';
19
    public const FILTER_IDENTIFIER = 'filter';
20
21
    public const LIMIT_IDENTIFIER  = 'limit';
22
    public const OFFSET_IDENTIFIER = 'offset';
23
    public const PAGE_IDENTIFIER   = 'page';
24
    public const PHRASE_IDENTIFIER = 'phrase';
25
26
27
    private $queryParams = [];
28
    /**
29
     * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort
30
     */
31
    private $sort;
32
    /**
33
     * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
34
     */
35
    private $filter;
36
    /**
37
     * @var integer|null
38
     */
39
    private $limit;
40
    /**
41
     * @var integer|null
42
     */
43
    private $page;
44
    /**
45
     * @var integer|null
46
     */
47
    private $offset;
48
    /**
49
     * @var string|null
50
     */
51
    private $phrase;
52
53
    /**
54
     * RequestParser constructor.
55
     *
56
     * @param string $query
57
     *
58
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception
59
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException
60
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
61
     */
62 18
    public function __construct(string $query)
63
    {
64 18
        $this->parse($query);
65 9
    }
66
67
    /**
68
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
69
     */
70 2
    public function getFilter(): Filter
71
    {
72 2
        return $this->filter;
73
    }
74
75
    /**
76
     * @param int|null $default
77
     *
78
     * @return int|null
79
     */
80 2
    public function getLimit(int $default = null): ?int
81
    {
82 2
        return $this->limit ?? $default;
83
    }
84
85
    /**
86
     * @param int|null $default
87
     *
88
     * @return int|null
89
     */
90 2
    public function getOffset(int $default = null): ?int
91
    {
92 2
        return $this->offset ?? $default;
93
    }
94
95
    /**
96
     * @param int|null $default
97
     *
98
     * @return int|null
99
     */
100 2
    public function getPage(int $default = null): ?int
101
    {
102 2
        return $this->page ?? $default;
103
    }
104
105
    /**
106
     * @return null|string
107
     */
108 2
    public function getPhrase(): ?string
109
    {
110 2
        return $this->phrase;
111
    }
112
113
    /**
114
     * @param string      $param
115
     * @param string|null $type If NULL return value AS IS
116
     * @param mixed|null  $default
117
     *
118
     * @return mixed|null
119
     */
120 18
    public function getQueryParam(string $param, string $type = null, $default = null)
121
    {
122 18
        if (isset($this->queryParams[$param])) {
123 18
            if ($type !== null) {
124 17
                $type = strtolower($type);
125 17
                if (!\in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array'])) {
126 1
                    throw new \InvalidArgumentException(sprintf('Unsupported type [%s]', $type));
127
                }
128
129 16
                $var = $this->queryParams[$param];
130
131 16
                if ($type === 'array' && \is_string($var)) {
132 2
                    $var = explode(',', $var);
133 15
                } elseif ($type === 'string' && \is_array($var)) {
134 1
                    $var = implode(',', $var);
135 15
                } elseif (\in_array($type, ['boolean', 'bool']) && \in_array(strtolower($var), ['true', 'false'])) {
136 2
                    $var = (strtolower($var) === 'true');
137 15
                } elseif (!settype($var, $type)) {
138
                    throw new \RuntimeException(sprintf('Cannot set type [%s]', $type));
139
                }
140
141 16
                return $var;
142
            }
143
144 1
            return $this->queryParams[$param];
145
        }
146
147 16
        return $default ?? null;
148
    }
149
150
    /**
151
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort
152
     */
153 2
    public function getSort(): Sort
154
    {
155 2
        return $this->sort;
156
    }
157
158
    /**
159
     * @param string $param
160
     *
161
     * @return $this
162
     */
163 1
    public function removeQueryParam(string $param)
164
    {
165 1
        unset($this->queryParams[$param]);
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * @param string $query
172
     *
173
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException
174
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
175
     */
176 18
    private function parse(string $query): void
177
    {
178 18
        parse_str($query, $this->queryParams);
179
180 18
        $this->sort   = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string'));
181 16
        $this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', []));
182 12
        $this->limit  = $this->getQueryParam('limit', 'int');
183 12
        if (null !== $this->limit && $this->limit < 0) {
184 1
            throw new InvalidParamException('Limit value cannot be lower than 0');
185
        }
186 11
        $this->offset = $this->getQueryParam('offset', 'int');
187 11
        if (null !== $this->offset && $this->offset < 0) {
188 1
            throw new InvalidParamException('Offset value cannot be lower than 0');
189
        }
190 10
        $this->page = $this->getQueryParam('page', 'int');
191 10
        if (null !== $this->page && $this->page < 0) {
192 1
            throw new InvalidParamException('Page value cannot be lower than 0');
193
        }
194 9
        $this->phrase = $this->getQueryParam('phrase', 'string');
195 9
    }
196
}
197