1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\GridBundle\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use Sylius\Component\Grid\Data\ExpressionBuilderInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ExpressionBuilder implements ExpressionBuilderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var QueryBuilder |
24
|
|
|
*/ |
25
|
|
|
private $queryBuilder; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param QueryBuilder $queryBuilder |
29
|
|
|
*/ |
30
|
|
|
public function __construct(QueryBuilder $queryBuilder) |
31
|
|
|
{ |
32
|
|
|
$this->queryBuilder = $queryBuilder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function andX(...$expressions) |
39
|
|
|
{ |
40
|
|
|
return $this->queryBuilder->expr()->andX(...$expressions); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function orX(...$expressions) |
47
|
|
|
{ |
48
|
|
|
return $this->queryBuilder->expr()->orX(...$expressions); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function comparison($field, $operator, $value) |
55
|
|
|
{ |
56
|
|
|
throw new \BadMethodCallException('Not supported yet.'); |
57
|
|
|
// TODO: Implement comparison() method. |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function equals($field, $value) |
64
|
|
|
{ |
65
|
|
|
$parameterName = $this->getParameterName($field); |
66
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
67
|
|
|
|
68
|
|
|
return $this->queryBuilder->expr()->eq($this->getFieldName($field), ':'.$parameterName); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function notEquals($field, $value) |
75
|
|
|
{ |
76
|
|
|
$parameterName = $this->getParameterName($field); |
77
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
78
|
|
|
|
79
|
|
|
return $this->queryBuilder->expr()->neq($this->getFieldName($field), ':'.$parameterName); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function lessThan($field, $value) |
86
|
|
|
{ |
87
|
|
|
$parameterName = $this->getParameterName($field); |
88
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
89
|
|
|
|
90
|
|
|
$this->queryBuilder->andWhere($this->getFieldName($field).' < :'.$parameterName); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function lessThanOrEqual($field, $value) |
97
|
|
|
{ |
98
|
|
|
$parameterName = $this->getParameterName($field); |
99
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
100
|
|
|
|
101
|
|
|
$this->queryBuilder->andWhere($this->getFieldName($field).' <= :'.$parameterName); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function greaterThan($field, $value) |
108
|
|
|
{ |
109
|
|
|
$parameterName = $this->getParameterName($field); |
110
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
111
|
|
|
|
112
|
|
|
$this->queryBuilder->andWhere($this->getFieldName($field).' > :'.$parameterName); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function greaterThanOrEqual($field, $value) |
119
|
|
|
{ |
120
|
|
|
$parameterName = $this->getParameterName($field); |
121
|
|
|
$this->queryBuilder->setParameter($parameterName, $value); |
122
|
|
|
|
123
|
|
|
$this->queryBuilder->andWhere($this->getFieldName($field).' >= :'.$parameterName); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function in($field, array $values) |
130
|
|
|
{ |
131
|
|
|
return $this->queryBuilder->expr()->in($this->getFieldName($field), $values); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function notIn($field, array $values) |
138
|
|
|
{ |
139
|
|
|
return $this->queryBuilder->expr()->notIn($this->getFieldName($field), $values); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function isNull($field) |
146
|
|
|
{ |
147
|
|
|
return $this->queryBuilder->expr()->isNull($this->getFieldName($field)); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function isNotNull($field) |
154
|
|
|
{ |
155
|
|
|
return $this->queryBuilder->expr()->isNotNull($this->getFieldName($field)); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function like($field, $pattern) |
162
|
|
|
{ |
163
|
|
|
return $this->queryBuilder->expr()->like($this->getFieldName($field), $this->queryBuilder->expr()->literal($pattern)); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function notLike($field, $pattern) |
170
|
|
|
{ |
171
|
|
|
return $this->queryBuilder->expr()->notLike($this->getFieldName($field), $this->queryBuilder->expr()->literal($pattern)); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
|
|
public function orderBy($field, $direction) |
178
|
|
|
{ |
179
|
|
|
return $this->queryBuilder->orderBy($this->getFieldName($field), $direction); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function addOrderBy($field, $direction) |
186
|
|
|
{ |
187
|
|
|
return $this->queryBuilder->addOrderBy($this->getFieldName($field), $direction); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
private function getFieldName($field) |
194
|
|
|
{ |
195
|
|
|
if (false === strpos($field, '.')) { |
196
|
|
|
return $this->queryBuilder->getRootAlias().'.'.$field; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $field; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $field |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
private function getParameterName($field) |
208
|
|
|
{ |
209
|
|
|
$parameterName = str_replace('.', '_', $field); |
210
|
|
|
|
211
|
|
|
$i = 1; |
212
|
|
|
while ($this->hasParameterName($parameterName)) { |
213
|
|
|
$parameterName .= $i; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $parameterName; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $parameterName |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
private function hasParameterName($parameterName) |
225
|
|
|
{ |
226
|
|
|
return null !== $this->queryBuilder->getParameter($parameterName); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.