Completed
Push — 1.0 ( 8e07ac...ea6409 )
by Peter
08:50
created

Spec::cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Operand\Addition;
16
use Happyr\DoctrineSpecification\Operand\Division;
17
use Happyr\DoctrineSpecification\Operand\Alias;
18
use Happyr\DoctrineSpecification\Operand\Field;
19
use Happyr\DoctrineSpecification\Operand\LikePattern;
20
use Happyr\DoctrineSpecification\Operand\Modulo;
21
use Happyr\DoctrineSpecification\Operand\Multiplication;
22
use Happyr\DoctrineSpecification\Operand\Operand;
23
use Happyr\DoctrineSpecification\Operand\Subtraction;
24
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
25
use Happyr\DoctrineSpecification\Operand\Value;
26
use Happyr\DoctrineSpecification\Operand\Values;
27
use Happyr\DoctrineSpecification\Query\AddSelect;
28
use Happyr\DoctrineSpecification\Query\GroupBy;
29
use Happyr\DoctrineSpecification\Query\InnerJoin;
30
use Happyr\DoctrineSpecification\Query\Join;
31
use Happyr\DoctrineSpecification\Query\LeftJoin;
32
use Happyr\DoctrineSpecification\Query\Limit;
33
use Happyr\DoctrineSpecification\Query\Offset;
34
use Happyr\DoctrineSpecification\Query\OrderBy;
35
use Happyr\DoctrineSpecification\Query\QueryModifier;
36
use Happyr\DoctrineSpecification\Query\Select;
37
use Happyr\DoctrineSpecification\Query\Selection\SelectAs;
38
use Happyr\DoctrineSpecification\Query\Selection\SelectEntity;
39
use Happyr\DoctrineSpecification\Query\Selection\SelectHiddenAs;
40
use Happyr\DoctrineSpecification\Query\Slice;
41
use Happyr\DoctrineSpecification\Result\AsArray;
42
use Happyr\DoctrineSpecification\Result\AsSingleScalar;
43
use Happyr\DoctrineSpecification\Result\Cache;
44
use Happyr\DoctrineSpecification\Result\RoundDateTime;
45
use Happyr\DoctrineSpecification\Specification\CountOf;
46
use Happyr\DoctrineSpecification\Specification\Having;
47
48
/**
49
 * Factory class for the specifications.
50
 *
51
 * @method static PlatformFunction CONCAT($str1, $str2)
52
 * @method static PlatformFunction SUBSTRING($str, $start, $length = null) Return substring of given string.
53
 * @method static PlatformFunction TRIM($str) Trim the string by the given trim char, defaults to whitespaces.
54
 * @method static PlatformFunction LOWER($str) Returns the string lowercased.
55
 * @method static PlatformFunction UPPER($str) Return the upper-case of the given string.
56
 * @method static PlatformFunction IDENTITY($expression, $fieldMapping = null) Retrieve the foreign key column of association of the owning side
57
 * @method static PlatformFunction LENGTH($str) Returns the length of the given string
58
 * @method static PlatformFunction LOCATE($needle, $haystack, $offset = 0) Locate the first occurrence of the substring in the string.
59
 * @method static PlatformFunction ABS($expression)
60
 * @method static PlatformFunction SQRT($q) Return the square-root of q.
61
 * @method static PlatformFunction MOD($a, $b) Return a MOD b.
62
 * @method static PlatformFunction SIZE($collection) Return the number of elements in the specified collection
63
 * @method static PlatformFunction DATE_DIFF($date1, $date2) Calculate the difference in days between date1-date2.
64
 * @method static PlatformFunction BIT_AND($a, $b)
65
 * @method static PlatformFunction BIT_OR($a, $b)
66
 * @method static PlatformFunction MIN($a)
67
 * @method static PlatformFunction MAX($a)
68
 * @method static PlatformFunction AVG($a)
69
 * @method static PlatformFunction SUM($a)
70
 * @method static PlatformFunction COUNT($a)
71
 * @method static PlatformFunction CURRENT_DATE() Return the current date
72
 * @method static PlatformFunction CURRENT_TIME() Returns the current time
73
 * @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
74
 * @method static PlatformFunction DATE_ADD($date, $days, $unit) Add the number of days to a given date. (Supported units are DAY, MONTH)
75
 * @method static PlatformFunction DATE_SUB($date, $days, $unit) Substract the number of days from a given date. (Supported units are DAY, MONTH)
76
 */
