1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
use Mado\QueryBundle\Component\Meta\Exceptions\UnInitializedQueryBuilderException; |
8
|
|
|
use Mado\QueryBundle\Dictionary; |
9
|
|
|
use Mado\QueryBundle\Exceptions; |
10
|
|
|
use Mado\QueryBundle\Queries\Objects\FilterObject; |
11
|
|
|
|
12
|
|
|
class QueryBuilderFactory extends AbstractQuery |
13
|
|
|
{ |
14
|
|
|
const DIRECTION_AZ = 'asc'; |
15
|
|
|
|
16
|
|
|
const DIRECTION_ZA = 'desc'; |
17
|
|
|
|
18
|
|
|
const DEFAULT_OPERATOR = 'eq'; |
19
|
|
|
|
20
|
|
|
private const AND_OPERATOR_LOGIC = 'AND'; |
21
|
|
|
|
22
|
|
|
private const OR_OPERATOR_LOGIC = 'OR'; |
23
|
|
|
|
24
|
|
|
protected $qBuilder; |
25
|
|
|
|
26
|
|
|
protected $fields; |
27
|
|
|
|
28
|
|
|
protected $andFilters; |
29
|
|
|
|
30
|
|
|
protected $orFilters; |
31
|
|
|
|
32
|
|
|
protected $sorting; |
33
|
|
|
|
34
|
|
|
private $joins = []; |
35
|
|
|
|
36
|
|
|
protected $rel; |
37
|
|
|
|
38
|
|
|
protected $printing; |
39
|
|
|
|
40
|
|
|
protected $page; |
41
|
|
|
|
42
|
|
|
protected $pageLength; |
43
|
|
|
|
44
|
|
|
protected $select; |
45
|
|
|
|
46
|
1 |
|
public function getAvailableFilters() |
47
|
|
|
{ |
48
|
1 |
|
return array_keys($this->getValueAvailableFilters()); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
public function getValueAvailableFilters() |
52
|
|
|
{ |
53
|
2 |
|
return Dictionary::getOperators(); |
54
|
|
|
} |
55
|
|
|
|
56
|
29 |
|
public function setFields(array $fields = []) |
57
|
|
|
{ |
58
|
29 |
|
$this->fields = $fields; |
59
|
|
|
|
60
|
29 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function getFields() |
64
|
|
|
{ |
65
|
2 |
|
if (null === $this->fields) { |
66
|
1 |
|
throw new \RuntimeException( |
67
|
1 |
|
'Oops! Fields are not defined' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->fields; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @since version 2.2 */ |
75
|
14 |
|
public function setAndFilters(array $andFilters = []) |
76
|
|
|
{ |
77
|
14 |
|
$this->andFilters = $andFilters; |
78
|
|
|
|
79
|
14 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
13 |
|
public function setOrFilters(array $orFilters = []) |
83
|
|
|
{ |
84
|
13 |
|
$this->orFilters = $orFilters; |
85
|
|
|
|
86
|
13 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function setSorting(array $sorting = []) |
90
|
|
|
{ |
91
|
4 |
|
$this->sorting = $sorting; |
92
|
|
|
|
93
|
4 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getAndFilters() |
97
|
|
|
{ |
98
|
1 |
|
return $this->andFilters; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function getOrFilters() |
102
|
|
|
{ |
103
|
1 |
|
return $this->orFilters; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name) |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
1 |
|
public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC) |
111
|
|
|
{ |
112
|
1 |
|
$this->joinFactory->join($relation, $logicOperator); |
113
|
|
|
|
114
|
1 |
|
$innerJoins = $this->joinFactory->getInnerJoin(); |
115
|
1 |
|
$leftJoins = $this->joinFactory->getLeftJoin(); |
116
|
|
|
|
117
|
1 |
|
foreach ($innerJoins as $join) { |
118
|
1 |
|
if (!$this->joinAlreadyDone($join)) { |
119
|
1 |
|
$this->storeJoinDone($join); |
120
|
1 |
|
$this->qBuilder->innerJoin($join['field'], $join['relation']); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
foreach ($leftJoins as $join) { |
125
|
|
|
if (!$this->joinAlreadyDone($join)) { |
126
|
|
|
$this->storeJoinDone($join); |
127
|
|
|
$this->qBuilder->leftJoin($join['field'], $join['relation']); |
128
|
|
|
} |
129
|
|
|
} |
130
|
1 |
|
} |
131
|
|
|
|
132
|
11 |
|
private function joinAlreadyDone($join) :bool |
133
|
|
|
{ |
134
|
11 |
|
$needle = $join['field'] . '_' . $join['relation']; |
135
|
11 |
|
if (in_array($needle, $this->joins)) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
11 |
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
11 |
|
private function storeJoinDone($join) |
143
|
|
|
{ |
144
|
11 |
|
$needle = $join['field'] . '_' . $join['relation']; |
145
|
11 |
|
$this->joins[] = $needle; |
146
|
11 |
|
} |
147
|
|
|
|
148
|
25 |
|
public function filter() |
149
|
|
|
{ |
150
|
25 |
|
if (null === $this->andFilters && null === $this->orFilters) { |
151
|
1 |
|
throw new Exceptions\MissingFiltersException(); |
152
|
|
|
} |
153
|
|
|
|
154
|
24 |
|
if (!$this->fields) { |
155
|
1 |
|
throw new Exceptions\MissingFieldsException(); |
156
|
|
|
} |
157
|
|
|
|
158
|
23 |
|
if (null !== $this->andFilters) { |
159
|
12 |
|
$andFilterFactory = new AndFilterFactory($this->entityAlias, $this->fields, $this->joinFactory); |
160
|
12 |
|
$andFilterFactory->createFilter($this->andFilters); |
161
|
|
|
|
162
|
12 |
|
$conditions = $andFilterFactory->getConditions(); |
163
|
12 |
|
$parameters = $andFilterFactory->getParameters(); |
164
|
12 |
|
$innerJoins = $andFilterFactory->getInnerJoin(); |
165
|
|
|
|
166
|
12 |
|
foreach($conditions as $condition) { |
167
|
12 |
|
$this->qBuilder->andWhere($condition); |
168
|
|
|
} |
169
|
|
|
|
170
|
12 |
|
foreach($parameters as $parameter) { |
171
|
8 |
|
$this->qBuilder->setParameter($parameter['field'], $parameter['value']); |
172
|
|
|
} |
173
|
|
|
|
174
|
12 |
|
foreach ($innerJoins as $join) { |
175
|
5 |
|
if (!$this->joinAlreadyDone($join)) { |
176
|
5 |
|
$this->storeJoinDone($join); |
177
|
5 |
|
$this->qBuilder->innerJoin($join['field'], $join['relation']); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
23 |
|
if (null !== $this->orFilters) { |
183
|
11 |
|
$orFilterFactory = new OrFilterFactory($this->entityAlias, $this->fields, $this->joinFactory); |
184
|
11 |
|
$orFilterFactory->createFilter($this->orFilters); |
185
|
|
|
|
186
|
11 |
|
$conditions = $orFilterFactory->getConditions(); |
187
|
11 |
|
$parameters = $orFilterFactory->getParameters(); |
188
|
11 |
|
$leftJoins = $orFilterFactory->getLeftJoin(); |
189
|
|
|
|
190
|
11 |
|
if ($conditions !== '') { |
191
|
11 |
|
$this->qBuilder->andWhere($conditions); |
192
|
|
|
|
193
|
11 |
|
foreach ($parameters as $parameter) { |
194
|
7 |
|
$this->qBuilder->setParameter($parameter['field'], $parameter['value']); |
195
|
|
|
} |
196
|
|
|
|
197
|
11 |
|
foreach ($leftJoins as $join) { |
198
|
5 |
|
if (!$this->joinAlreadyDone($join)) { |
199
|
5 |
|
$this->storeJoinDone($join); |
200
|
5 |
|
$this->qBuilder->leftJoin($join['field'], $join['relation']); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
23 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
5 |
|
public function sort() |
210
|
|
|
{ |
211
|
5 |
|
if (!$this->fields) { |
212
|
1 |
|
throw new \RuntimeException( |
213
|
1 |
|
'Oops! Fields are not defined' |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
4 |
|
if (null === $this->sorting) { |
218
|
1 |
|
throw new \RuntimeException( |
219
|
1 |
|
'Oops! Sorting is not defined' |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
3 |
|
foreach ($this->sorting as $sort => $val) { |
224
|
3 |
|
$val = strtolower($val); |
225
|
|
|
|
226
|
3 |
|
$fieldName = $this->parser->camelize($sort); |
227
|
|
|
|
228
|
3 |
|
if (in_array($fieldName, $this->fields)) { |
229
|
2 |
|
$direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
230
|
2 |
|
$this->ensureQueryBuilderIsDefined(); |
231
|
1 |
|
$this->qBuilder->addOrderBy($this->entityAlias . '.' . $fieldName, $direction); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
if (strstr($sort, '_embedded.')) { |
235
|
1 |
|
$this->join($sort); |
236
|
1 |
|
$relationEntityAlias = $this->joinFactory->getRelationEntityAlias(); |
237
|
|
|
|
238
|
1 |
|
$embeddedFields = explode('.', $sort); |
239
|
1 |
|
$fieldName = $this->parser->camelize($embeddedFields[2]); |
240
|
1 |
|
$direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
241
|
|
|
|
242
|
2 |
|
$this->qBuilder->addOrderBy($relationEntityAlias . '.' . $fieldName, $direction); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
2 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
22 |
|
public function getQueryBuilder() :QueryBuilder |
251
|
|
|
{ |
252
|
22 |
|
if (!$this->qBuilder) { |
253
|
1 |
|
throw new UnInitializedQueryBuilderException(); |
254
|
|
|
} |
255
|
|
|
|
256
|
21 |
|
return $this->qBuilder; |
257
|
|
|
} |
258
|
|
|
|
259
|
10 |
|
public function setRel(array $rel) |
260
|
|
|
{ |
261
|
10 |
|
$this->rel = $rel; |
262
|
|
|
|
263
|
10 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
2 |
|
public function getRel() : array |
267
|
|
|
{ |
268
|
2 |
|
return $this->rel; |
269
|
|
|
} |
270
|
|
|
|
271
|
1 |
|
public function addRel($relation) |
272
|
|
|
{ |
273
|
1 |
|
array_push($this->rel, $relation); |
274
|
1 |
|
} |
275
|
|
|
|
276
|
2 |
|
public function setPrinting($printing) |
277
|
|
|
{ |
278
|
2 |
|
$this->printing = $printing; |
279
|
|
|
|
280
|
2 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
1 |
|
public function getPrinting() |
284
|
|
|
{ |
285
|
1 |
|
return $this->printing; |
286
|
|
|
} |
287
|
|
|
|
288
|
2 |
|
public function setPage(int $page) |
289
|
|
|
{ |
290
|
2 |
|
$this->page = $page; |
291
|
|
|
|
292
|
2 |
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
public function getPage() :int |
296
|
|
|
{ |
297
|
1 |
|
return $this->page; |
298
|
|
|
} |
299
|
|
|
|
300
|
2 |
|
public function setPageLength($pageLength) |
301
|
|
|
{ |
302
|
2 |
|
$this->pageLength = $pageLength; |
303
|
|
|
|
304
|
2 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
1 |
|
public function getPageLength() |
308
|
|
|
{ |
309
|
1 |
|
return $this->pageLength; |
310
|
|
|
} |
311
|
|
|
|
312
|
2 |
|
public function setSelect($select) : QueryBuilderFactory |
313
|
|
|
{ |
314
|
2 |
|
$this->select = $select; |
315
|
|
|
|
316
|
2 |
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
public function getSelect() |
320
|
|
|
{ |
321
|
1 |
|
return $this->select; |
322
|
|
|
} |
323
|
|
|
|
324
|
1 |
|
public function getEntityManager() : EntityManager |
325
|
|
|
{ |
326
|
1 |
|
return $this->manager; |
327
|
|
|
} |
328
|
|
|
|
329
|
2 |
|
public function ensureQueryBuilderIsDefined() |
330
|
|
|
{ |
331
|
2 |
|
if (!$this->qBuilder) { |
332
|
1 |
|
throw new \RuntimeException( |
333
|
|
|
'Oops! QueryBuilder was never initialized. ' |
334
|
|
|
. "\n" . 'QueryBuilderFactory::createQueryBuilder()' |
335
|
1 |
|
. "\n" . 'QueryBuilderFactory::createSelectAndGroupBy()' |
336
|
|
|
); |
337
|
|
|
} |
338
|
1 |
|
} |
339
|
|
|
} |
340
|
|
|
|