|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Persistence\Doctrine; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface; |
|
18
|
|
|
use Acme\App\Core\Port\Persistence\QueryInterface; |
|
19
|
|
|
use Acme\PhpExtension\Helper\ClassHelper; |
|
20
|
|
|
use Doctrine\ORM\AbstractQuery; |
|
21
|
|
|
|
|
22
|
|
|
final class DqlQueryBuilder implements DqlQueryBuilderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $filters = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $hydrationMode = AbstractQuery::HYDRATE_OBJECT; |
|
33
|
|
|
|
|
34
|
|
|
public function build(): QueryInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$dqlQuery = new DqlQuery($this->filters); |
|
37
|
|
|
$dqlQuery->setHydrationMode($this->hydrationMode); |
|
38
|
|
|
|
|
39
|
|
|
return $dqlQuery; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function create(string $entityName, string $alias = null, string $indexBy = null): DqlQueryBuilderInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$alias = $alias ?? ClassHelper::extractCanonicalClassName($entityName); |
|
45
|
|
|
|
|
46
|
|
|
$this->reset(); |
|
47
|
|
|
|
|
48
|
|
|
return $this->select($alias)->from($entityName, $alias, $indexBy)->setMaxResults(self::DEFAULT_MAX_RESULTS); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function setParameter($key, $value, $type = null): DqlQueryBuilderInterface |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function setMaxResults(int $maxResults): DqlQueryBuilderInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function select(string ...$select): DqlQueryBuilderInterface |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function distinct(bool $flag = true): DqlQueryBuilderInterface |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function addSelect(string ...$select): DqlQueryBuilderInterface |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function delete(string $delete = null, string $alias = null): DqlQueryBuilderInterface |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function update(string $update = null, string $alias = null): DqlQueryBuilderInterface |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function from(string $from, string $alias, string $indexBy = null): DqlQueryBuilderInterface |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function indexBy(string $alias, string $indexBy): DqlQueryBuilderInterface |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function join( |
|
97
|
|
|
string $join, |
|
98
|
|
|
string $alias, |
|
99
|
|
|
string $conditionType = null, |
|
100
|
|
|
string $condition = null, |
|
101
|
|
|
string $indexBy = null |
|
102
|
|
|
): DqlQueryBuilderInterface { |
|
103
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function innerJoin( |
|
107
|
|
|
string $join, |
|
108
|
|
|
string $alias, |
|
109
|
|
|
string $conditionType = null, |
|
110
|
|
|
string $condition = null, |
|
111
|
|
|
string $indexBy = null |
|
112
|
|
|
): DqlQueryBuilderInterface { |
|
113
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function leftJoin( |
|
117
|
|
|
string $join, |
|
118
|
|
|
string $alias, |
|
119
|
|
|
string $conditionType = null, |
|
120
|
|
|
string $condition = null, |
|
121
|
|
|
string $indexBy = null |
|
122
|
|
|
): DqlQueryBuilderInterface { |
|
123
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function where(string $predicates): DqlQueryBuilderInterface |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function andWhere(string $predicates): DqlQueryBuilderInterface |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function orWhere(string $predicates): DqlQueryBuilderInterface |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function groupBy(string $groupBy): DqlQueryBuilderInterface |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function addGroupBy(string $groupBy): DqlQueryBuilderInterface |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function having(string $having): DqlQueryBuilderInterface |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function andHaving(string $having): DqlQueryBuilderInterface |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function orHaving(string $having): DqlQueryBuilderInterface |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function orderBy(string $sort, string $order = null): DqlQueryBuilderInterface |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function addOrderBy(string $sort, string $order = null): DqlQueryBuilderInterface |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->addFilter(__METHOD__, \func_get_args()); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function useScalarHydration(): DqlQueryBuilderInterface |
|
177
|
|
|
{ |
|
178
|
|
|
$this->hydrationMode = AbstractQuery::HYDRATE_SCALAR; |
|
179
|
|
|
|
|
180
|
|
|
return $this; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
private function addFilter(string $methodName, array $args): DqlQueryBuilderInterface |
|
184
|
|
|
{ |
|
185
|
|
|
$this->filters[] = [$methodName, $args]; |
|
186
|
|
|
|
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function reset(): void |
|
191
|
|
|
{ |
|
192
|
|
|
$this->filters = []; |
|
193
|
|
|
$this->hydrationMode = AbstractQuery::HYDRATE_OBJECT; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.