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
|
12 |
|
public function __construct(string $query) |
61
|
|
|
{ |
62
|
12 |
|
$this->parse($query); |
63
|
6 |
|
} |
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|null $type If NULL return value AS IS |
114
|
|
|
* @param mixed|null $default |
115
|
|
|
* |
116
|
|
|
* @return mixed|null |
117
|
|
|
*/ |
118
|
12 |
|
public function getQueryParam(string $param, string $type = null, $default = null) |
119
|
|
|
{ |
120
|
12 |
|
if (isset($this->queryParams[$param])) { |
121
|
12 |
|
if ($type !== null) { |
122
|
12 |
|
$type = strtolower($type); |
123
|
12 |
|
if (!\in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array'])) { |
124
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unsupported type [%s]', $type)); |
125
|
|
|
} |
126
|
|
|
|
127
|
11 |
|
$var = $this->queryParams[$param]; |
128
|
|
|
|
129
|
11 |
|
if ($type === 'array' && \is_string($var)) { |
130
|
2 |
|
$var = explode(',', $var); |
131
|
10 |
|
} elseif ($type === 'string' && \is_array($var)) { |
132
|
1 |
|
$var = implode(',', $var); |
133
|
10 |
|
} elseif (!settype($var, strtolower($type))) { |
134
|
|
|
throw new \RuntimeException(sprintf('Cannot set type [%s]', $type)); |
135
|
|
|
} |
136
|
|
|
|
137
|
11 |
|
return $var; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->queryParams[$param]; |
141
|
|
|
} |
142
|
|
|
|
143
|
10 |
|
return $default ?? null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
148
|
|
|
*/ |
149
|
2 |
|
public function getSort(): Sort |
150
|
|
|
{ |
151
|
2 |
|
return $this->sort; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $query |
156
|
|
|
* |
157
|
|
|
* @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
158
|
|
|
* @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
159
|
|
|
*/ |
160
|
12 |
|
private function parse(string $query): void |
161
|
|
|
{ |
162
|
12 |
|
parse_str($query, $this->queryParams); |
163
|
|
|
|
164
|
12 |
|
$this->sort = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string')); |
165
|
10 |
|
$this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', [])); |
166
|
6 |
|
$this->limit = $this->getQueryParam('limit', 'int'); |
167
|
6 |
|
$this->offset = $this->getQueryParam('offset', 'int'); |
168
|
6 |
|
$this->page = $this->getQueryParam('page', 'int'); |
169
|
6 |
|
$this->phrase = $this->getQueryParam('phrase', 'string'); |
170
|
6 |
|
} |
171
|
|
|
} |
172
|
|
|
|