|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\DoctrineQueryFilter; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\ORM\Query; |
|
10
|
|
|
use Doctrine\ORM\Query\Expr; |
|
11
|
|
|
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Alex Patterson <[email protected]> |
|
15
|
|
|
* @package Arp\DoctrineQueryFilter\Query |
|
16
|
|
|
*/ |
|
17
|
|
|
final class QueryBuilder implements QueryBuilderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private DoctrineQueryBuilder $queryBuilder; |
|
20
|
|
|
|
|
21
|
|
|
private string $rootAlias = ''; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param DoctrineQueryBuilder $queryBuilder |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(DoctrineQueryBuilder $queryBuilder) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->queryBuilder = $queryBuilder; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return QueryBuilderInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
public function createQueryBuilder(): QueryBuilderInterface |
|
35
|
|
|
{ |
|
36
|
|
|
return new self($this->getEntityManager()->createQueryBuilder()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return EntityManagerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getEntityManager(): EntityManagerInterface |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->queryBuilder->getEntityManager(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getRootAlias(): string |
|
51
|
|
|
{ |
|
52
|
|
|
if (!empty($this->rootAlias)) { |
|
53
|
|
|
return $this->rootAlias; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->queryBuilder->getRootAliases()[0] ?? ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $rootAlias |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setRootAlias(string $rootAlias): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->rootAlias = $rootAlias; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Query |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getQuery(): Query |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->queryBuilder->getQuery(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the wrapped Doctrine query builder instance |
|
77
|
|
|
* |
|
78
|
|
|
* @return DoctrineQueryBuilder |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getWrappedQueryBuilder(): DoctrineQueryBuilder |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->queryBuilder; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Expr |
|
87
|
|
|
*/ |
|
88
|
|
|
public function expr(): Expr |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->queryBuilder->expr(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array<mixed> |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getQueryParts(): array |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->queryBuilder->getDQLParts(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed ...$args |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function orWhere(...$args): QueryBuilderInterface |
|
107
|
|
|
{ |
|
108
|
|
|
$this->queryBuilder->orWhere(...$args); |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param mixed ...$args |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function andWhere(...$args): QueryBuilderInterface |
|
119
|
|
|
{ |
|
120
|
|
|
$this->queryBuilder->andWhere(...$args); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* @param string $alias |
|
128
|
|
|
* @param string $type |
|
129
|
|
|
* @param string|null $condition |
|
130
|
|
|
* @param string|null $indexBy |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function innerJoin( |
|
135
|
|
|
string $name, |
|
136
|
|
|
string $alias, |
|
137
|
|
|
string $type, |
|
138
|
|
|
$condition = null, |
|
139
|
|
|
string $indexBy = null |
|
140
|
|
|
): QueryBuilderInterface { |
|
141
|
|
|
$this->queryBuilder->innerJoin($name, $alias, $type, $condition, $indexBy); |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $name |
|
148
|
|
|
* @param string $alias |
|
149
|
|
|
* @param string $type |
|
150
|
|
|
* @param string|null $condition |
|
151
|
|
|
* @param string|null $indexBy |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
|
|
public function leftJoin( |
|
156
|
|
|
string $name, |
|
157
|
|
|
string $alias, |
|
158
|
|
|
string $type, |
|
159
|
|
|
$condition = null, |
|
160
|
|
|
string $indexBy = null |
|
161
|
|
|
): QueryBuilderInterface { |
|
162
|
|
|
$this->queryBuilder->leftJoin($name, $alias, $type, $condition, $indexBy); |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param Expr\OrderBy|string $sort |
|
169
|
|
|
* @param string|null $direction |
|
170
|
|
|
* |
|
171
|
|
|
* @return QueryBuilderInterface |
|
172
|
|
|
*/ |
|
173
|
|
|
public function orderBy($sort, ?string $direction = null): QueryBuilderInterface |
|
174
|
|
|
{ |
|
175
|
|
|
if ($sort instanceof Expr\OrderBy) { |
|
176
|
|
|
$direction = null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$this->queryBuilder->orderBy($sort, $direction); |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param Expr\OrderBy|string $sort |
|
186
|
|
|
* @param string|null $direction |
|
187
|
|
|
* |
|
188
|
|
|
* @return QueryBuilderInterface |
|
189
|
|
|
*/ |
|
190
|
|
|
public function addOrderBy($sort, ?string $direction = null): QueryBuilderInterface |
|
191
|
|
|
{ |
|
192
|
|
|
if ($sort instanceof Expr\OrderBy) { |
|
193
|
|
|
$direction = null; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$this->queryBuilder->addOrderBy($sort, $direction); |
|
197
|
|
|
|
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return ArrayCollection<int, Query\Parameter> |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getParameters(): ArrayCollection |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->queryBuilder->getParameters(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param ArrayCollection<int, Query\Parameter> $parameters |
|
211
|
|
|
* |
|
212
|
|
|
* @return $this|QueryBuilderInterface |
|
213
|
|
|
*/ |
|
214
|
|
|
public function setParameters(ArrayCollection $parameters): QueryBuilderInterface |
|
215
|
|
|
{ |
|
216
|
|
|
$this->queryBuilder->setParameters($parameters); |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param string $name |
|
223
|
|
|
* @param mixed $value |
|
224
|
|
|
* @param string|null $type |
|
225
|
|
|
* |
|
226
|
|
|
* @return $this|QueryBuilderInterface |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setParameter(string $name, $value, ?string $type = null): QueryBuilderInterface |
|
229
|
|
|
{ |
|
230
|
|
|
$this->queryBuilder->setParameter($name, $value, $type); |
|
231
|
|
|
|
|
232
|
|
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param QueryBuilderInterface $queryBuilder |
|
237
|
|
|
* |
|
238
|
|
|
* @return $this|QueryBuilderInterface |
|
239
|
|
|
*/ |
|
240
|
|
|
public function mergeParameters(QueryBuilderInterface $queryBuilder): QueryBuilderInterface |
|
241
|
|
|
{ |
|
242
|
|
|
$parameters = $this->getParameters(); |
|
243
|
|
|
foreach ($queryBuilder->getParameters() as $parameter) { |
|
244
|
|
|
$parameters->add($parameter); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|