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
|
15 |
|
public function __construct(string $query) |
63
|
|
|
{ |
64
|
15 |
|
$this->parse($query); |
65
|
6 |
|
} |
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
|
15 |
|
public function getQueryParam(string $param, string $type = null, $default = null) |
121
|
|
|
{ |
122
|
15 |
|
if (isset($this->queryParams[$param])) { |
123
|
15 |
|
if ($type !== null) { |
124
|
15 |
|
$type = strtolower($type); |
125
|
15 |
|
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
|
14 |
|
$var = $this->queryParams[$param]; |
130
|
|
|
|
131
|
14 |
|
if ($type === 'array' && \is_string($var)) { |
132
|
2 |
|
$var = explode(',', $var); |
133
|
13 |
|
} elseif ($type === 'string' && \is_array($var)) { |
134
|
1 |
|
$var = implode(',', $var); |
135
|
13 |
|
} elseif (!settype($var, strtolower($type))) { |
136
|
|
|
throw new \RuntimeException(sprintf('Cannot set type [%s]', $type)); |
137
|
|
|
} |
138
|
|
|
|
139
|
14 |
|
return $var; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->queryParams[$param]; |
143
|
|
|
} |
144
|
|
|
|
145
|
13 |
|
return $default ?? null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
150
|
|
|
*/ |
151
|
2 |
|
public function getSort(): Sort |
152
|
|
|
{ |
153
|
2 |
|
return $this->sort; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $query |
158
|
|
|
* |
159
|
|
|
* @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
160
|
|
|
* @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
161
|
|
|
*/ |
162
|
15 |
|
private function parse(string $query): void |
163
|
|
|
{ |
164
|
15 |
|
parse_str($query, $this->queryParams); |
165
|
|
|
|
166
|
15 |
|
$this->sort = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string')); |
167
|
13 |
|
$this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', [])); |
168
|
9 |
|
$this->limit = $this->getQueryParam('limit', 'int'); |
169
|
9 |
|
if (null !== $this->limit && $this->limit < 0) { |
170
|
1 |
|
throw new InvalidParamException('Limit value cannot be lower than 0'); |
171
|
|
|
} |
172
|
8 |
|
$this->offset = $this->getQueryParam('offset', 'int'); |
173
|
8 |
|
if (null !== $this->offset && $this->offset < 0) { |
174
|
1 |
|
throw new InvalidParamException('Offset value cannot be lower than 0'); |
175
|
|
|
} |
176
|
7 |
|
$this->page = $this->getQueryParam('page', 'int'); |
177
|
7 |
|
if (null !== $this->page && $this->page < 0) { |
178
|
1 |
|
throw new InvalidParamException('Page value cannot be lower than 0'); |
179
|
|
|
} |
180
|
6 |
|
$this->phrase = $this->getQueryParam('phrase', 'string'); |
181
|
6 |
|
} |
182
|
|
|
} |
183
|
|
|
|