77
class Spec
78
{
79
    /**
80
     * @param string $name
81
     * @param array  $arguments
82
     *
83
     * @return PlatformFunction
84
     */
85
    public static function __callStatic($name, array $arguments = [])
86
    {
87
        return self::fun($name, $arguments);
88
    }
89
90
    /*
91
     * Logic
92
     */
93
94
    /**
95
     * @return AndX
96
     */
97
    public static function andX()
98
    {
99
        $args = func_get_args();
100
        $reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\AndX');
101
102
        return $reflection->newInstanceArgs($args);
103
    }
104
105
    /**
106
     * @return OrX
107
     */
108
    public static function orX()
109
    {
110
        $args = func_get_args();
111
        $reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\OrX');
112
113
        return $reflection->newInstanceArgs($args);
114
    }
115
116
    /**
117
     * @param Filter $spec
118
     *
119
     * @return Not
120
     */
121
    public static function not(Filter $spec)
122
    {
123
        return new Not($spec);
124
    }
125
126
    /*
127
     * Query modifier
128
     */
129
130
    /**
131
     * @param string $field
132
     * @param string $newAlias
133
     * @param string $dqlAlias
134
     *
135
     * @return Join
136
     */
137
    public static function join($field, $newAlias, $dqlAlias = null)
138
    {
139
        return new Join($field, $newAlias, $dqlAlias);
140
    }
141
142
    /**
143
     * @param string $field
144
     * @param string $newAlias
145
     * @param string $dqlAlias
146
     *
147
     * @return LeftJoin
148
     */
149
    public static function leftJoin($field, $newAlias, $dqlAlias = null)
150
    {
151
        return new LeftJoin($field, $newAlias, $dqlAlias);
152
    }
153
154
    /**
155
     * @param string $field
156
     * @param string $newAlias
157
     * @param string $dqlAlias
158
     *
159
     * @return InnerJoin
160
     */
161
    public static function innerJoin($field, $newAlias, $dqlAlias = null)
162
    {
163
        return new InnerJoin($field, $newAlias, $dqlAlias);
164
    }
165
166
    /**
167
     * @param int $count
168
     *
169
     * @return Limit
170
     */
171
    public static function limit($count)
172
    {
173
        return new Limit($count);
174
    }
175
176
    /**
177
     * @param int $count
178
     *
179
     * @return Offset
180
     */
181
    public static function offset($count)
182
    {
183
        return new Offset($count);
184
    }
185
186
    /**
187
     * @param int $sliceSize
188
     * @param int $sliceNumber
189
     *
190
     * @return Slice
191
     */
192
    public static function slice($sliceSize, $sliceNumber = 0)
193
    {
194
        return new Slice($sliceSize, $sliceNumber);
195
    }
196
197
    /**
198
     * @param string      $field
199
     * @param string      $order
200
     * @param string|null $dqlAlias
201
     *
202
     * @return OrderBy
203
     */
204
    public static function orderBy($field, $order = 'ASC', $dqlAlias = null)
205
    {
206
        return new OrderBy($field, $order, $dqlAlias);
207
    }
208
209
    /**
210
     * @param string $field
211
     * @param string $dqlAlias
212
     *
213
     * @return GroupBy
214
     */
215
    public static function groupBy($field, $dqlAlias = null)
216
    {
217
        return new GroupBy($field, $dqlAlias);
218
    }
219
220
    /*
221
     * Selection
222
     */
223
224
    /**
225
     * @param mixed $field
226
     *
227
     * @return Select
228
     */
229
    public static function select($field)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
230
    {
231
        return new Select(func_get_args());
232
    }
233
234
    /**
235
     * @param mixed $field
236
     *
237
     * @return AddSelect
238
     */
239
    public static function addSelect($field)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
240
    {
241
        return new AddSelect(func_get_args());
242
    }
243
244
    /**
245
     * @param string $dqlAlias
246
     *
247
     * @return SelectEntity
248
     */
249
    public static function selectEntity($dqlAlias)
250
    {
251
        return new SelectEntity($dqlAlias);
252
    }
253
254
    /**
255
     * @param Filter|Operand|string $expression
256
     * @param string                $alias
257
     *
258
     * @return SelectAs
259
     */
260
    public static function selectAs($expression, $alias)
261
    {
262
        return new SelectAs($expression, $alias);
263
    }
264
265
    /**
266
     * @param Filter|Operand|string $expression
267
     * @param string                $alias
268
     *
269
     * @return SelectHiddenAs
270
     */
271
    public static function selectHiddenAs($expression, $alias)
272
    {
273
        return new SelectHiddenAs($expression, $alias);
274
    }
275
276
    /*
277
     * Result modifier
278
     */
279
280
    /**
281
     * @return AsArray
282
     */
283
    public static function asArray()
284
    {
285
        return new AsArray();
286
    }
287
288
    /**
289
     * @return AsSingleScalar
290
     */
291
    public static function asSingleScalar()
292
    {
293
        return new AsSingleScalar();
294
    }
295
296
    /**
297
     * @param int $cacheLifetime How many seconds the cached entry is valid
298
     *
299
     * @return Cache
300
     */
301
    public static function cache($cacheLifetime)
302
    {
303
        return new Cache($cacheLifetime);
304
    }
305
306
    /**
307
     * @param int $roundSeconds How may seconds to round time
308
     *
309
     * @return RoundDateTime
310
     */
311
    public static function roundDateTimeParams($roundSeconds)
312
    {
313
        return new RoundDateTime($roundSeconds);
314
    }
315
316
    /*
317
     * Filters
318
     */
319
320
    /**
321
     * @param Operand|string $field
322
     * @param string|null    $dqlAlias
323
     *
324
     * @return IsNull
325
     */
326
    public static function isNull($field, $dqlAlias = null)
327
    {
328
        return new IsNull($field, $dqlAlias);
329
    }
330
331
    /**
332
     * @param Operand|string $field
333
     * @param string|null    $dqlAlias
334
     *
335
     * @return IsNotNull
336
     */
337
    public static function isNotNull($field, $dqlAlias = null)
338
    {
339
        return new IsNotNull($field, $dqlAlias);
340
    }
341
342
    /**
343
     * Make sure the $field has a value equals to $value.
344
     *
345
     * @param string $field
346
     * @param mixed  $value
347
     * @param string $dqlAlias
348
     *
349
     * @return In
350
     */
351
    public static function in($field, $value, $dqlAlias = null)
352
    {
353
        return new In($field, $value, $dqlAlias);
354
    }
355
356
    /**
357
     * @param string $field
358
     * @param mixed  $value
359
     * @param string $dqlAlias
360
     *
361
     * @return Not
362
     */
363
    public static function notIn($field, $value, $dqlAlias = null)
364
    {
365
        return new Not(new In($field, $value, $dqlAlias));
366
    }
367
368
    /**
369
     * @param Operand|string $field
370
     * @param Operand|mixed  $value
371
     * @param string|null    $dqlAlias
372
     *
373
     * @return Comparison
374
     */
375
    public static function eq($field, $value, $dqlAlias = null)
376
    {
377
        return new Comparison(Comparison::EQ, $field, $value, $dqlAlias);
378
    }
379
380
    /**
381
     * @param Operand|string $field
382
     * @param Operand|mixed  $value
383
     * @param string|null    $dqlAlias
384
     *
385
     * @return Comparison
386
     */
387
    public static function neq($field, $value, $dqlAlias = null)
388
    {
389
        return new Comparison(Comparison::NEQ, $field, $value, $dqlAlias);
390
    }
391
392
    /**
393
     * @param Operand|string $field
394
     * @param Operand|mixed  $value
395
     * @param string|null    $dqlAlias
396
     *
397
     * @return Comparison
398
     */
399
    public static function lt($field, $value, $dqlAlias = null)
400
    {
401
        return new Comparison(Comparison::LT, $field, $value, $dqlAlias);
402
    }
403
404
    /**
405
     * @param Operand|string $field
406
     * @param Operand|mixed  $value
407
     * @param string|null    $dqlAlias
408
     *
409
     * @return Comparison
410
     */
411
    public static function lte($field, $value, $dqlAlias = null)
412
    {
413
        return new Comparison(Comparison::LTE, $field, $value, $dqlAlias);
414
    }
415
416
    /**
417
     * @param Operand|string $field
418
     * @param Operand|mixed  $value
419
     * @param string|null    $dqlAlias
420
     *
421
     * @return Comparison
422
     */
423
    public static function gt($field, $value, $dqlAlias = null)
424
    {
425
        return new Comparison(Comparison::GT, $field, $value, $dqlAlias);
426
    }
427
428
    /**
429
     * @param Operand|string $field
430
     * @param Operand|mixed  $value
431
     * @param string|null    $dqlAlias
432
     *
433
     * @return Comparison
434
     */
435
    public static function gte($field, $value, $dqlAlias = null)
436
    {
437
        return new Comparison(Comparison::GTE, $field, $value, $dqlAlias);
438
    }
439
440
    /**
441
     * @param Operand|string $field
442
     * @param string         $value
443
     * @param string         $format
444
     * @param string|null    $dqlAlias
445
     *
446
     * @return Like
447
     */
448
    public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null)
