1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Grid\DataSource\Doctrine\MongoDB; |
13
|
|
|
|
14
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
15
|
|
|
use Doctrine\ODM\MongoDB\Query\Expr; |
16
|
|
|
use Lug\Component\Grid\DataSource\ExpressionBuilderInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ExpressionBuilder implements ExpressionBuilderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Builder |
25
|
|
|
*/ |
26
|
|
|
private $queryBuilder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Builder $queryBuilder |
30
|
|
|
*/ |
31
|
27 |
|
public function __construct(Builder $queryBuilder) |
32
|
|
|
{ |
33
|
27 |
|
$this->queryBuilder = $queryBuilder; |
34
|
27 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function andX(array $expressions) |
40
|
|
|
{ |
41
|
2 |
|
$expr = $this->expr(); |
42
|
|
|
|
43
|
2 |
|
foreach ($expressions as $expression) { |
44
|
2 |
|
$expr->addAnd($expression); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
return $expr; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
2 |
|
public function orX(array $expressions) |
54
|
|
|
{ |
55
|
2 |
|
$expr = $this->expr(); |
56
|
|
|
|
57
|
2 |
|
foreach ($expressions as $expression) { |
58
|
2 |
|
$expr->addOr($expression); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
2 |
|
return $expr; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function asc($x) |
68
|
|
|
{ |
69
|
1 |
|
return $this->expr()->sort($x, 'ASC'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function desc($x) |
76
|
|
|
{ |
77
|
1 |
|
return $this->expr()->sort($x, 'DESC'); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
6 |
|
public function eq($x, $y) |
84
|
|
|
{ |
85
|
6 |
|
return $this->field($x)->equals($y); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
public function neq($x, $y) |
92
|
|
|
{ |
93
|
2 |
|
return $this->field($x)->notEqual($y); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
public function lt($x, $y) |
100
|
|
|
{ |
101
|
2 |
|
return $this->field($x)->lt($y); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
2 |
|
public function lte($x, $y) |
108
|
|
|
{ |
109
|
2 |
|
return $this->field($x)->lte($y); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
2 |
|
public function gt($x, $y) |
116
|
|
|
{ |
117
|
2 |
|
return $this->field($x)->gt($y); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
2 |
|
public function gte($x, $y) |
124
|
|
|
{ |
125
|
2 |
|
return $this->field($x)->gte($y); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function exists($x) |
132
|
|
|
{ |
133
|
1 |
|
return $this->field($x)->exists(true); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
1 |
|
public function in($x, $y) |
140
|
|
|
{ |
141
|
1 |
|
return $this->field($x)->in($y); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
1 |
|
public function notIn($x, $y) |
148
|
|
|
{ |
149
|
1 |
|
return $this->field($x)->notIn($y); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
1 |
|
public function isNull($x) |
156
|
|
|
{ |
157
|
1 |
|
return $this->eq($x, null); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
1 |
|
public function isNotNull($x) |
164
|
|
|
{ |
165
|
1 |
|
return $this->neq($x, null); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
4 |
|
public function like($x, $y) |
172
|
|
|
{ |
173
|
4 |
|
return $this->eq($x, $this->convertLikeToRegex($y)); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
4 |
|
public function notLike($x, $y) |
180
|
|
|
{ |
181
|
4 |
|
return $this->field($x)->not($this->convertLikeToRegex($y)); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
1 |
|
public function between($value, $x, $y) |
188
|
|
|
{ |
189
|
1 |
|
return $this->andX([ |
|
|
|
|
190
|
1 |
|
$this->gte($value, $x), |
191
|
1 |
|
$this->lte($value, $y), |
192
|
1 |
|
]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
1 |
|
public function notBetween($value, $x, $y) |
199
|
|
|
{ |
200
|
1 |
|
return $this->orX([ |
|
|
|
|
201
|
1 |
|
$this->lt($value, $x), |
202
|
1 |
|
$this->gt($value, $y), |
203
|
1 |
|
]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $field |
208
|
|
|
* |
209
|
|
|
* @return Expr |
210
|
|
|
*/ |
211
|
21 |
|
private function field($field) |
212
|
|
|
{ |
213
|
21 |
|
return $this->expr()->field($field); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Expr |
218
|
|
|
*/ |
219
|
25 |
|
private function expr() |
220
|
|
|
{ |
221
|
25 |
|
return $this->queryBuilder->expr(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $x |
226
|
|
|
* |
227
|
|
|
* @return \MongoRegex |
228
|
|
|
*/ |
229
|
8 |
|
private function convertLikeToRegex($x) |
230
|
|
|
{ |
231
|
8 |
|
$x = preg_quote($x); |
232
|
8 |
|
$pattern = '.*?'; |
233
|
8 |
|
$regex = null; |
234
|
|
|
|
235
|
8 |
|
if (strpos($x, $like = '%') === 0) { |
236
|
4 |
|
$regex .= $pattern; |
237
|
4 |
|
$x = substr($x, 1); |
238
|
4 |
|
} |
239
|
|
|
|
240
|
8 |
|
if (strrpos($x, $like) === strlen($x) - 1) { |
241
|
4 |
|
$regex .= substr($x, 0, -1).$pattern; |
242
|
4 |
|
} else { |
243
|
4 |
|
$regex .= $x; |
244
|
|
|
} |
245
|
|
|
|
246
|
8 |
|
return new \MongoRegex('/^'.$regex.'$/'); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
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.