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