449
    {
450
        return new Like($field, $value, $format, $dqlAlias);
451
    }
452
453
    /**
454
     * @param string      $value
455
     * @param string|null $dqlAlias
456
     *
457
     * @return InstanceOfX
458
     */
459
    public static function instanceOfX($value, $dqlAlias = null)
460
    {
461
        return new InstanceOfX($value, $dqlAlias);
462
    }
463
464
    /*
465
     * Specifications
466
     */
467
468
    /**
469
     * @param Filter|QueryModifier $spec
470
     *
471
     * @return CountOf
472
     */
473
    public static function countOf($spec)
474
    {
475
        return new CountOf($spec);
476
    }
477
478
    /**
479
     * @param Filter|string $spec
480
     *
481
     * @return Having
482
     */
483
    public static function having($spec)
484
    {
485
        return new Having($spec);
486
    }
487
488
    /*
489
     * Operands
490
     */
491
492
    /**
493
     * @param string $fieldName
494
     *
495
     * @return Field
496
     */
497
    public static function field($fieldName)
498
    {
499
        return new Field($fieldName);
500
    }
501
502
    /**
503
     * @param mixed           $value
504
     * @param int|string|null $valueType
505
     *
506
     * @return Value
507
     */
508
    public static function value($value, $valueType = null)
