|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Mado\QueryBundle\Dictionary; |
|
7
|
|
|
use Mado\QueryBundle\Exceptions\InvalidFiltersException; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
|
|
10
|
|
|
class FiltersFromHttp implements Filters |
|
11
|
|
|
{ |
|
12
|
|
|
private $discriminatorCounter = 1; |
|
13
|
|
|
|
|
14
|
|
|
private $route = ''; |
|
15
|
|
|
private $routeParams = ''; |
|
16
|
|
|
private $id = ''; |
|
17
|
|
|
|
|
18
|
|
|
private $andFilters = []; |
|
19
|
|
|
private $orFilters = []; |
|
20
|
|
|
|
|
21
|
|
|
private $sorting = []; |
|
22
|
|
|
|
|
23
|
|
|
private $printing = []; |
|
24
|
|
|
private $rel = ''; |
|
25
|
|
|
private $page = ''; |
|
26
|
|
|
private $select = ''; |
|
27
|
|
|
private $limit = ''; |
|
28
|
|
|
|
|
29
|
|
|
private function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Request $request |
|
35
|
|
|
* |
|
36
|
|
|
* @return FiltersFromHttp |
|
37
|
|
|
* @throws InvalidFiltersException |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function buildFromRequest(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
$filter = new self(); |
|
42
|
|
|
|
|
43
|
|
|
$id = $request->attributes->get('id'); |
|
44
|
|
|
$route = $request->attributes->get('_route'); |
|
45
|
|
|
$routeParams = $request->attributes->get('_route_params', []); |
|
46
|
|
|
|
|
47
|
|
|
$rel = $request->query->get('rel', ''); |
|
48
|
|
|
$select = $request->query->get('select', ''); |
|
49
|
|
|
$printing = $request->query->get('printing', []); |
|
50
|
|
|
|
|
51
|
|
|
$andFilters = $request->query->get('filtering', []); |
|
52
|
|
|
$orFilters = $request->query->get('filtering_or', []); |
|
53
|
|
|
$sorting = $request->query->get('sorting', []); |
|
54
|
|
|
|
|
55
|
|
|
$page = $request->query->get('page', ''); |
|
56
|
|
|
$limit = $request->query->get('limit', ''); |
|
57
|
|
|
|
|
58
|
|
|
$filter->ensureFilterIsValid($printing); |
|
59
|
|
|
$filter->ensureFilterIsValid($andFilters); |
|
60
|
|
|
$filter->ensureFilterIsValid($orFilters); |
|
61
|
|
|
$filter->ensureFilterIsValid($sorting); |
|
62
|
|
|
|
|
63
|
|
|
// FIXME: what is it for? Can be removed? |
|
64
|
|
|
$filterOrCorrected = []; |
|
65
|
|
|
foreach ($orFilters as $key => $filterValue) { |
|
66
|
|
|
if (is_array($filterValue)) { |
|
67
|
|
|
foreach ($filterValue as $keyInternal => $internal) { |
|
68
|
|
|
$filterOrCorrected[$keyInternal . '|' . $filter->getDiscriminator()] = $internal; |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
$filterOrCorrected[$key] = $filterValue; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$filter->setId($id); |
|
76
|
|
|
$filter->setRoute($route); |
|
77
|
|
|
$filter->setRouteParams($routeParams); |
|
78
|
|
|
|
|
79
|
|
|
$filter->setRel($rel); |
|
80
|
|
|
$filter->setSelect($select); |
|
81
|
|
|
$filter->setPrinting($printing); |
|
82
|
|
|
|
|
83
|
|
|
$filter->setAndFilters($andFilters); |
|
84
|
|
|
$filter->setOrFilters($filterOrCorrected); |
|
85
|
|
|
$filter->setSorting($sorting); |
|
86
|
|
|
|
|
87
|
|
|
$filter->setPage($page); |
|
88
|
|
|
$filter->setLimit($limit); |
|
89
|
|
|
|
|
90
|
|
|
return $filter; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $filters |
|
95
|
|
|
* |
|
96
|
|
|
* @throws InvalidFiltersException |
|
97
|
|
|
*/ |
|
98
|
|
|
private function ensureFilterIsValid($filters) |
|
99
|
|
|
{ |
|
100
|
|
|
if (!is_array($filters)) { |
|
101
|
|
|
|
|
102
|
|
|
$message = "Wrong query string exception: "; |
|
103
|
|
|
$message .= var_export($filters, true) . "\n"; |
|
104
|
|
|
$message .= "Please check query string should be something like " . |
|
105
|
|
|
"http://127.0.0.1:8000/?filtering[status]=todo"; |
|
106
|
|
|
|
|
107
|
|
|
throw new InvalidFiltersException($message); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function addAndFilter(string $field, string $operator, string $value) |
|
112
|
|
|
{ |
|
113
|
|
|
Dictionary::ensureTypeIsDefined($operator); |
|
114
|
|
|
|
|
115
|
|
|
$key = $field . '|' . $operator; |
|
116
|
|
|
while (isset($this->andFilters[$key])) { |
|
117
|
|
|
$key = $field . '|' . $operator . '|' . $this->getDiscriminator(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->andFilters[$key] = $value; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function addOrFilter(string $field, string $operator, string $value) |
|
126
|
|
|
{ |
|
127
|
|
|
Dictionary::ensureTypeIsDefined($operator); |
|
128
|
|
|
|
|
129
|
|
|
$key = $field . '|' . $operator; |
|
130
|
|
|
while (isset($this->orFilters[$key])) { |
|
131
|
|
|
$key = $field . '|' . $operator . '|' . $this->getDiscriminator(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->orFilters[$key] = $value; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function getDiscriminator() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->discriminatorCounter++; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function setId(string $id) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->id = $id; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function setRoute(string $route) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->route = $route; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function setRouteParams(array $routeParams) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->routeParams = $routeParams; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
private function setAndFilters(array $andFilters) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->andFilters = $andFilters; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function setOrFilters(array $orFilters) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->orFilters = $orFilters; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
private function setSorting(array $sorting) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->sorting = $sorting; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
private function setPrinting(array $printing) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->printing = $printing; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function setRel(string $rel) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->rel = $rel; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
private function setPage(string $page) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->page = $page; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
private function setSelect(string $select) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->select = $select; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function setLimit(string $limit) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->limit = $limit; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getQueryBuilderOptions(): QueryBuilderOptions |
|
200
|
|
|
{ |
|
201
|
|
|
return QueryBuilderOptions::fromArray([ |
|
202
|
|
|
'_route' => $this->route, |
|
203
|
|
|
'_route_params' => $this->routeParams, |
|
204
|
|
|
'id' => $this->id, |
|
205
|
|
|
'page' => $this->page, |
|
206
|
|
|
'limit' => $this->limit, |
|
207
|
|
|
'filters' => $this->andFilters, |
|
208
|
|
|
'orFilters' => $this->orFilters, |
|
209
|
|
|
'sorting' => $this->sorting, |
|
210
|
|
|
'rel' => $this->rel, |
|
211
|
|
|
'select' => $this->select, |
|
212
|
|
|
'printing' => $this->printing, |
|
213
|
|
|
]); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|