1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\DoctrineQueryFilter; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Enum\JoinConditionType; |
8
|
|
|
use Arp\DoctrineQueryFilter\Enum\OrderByDirection; |
9
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Doctrine\ORM\Query; |
12
|
|
|
use Doctrine\ORM\Query\Expr; |
13
|
|
|
use Doctrine\ORM\Query\Expr\Comparison; |
14
|
|
|
use Doctrine\ORM\Query\Expr\Composite; |
15
|
|
|
use Doctrine\ORM\Query\Expr\OrderBy; |
16
|
|
|
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; |
17
|
|
|
|
18
|
|
|
final class QueryBuilder implements QueryBuilderInterface |
19
|
|
|
{ |
20
|
|
|
public function __construct( |
21
|
|
|
private readonly DoctrineQueryBuilder $queryBuilder, |
22
|
|
|
private string $rootAlias = '' |
23
|
|
|
) { |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createQueryBuilder(): QueryBuilderInterface |
27
|
|
|
{ |
28
|
|
|
return new self($this->getEntityManager()->createQueryBuilder()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getEntityManager(): EntityManagerInterface |
32
|
|
|
{ |
33
|
|
|
return $this->queryBuilder->getEntityManager(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getRootAlias(): string |
37
|
|
|
{ |
38
|
|
|
if (!empty($this->rootAlias)) { |
39
|
|
|
return $this->rootAlias; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->queryBuilder->getRootAliases()[0] ?? ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setRootAlias(string $rootAlias): void |
46
|
|
|
{ |
47
|
|
|
$this->rootAlias = $rootAlias; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getQuery(): Query |
51
|
|
|
{ |
52
|
|
|
return $this->queryBuilder->getQuery(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getWrappedQueryBuilder(): DoctrineQueryBuilder |
56
|
|
|
{ |
57
|
|
|
return $this->queryBuilder; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function expr(): Expr |
61
|
|
|
{ |
62
|
|
|
return $this->queryBuilder->expr(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array<string, mixed> |
67
|
|
|
*/ |
68
|
|
|
public function getQueryParts(): array |
69
|
|
|
{ |
70
|
|
|
return $this->queryBuilder->getDQLParts(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function orWhere(mixed ...$args): QueryBuilderInterface |
74
|
|
|
{ |
75
|
|
|
$this->queryBuilder->orWhere(...$args); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function andWhere(mixed ...$args): QueryBuilderInterface |
81
|
|
|
{ |
82
|
|
|
$this->queryBuilder->andWhere(...$args); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @param string $alias |
90
|
|
|
* @param JoinConditionType|null $conditionType |
91
|
|
|
* @param string|Comparison|Composite|null $condition |
92
|
|
|
* @param string|null $indexBy |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
*/ |
96
|
|
|
public function innerJoin( |
97
|
|
|
string $name, |
98
|
|
|
string $alias, |
99
|
|
|
?JoinConditionType $conditionType = null, |
100
|
|
|
mixed $condition = null, |
101
|
|
|
?string $indexBy = null |
102
|
|
|
): self { |
103
|
|
|
$conditionType ??= JoinConditionType::WITH; |
104
|
|
|
$this->queryBuilder->innerJoin($name, $alias, $conditionType->value, $condition, $indexBy); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param string $alias |
112
|
|
|
* @param JoinConditionType|null $conditionType |
113
|
|
|
* @param string|Comparison|Composite|null $condition |
114
|
|
|
* @param string|null $indexBy |
115
|
|
|
* |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function leftJoin( |
119
|
|
|
string $name, |
120
|
|
|
string $alias, |
121
|
|
|
?JoinConditionType $conditionType = null, |
122
|
|
|
mixed $condition = null, |
123
|
|
|
?string $indexBy = null |
124
|
|
|
): self { |
125
|
|
|
$conditionType ??= JoinConditionType::WITH; |
126
|
|
|
$this->queryBuilder->leftJoin($name, $alias, $conditionType->value, $condition, $indexBy); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function orderBy(Expr\OrderBy|string $sort, ?OrderByDirection $direction = null): self |
132
|
|
|
{ |
133
|
|
|
if ($sort instanceof Expr\OrderBy) { |
134
|
|
|
$direction = null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->queryBuilder->orderBy($sort, $direction?->value); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function addOrderBy(OrderBy|string $sort, ?OrderByDirection $direction = null): self |
143
|
|
|
{ |
144
|
|
|
if ($sort instanceof Expr\OrderBy) { |
145
|
|
|
$direction = null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->queryBuilder->addOrderBy($sort, $direction?->value); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return ArrayCollection<int, Query\Parameter> |
155
|
|
|
*/ |
156
|
|
|
public function getParameters(): ArrayCollection |
157
|
|
|
{ |
158
|
|
|
return $this->queryBuilder->getParameters(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setParameters(ArrayCollection $parameters): self |
162
|
|
|
{ |
163
|
|
|
$this->queryBuilder->setParameters($parameters); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setParameter(string $name, mixed $value, ?string $type = null): self |
169
|
|
|
{ |
170
|
|
|
$this->queryBuilder->setParameter($name, $value, $type); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function mergeParameters(QueryBuilderInterface $queryBuilder): self |
176
|
|
|
{ |
177
|
|
|
$parameters = $this->getParameters(); |
178
|
|
|
foreach ($queryBuilder->getParameters() as $parameter) { |
179
|
|
|
$parameters->add($parameter); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|