509
    {
510
        return new Value($value, $valueType);
511
    }
512
513
    /**
514
     * @param array           $values
515
     * @param int|string|null $valueType
516
     *
517
     * @return Values
518
     */
519
    public static function values($values, $valueType = null)
520
    {
521
        return new Values($values, $valueType);
522
    }
523
524
    /**
525
     * @param string $value
526
     * @param string $format
527
     *
528
     * @return LikePattern
529
     */
530
    public static function likePattern($value, $format = LikePattern::CONTAINS)
531
    {
532
        return new LikePattern($value, $format);
533
    }
534
535
    /*
536
     * Arithmetic operands
537
     */
538
539
    /**
540
     * @param Operand|string $field
541
     * @param Operand|mixed  $value
542
     *
543
     * @return Addition
544
     */
545
    public static function add($field, $value)
546
    {
547
        return new Addition($field, $value);
548
    }
549
550
    /**
551
     * @param Operand|string $field
552
     * @param Operand|mixed  $value
553
     *
554
     * @return Subtraction
555
     */
556
    public static function sub($field, $value)
557
    {
558
        return new Subtraction($field, $value);
559
    }
560
561
    /**
562
     * @param Operand|string $field
563
     * @param Operand|mixed  $value
564
     *
565
     * @return Multiplication
566
     */
567
    public static function mul($field, $value)
568
    {
569
        return new Multiplication($field, $value);
570
    }
571
572
    /**
573
     * @param Operand|string $field
574
     * @param Operand|mixed  $value
575
     *
576
     * @return Division
577
     */
578
    public static function div($field, $value)
579
    {
580
        return new Division($field, $value);
581
    }
582
583
    /**
584
     * @param Operand|string $field
585
     * @param Operand|mixed  $value
586
     *
587
     * @return Modulo
588
     */
589
    public static function mod($field, $value)
590
    {
591
        return new Modulo($field, $value);
592
    }
593
594
    /**
595
     * @param string $functionName
596
     * @param mixed  $arguments
597
     *
598
     * @return PlatformFunction
599
     */
600
    public static function fun($functionName, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $functionName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
601
    {
602
        $arguments = func_get_args();
603
604
        return new PlatformFunction(array_shift($arguments), $arguments);
605
    }
606
607
    /**
608
     * @param string $alias
609
     *
610
     * @return Alias
611
     */
612
    public static function alias($alias)
613
    {
614
        return new Alias($alias);
615
    }
616
}
617