|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\DoctrineSpecification; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\DoctrineSpecification\Filter\Comparison; |
|
6
|
|
|
use Happyr\DoctrineSpecification\Filter\Filter; |
|
7
|
|
|
use Happyr\DoctrineSpecification\Filter\In; |
|
8
|
|
|
use Happyr\DoctrineSpecification\Filter\InstanceOfX; |
|
9
|
|
|
use Happyr\DoctrineSpecification\Filter\IsNotNull; |
|
10
|
|
|
use Happyr\DoctrineSpecification\Filter\IsNull; |
|
11
|
|
|
use Happyr\DoctrineSpecification\Filter\Like; |
|
12
|
|
|
use Happyr\DoctrineSpecification\Logic\AndX; |
|
13
|
|
|
use Happyr\DoctrineSpecification\Logic\Not; |
|
14
|
|
|
use Happyr\DoctrineSpecification\Logic\OrX; |
|
15
|
|
|
use Happyr\DoctrineSpecification\Query\GroupBy; |
|
16
|
|
|
use Happyr\DoctrineSpecification\Query\InnerJoin; |
|
17
|
|
|
use Happyr\DoctrineSpecification\Query\Join; |
|
18
|
|
|
use Happyr\DoctrineSpecification\Query\LeftJoin; |
|
19
|
|
|
use Happyr\DoctrineSpecification\Query\Limit; |
|
20
|
|
|
use Happyr\DoctrineSpecification\Query\Offset; |
|
21
|
|
|
use Happyr\DoctrineSpecification\Query\OrderBy; |
|
22
|
|
|
use Happyr\DoctrineSpecification\Query\Slice; |
|
23
|
|
|
use Happyr\DoctrineSpecification\Result\AsArray; |
|
24
|
|
|
use Happyr\DoctrineSpecification\Result\AsSingleScalar; |
|
25
|
|
|
use Happyr\DoctrineSpecification\Result\Cache; |
|
26
|
|
|
use Happyr\DoctrineSpecification\Specification\CountOf; |
|
27
|
|
|
use Happyr\DoctrineSpecification\Specification\Having; |
|
28
|
|
|
use Happyr\DoctrineSpecification\Specification\Specification; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Factory class for the specifications. |
|
32
|
|
|
*/ |
|
33
|
|
|
class Spec |
|
34
|
|
|
{ |
|
35
|
|
|
/* |
|
36
|
|
|
* Logic |
|
37
|
|
|
*/ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return AndX |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function andX() |
|
43
|
|
|
{ |
|
44
|
|
|
$args = func_get_args(); |
|
45
|
|
|
$reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\AndX'); |
|
46
|
|
|
|
|
47
|
|
|
return $reflection->newInstanceArgs($args); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return OrX |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function orX() |
|
54
|
|
|
{ |
|
55
|
|
|
$args = func_get_args(); |
|
56
|
|
|
$reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\OrX'); |
|
57
|
|
|
|
|
58
|
|
|
return $reflection->newInstanceArgs($args); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Filter $spec |
|
63
|
|
|
* |
|
64
|
|
|
* @return Not |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function not(Filter $spec) |
|
67
|
|
|
{ |
|
68
|
|
|
return new Not($spec); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/* |
|
72
|
|
|
* Query modifier |
|
73
|
|
|
*/ |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $field |
|
77
|
|
|
* @param string $newAlias |
|
78
|
|
|
* @param string $dqlAlias |
|
79
|
|
|
* |
|
80
|
|
|
* @return Join |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function join($field, $newAlias, $dqlAlias = null) |
|
83
|
|
|
{ |
|
84
|
|
|
return new Join($field, $newAlias, $dqlAlias); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $field |
|
89
|
|
|
* @param string $newAlias |
|
90
|
|
|
* @param string $dqlAlias |
|
91
|
|
|
* |
|
92
|
|
|
* @return LeftJoin |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function leftJoin($field, $newAlias, $dqlAlias = null) |
|
95
|
|
|
{ |
|
96
|
|
|
return new LeftJoin($field, $newAlias, $dqlAlias); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $field |
|
101
|
|
|
* @param string $newAlias |
|
102
|
|
|
* @param string $dqlAlias |
|
103
|
|
|
* |
|
104
|
|
|
* @return InnerJoin |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function innerJoin($field, $newAlias, $dqlAlias = null) |
|
107
|
|
|
{ |
|
108
|
|
|
return new InnerJoin($field, $newAlias, $dqlAlias); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param int $count |
|
113
|
|
|
* |
|
114
|
|
|
* @return Limit |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function limit($count) |
|
117
|
|
|
{ |
|
118
|
|
|
return new Limit($count); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param int $count |
|
123
|
|
|
* |
|
124
|
|
|
* @return Offset |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function offset($count) |
|
127
|
|
|
{ |
|
128
|
|
|
return new Offset($count); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param int $sliceSize |
|
133
|
|
|
* @param int $sliceNumber |
|
134
|
|
|
* |
|
135
|
|
|
* @return Slice |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function slice($sliceSize, $sliceNumber = 0) |
|
138
|
|
|
{ |
|
139
|
|
|
return new Slice($sliceSize, $sliceNumber); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $field |
|
144
|
|
|
* @param string $order |
|
145
|
|
|
* @param string|null $dqlAlias |
|
146
|
|
|
* |
|
147
|
|
|
* @return OrderBy |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
|
150
|
|
|
{ |
|
151
|
|
|
return new OrderBy($field, $order, $dqlAlias); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $field |
|
156
|
|
|
* @param string $dqlAlias |
|
157
|
|
|
* |
|
158
|
|
|
* @return GroupBy |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function groupBy($field, $dqlAlias = null) |
|
161
|
|
|
{ |
|
162
|
|
|
return new GroupBy($field, $dqlAlias); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/* |
|
166
|
|
|
* Result modifier |
|
167
|
|
|
*/ |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return AsArray |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function asArray() |
|
173
|
|
|
{ |
|
174
|
|
|
return new AsArray(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return AsSingleScalar |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function asSingleScalar() |
|
181
|
|
|
{ |
|
182
|
|
|
return new AsSingleScalar(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param int $cacheLifetime How many seconds the cached entry is valid |
|
187
|
|
|
* |
|
188
|
|
|
* @return Cache |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function cache($cacheLifetime) |
|
191
|
|
|
{ |
|
192
|
|
|
return new Cache($cacheLifetime); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/* |
|
196
|
|
|
* Filters |
|
197
|
|
|
*/ |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param string $field |
|
201
|
|
|
* @param string|null $dqlAlias |
|
202
|
|
|
* |
|
203
|
|
|
* @return IsNull |
|
204
|
|
|
*/ |
|
205
|
|
|
public static function isNull($field, $dqlAlias = null) |
|
206
|
|
|
{ |
|
207
|
|
|
return new IsNull($field, $dqlAlias); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param string $field |
|
212
|
|
|
* @param string|null $dqlAlias |
|
213
|
|
|
* |
|
214
|
|
|
* @return IsNotNull |
|
215
|
|
|
*/ |
|
216
|
|
|
public static function isNotNull($field, $dqlAlias = null) |
|
217
|
|
|
{ |
|
218
|
|
|
return new IsNotNull($field, $dqlAlias); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Make sure the $field has a value equals to $value. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $field |
|
225
|
|
|
* @param mixed $value |
|
226
|
|
|
* @param string $dqlAlias |
|
227
|
|
|
* |
|
228
|
|
|
* @return In |
|
229
|
|
|
*/ |
|
230
|
|
|
public static function in($field, $value, $dqlAlias = null) |
|
231
|
|
|
{ |
|
232
|
|
|
return new In($field, $value, $dqlAlias); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param string $field |
|
237
|
|
|
* @param mixed $value |
|
238
|
|
|
* @param string $dqlAlias |
|
239
|
|
|
* |
|
240
|
|
|
* @return Not |
|
241
|
|
|
*/ |
|
242
|
|
|
public static function notIn($field, $value, $dqlAlias = null) |
|
243
|
|
|
{ |
|
244
|
|
|
return new Not(new In($field, $value, $dqlAlias)); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $field |
|
249
|
|
|
* @param string $value |
|
250
|
|
|
* @param string $dqlAlias |
|
251
|
|
|
* |
|
252
|
|
|
* @return Comparison |
|
253
|
|
|
*/ |
|
254
|
|
|
public static function eq($field, $value, $dqlAlias = null) |
|
255
|
|
|
{ |
|
256
|
|
|
return new Comparison(Comparison::EQ, $field, $value, $dqlAlias); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param string $field |
|
261
|
|
|
* @param string $value |
|
262
|
|
|
* @param string $dqlAlias |
|
263
|
|
|
* |
|
264
|
|
|
* @return Comparison |
|
265
|
|
|
*/ |
|
266
|
|
|
public static function neq($field, $value, $dqlAlias = null) |
|
267
|
|
|
{ |
|
268
|
|
|
return new Comparison(Comparison::NEQ, $field, $value, $dqlAlias); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param string $field |
|
273
|
|
|
* @param string $value |
|
274
|
|
|
* @param string $dqlAlias |
|
275
|
|
|
* |
|
276
|
|
|
* @return Comparison |
|
277
|
|
|
*/ |
|
278
|
|
|
public static function lt($field, $value, $dqlAlias = null) |
|
279
|
|
|
{ |
|
280
|
|
|
return new Comparison(Comparison::LT, $field, $value, $dqlAlias); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param string $field |
|
285
|
|
|
* @param string $value |
|
286
|
|
|
* @param string $dqlAlias |
|
287
|
|
|
* |
|
288
|
|
|
* @return Comparison |
|
289
|
|
|
*/ |
|
290
|
|
|
public static function lte($field, $value, $dqlAlias = null) |
|
291
|
|
|
{ |
|
292
|
|
|
return new Comparison(Comparison::LTE, $field, $value, $dqlAlias); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param string $field |
|
297
|
|
|
* @param string $value |
|
298
|
|
|
* @param string $dqlAlias |
|
299
|
|
|
* |
|
300
|
|
|
* @return Comparison |
|
301
|
|
|
*/ |
|
302
|
|
|
public static function gt($field, $value, $dqlAlias = null) |
|
303
|
|
|
{ |
|
304
|
|
|
return new Comparison(Comparison::GT, $field, $value, $dqlAlias); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param string $field |
|
309
|
|
|
* @param string $value |
|
310
|
|
|
* @param string $dqlAlias |
|
311
|
|
|
* |
|
312
|
|
|
* @return Comparison |
|
313
|
|
|
*/ |
|
314
|
|
|
public static function gte($field, $value, $dqlAlias = null) |
|
315
|
|
|
{ |
|
316
|
|
|
return new Comparison(Comparison::GTE, $field, $value, $dqlAlias); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @param string $field |
|
321
|
|
|
* @param string $value |
|
322
|
|
|
* @param string $format |
|
323
|
|
|
* @param string $dqlAlias |
|
324
|
|
|
* |
|
325
|
|
|
* @return Like |
|
326
|
|
|
*/ |
|
327
|
|
|
public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
|
328
|
|
|
{ |
|
329
|
|
|
return new Like($field, $value, $format, $dqlAlias); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @param string $value |
|
334
|
|
|
* @param null $dqlAlias |
|
335
|
|
|
* |
|
336
|
|
|
* @return InstanceOfX |
|
337
|
|
|
*/ |
|
338
|
|
|
public static function instanceOfX($value, $dqlAlias = null) |
|
339
|
|
|
{ |
|
340
|
|
|
return new InstanceOfX($value, $dqlAlias); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/* |
|
344
|
|
|
* Specifications |
|
345
|
|
|
*/ |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param Specification $spec |
|
349
|
|
|
* |
|
350
|
|
|
* @return CountOf |
|
351
|
|
|
*/ |
|
352
|
|
|
public static function countOf(Specification $spec) |
|
353
|
|
|
{ |
|
354
|
|
|
return new CountOf($spec); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @param Filter|string $spec |
|
359
|
|
|
* |
|
360
|
|
|
* @return Having |
|
361
|
|
|
*/ |
|
362
|
|
|
public static function having($spec) |
|
363
|
|
|
{ |
|
364
|
|
|
return new Having($spec); |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|