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