Completed
Push — 2.0 ( 33bb83...14462a )
by Peter
07:18 queued 16s
created

Spec::gte()   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 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of the Happyr Doctrine Specification package.
6
 *
7
 * (c) Tobias Nyholm <[email protected]>
8
 *     Kacper Gunia <[email protected]>
9
 *     Peter Gribanov <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Happyr\DoctrineSpecification;
16
17
use Happyr\DoctrineSpecification\Filter\Equals;
18
use Happyr\DoctrineSpecification\Filter\Filter;
19
use Happyr\DoctrineSpecification\Filter\GreaterOrEqualThan;
20
use Happyr\DoctrineSpecification\Filter\GreaterThan;
21
use Happyr\DoctrineSpecification\Filter\In;
22
use Happyr\DoctrineSpecification\Filter\InstanceOfX;
23
use Happyr\DoctrineSpecification\Filter\IsNotNull;
24
use Happyr\DoctrineSpecification\Filter\IsNull;
25
use Happyr\DoctrineSpecification\Filter\LessOrEqualThan;
26
use Happyr\DoctrineSpecification\Filter\LessThan;
27
use Happyr\DoctrineSpecification\Filter\Like;
28
use Happyr\DoctrineSpecification\Filter\MemberOfX;
29
use Happyr\DoctrineSpecification\Filter\NotEquals;
30
use Happyr\DoctrineSpecification\Logic\AndX;
31
use Happyr\DoctrineSpecification\Logic\Not;
32
use Happyr\DoctrineSpecification\Logic\OrX;
33
use Happyr\DoctrineSpecification\Operand\Addition;
34
use Happyr\DoctrineSpecification\Operand\Alias;
35
use Happyr\DoctrineSpecification\Operand\Division;
36
use Happyr\DoctrineSpecification\Operand\Field;
37
use Happyr\DoctrineSpecification\Operand\LikePattern;
38
use Happyr\DoctrineSpecification\Operand\Multiplication;
39
use Happyr\DoctrineSpecification\Operand\Operand;
40
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
41
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Avg;
42
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count;
43
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateAdd;
44
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateSub;
45
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Max;
46
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Min;
47
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Sum;
48
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Trim;
49
use Happyr\DoctrineSpecification\Operand\Subtraction;
50
use Happyr\DoctrineSpecification\Operand\Value;
51
use Happyr\DoctrineSpecification\Operand\Values;
52
use Happyr\DoctrineSpecification\Query\AddSelect;
53
use Happyr\DoctrineSpecification\Query\Distinct;
54
use Happyr\DoctrineSpecification\Query\GroupBy;
55
use Happyr\DoctrineSpecification\Query\Having;
56
use Happyr\DoctrineSpecification\Query\IndexBy;
57
use Happyr\DoctrineSpecification\Query\InnerJoin;
58
use Happyr\DoctrineSpecification\Query\Join;
59
use Happyr\DoctrineSpecification\Query\LeftJoin;
60
use Happyr\DoctrineSpecification\Query\Limit;
61
use Happyr\DoctrineSpecification\Query\Offset;
62
use Happyr\DoctrineSpecification\Query\OrderBy;
63
use Happyr\DoctrineSpecification\Query\QueryModifier;
64
use Happyr\DoctrineSpecification\Query\Select;
65
use Happyr\DoctrineSpecification\Query\Selection\SelectAs;
66
use Happyr\DoctrineSpecification\Query\Selection\SelectEntity;
67
use Happyr\DoctrineSpecification\Query\Selection\SelectHiddenAs;
68
use Happyr\DoctrineSpecification\Query\Selection\Selection;
69
use Happyr\DoctrineSpecification\Query\SelectNew;
70
use Happyr\DoctrineSpecification\Query\Slice;
71
use Happyr\DoctrineSpecification\Result\AsArray;
72
use Happyr\DoctrineSpecification\Result\AsScalar;
73
use Happyr\DoctrineSpecification\Result\AsSingleScalar;
74
use Happyr\DoctrineSpecification\Result\Cache;
75
use Happyr\DoctrineSpecification\Result\RoundDateTime;
76
use Happyr\DoctrineSpecification\Specification\CountOf;
77
78
/**
79
 * Factory class for the specifications.
80
 *
81
 * @method static PlatformFunction CONCAT($str1, ...$str2)
82
 * @method static PlatformFunction SUBSTRING($str, $start, $length = null) Return substring of given string.
83
 * @method static PlatformFunction LOWER($str) Returns the string lowercased.
84
 * @method static PlatformFunction UPPER($str) Return the upper-case of the given string.
85
 * @method static PlatformFunction IDENTITY($expression, $fieldMapping = null) Retrieve the foreign key column of association of the owning side
86
 * @method static PlatformFunction LENGTH($str) Returns the length of the given string
87
 * @method static PlatformFunction LOCATE($needle, $haystack, $offset = 0) Locate the first occurrence of the substring in the string.
88
 * @method static PlatformFunction ABS($expression)
89
 * @method static PlatformFunction SQRT($q) Return the square-root of q.
90
 * @method static PlatformFunction MOD($a, $b) Return a MOD b.
91
 * @method static PlatformFunction SIZE($collection) Return the number of elements in the specified collection
92
 * @method static PlatformFunction DATE_DIFF($date1, $date2) Calculate the difference in days between date1-date2.
93
 * @method static PlatformFunction BIT_AND($a, $b)
94
 * @method static PlatformFunction BIT_OR($a, $b)
95
 * @method static PlatformFunction CURRENT_DATE() Return the current date
96
 * @method static PlatformFunction CURRENT_TIME() Returns the current time
97
 * @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
98
 */
99
class Spec
100
{
101
    /**
102
     * @param string  $functionName
103
     * @param mixed[] $arguments
104
     *
105
     * @return PlatformFunction
106
     */
107
    public static function __callStatic(string $functionName, array $arguments = []): PlatformFunction
108
    {
109
        return self::fun($functionName, ...$arguments);
110
    }
111
112
    // Logic
113
114
    /**
115
     * @param Filter|QueryModifier ...$specs
116
     *
117
     * @return AndX
118
     */
119
    public static function andX(...$specs): AndX
120
    {
121
        return new AndX(...$specs);
122
    }
123
124
    /**
125
     * @param Filter|QueryModifier ...$specs
126
     *
127
     * @return OrX
128
     */
129
    public static function orX(...$specs): OrX
130
    {
131
        return new OrX(...$specs);
132
    }
133
134
    /**
135
     * @param Filter $spec
136
     *
137
     * @return Not
138
     */
139
    public static function not(Filter $spec): Not
140
    {
141
        return new Not($spec);
142
    }
143
144
    // Query modifier
145
146
    /**
147
     * @param string      $field
148
     * @param string      $newAlias
149
     * @param string|null $context
150
     *
151
     * @return Join
152
     */
153
    public static function join(string $field, string $newAlias, ?string $context = null): Join
154
    {
155
        return new Join($field, $newAlias, $context);
156
    }
157
158
    /**
159
     * @param string      $field
160
     * @param string      $newAlias
161
     * @param string|null $context
162
     *
163
     * @return LeftJoin
164
     */
165
    public static function leftJoin(string $field, string $newAlias, ?string $context = null): LeftJoin
166
    {
167
        return new LeftJoin($field, $newAlias, $context);
168
    }
169
170
    /**
171
     * @param string      $field
172
     * @param string      $newAlias
173
     * @param string|null $context
174
     *
175
     * @return InnerJoin
176
     */
177
    public static function innerJoin(string $field, string $newAlias, ?string $context = null): InnerJoin
178
    {
179
        return new InnerJoin($field, $newAlias, $context);
180
    }
181
182
    /**
183
     * @param Field|string $field
184
     * @param string|null  $context
185
     *
186
     * @return IndexBy
187
     */
188
    public static function indexBy($field, ?string $context = null): IndexBy
189
    {
190
        return new IndexBy($field, $context);
191
    }
192
193
    /**
194
     * @param int $count
195
     *
196
     * @return Limit
197
     */
198
    public static function limit(int $count): Limit
199
    {
200
        return new Limit($count);
201
    }
202
203
    /**
204
     * @param int $count
205
     *
206
     * @return Offset
207
     */
208
    public static function offset(int $count): Offset
209
    {
210
        return new Offset($count);
211
    }
212
213
    /**
214
     * @param int $sliceSize
215
     * @param int $sliceNumber
216
     *
217
     * @return Slice
218
     */
219
    public static function slice(int $sliceSize, int $sliceNumber = 0): Slice
220
    {
221
        return new Slice($sliceSize, $sliceNumber);
222
    }
223
224
    /**
225
     * @param Field|Alias|string $field
226
     * @param string             $order
227
     * @param string|null        $context
228
     *
229
     * @return OrderBy
230
     */
231
    public static function orderBy($field, string $order = 'ASC', ?string $context = null): OrderBy
232
    {
233
        return new OrderBy($field, $order, $context);
234
    }
235
236
    /**
237
     * @param Field|Alias|string $field
238
     * @param string|null        $context
239
     *
240
     * @return GroupBy
241
     */
242
    public static function groupBy($field, ?string $context = null): GroupBy
243
    {
244
        return new GroupBy($field, $context);
245
    }
246
247
    /**
248
     * @return Distinct
249
     */
250
    public static function distinct(): Distinct
251
    {
252
        return new Distinct();
253
    }
254
255
    // Selection
256
257
    /**
258
     * @param Selection|string ...$fields
259
     *
260
     * @return Select
261
     */
262
    public static function select(...$fields): Select
263
    {
264
        return new Select(...$fields);
265
    }
266
267
    /**
268
     * @param Selection|string ...$fields
269
     *
270
     * @return AddSelect
271
     */
272
    public static function addSelect(...$fields): AddSelect
273
    {
274
        return new AddSelect(...$fields);
275
    }
276
277
    /**
278
     * @param string        $class
279
     * @param Operand|mixed ...$arguments
280
     *
281
     * @phpstan-param class-string $class
282
     *
283
     * @return SelectNew
284
     */
285
    public static function selectNew(string $class, ...$arguments): SelectNew
286
    {
287
        return new SelectNew($class, ...$arguments);
288
    }
289
290
    /**
291
     * @param string $dqlAliasInContext
292
     *
293
     * @return SelectEntity
294
     */
295
    public static function selectEntity(string $dqlAliasInContext): SelectEntity
296
    {
297
        return new SelectEntity($dqlAliasInContext);
298
    }
299
300
    /**
301
     * @param Filter|Operand|string $expression
302
     * @param string                $newAlias
303
     *
304
     * @return SelectAs
305
     */
306
    public static function selectAs($expression, string $newAlias): SelectAs
307
    {
308
        return new SelectAs($expression, $newAlias);
309
    }
310
311
    /**
312
     * @param Filter|Operand|string $expression
313
     * @param string                $newAlias
314
     *
315
     * @return SelectHiddenAs
316
     */
317
    public static function selectHiddenAs($expression, string $newAlias): SelectHiddenAs
318
    {
319
        return new SelectHiddenAs($expression, $newAlias);
320
    }
321
322
    // Result modifier
323
324
    /**
325
     * @return AsArray
326
     */
327
    public static function asArray(): AsArray
328
    {
329
        return new AsArray();
330
    }
331
332
    /**
333
     * @return AsSingleScalar
334
     */
335
    public static function asSingleScalar(): AsSingleScalar
336
    {
337
        return new AsSingleScalar();
338
    }
339
340
    /**
341
     * @return AsScalar
342
     */
343
    public static function asScalar(): AsScalar
344
    {
345
        return new AsScalar();
346
    }
347
348
    /**
349
     * @param int $cacheLifetime How many seconds the cached entry is valid
350
     *
351
     * @return Cache
352
     */
353
    public static function cache(int $cacheLifetime): Cache
354
    {
355
        return new Cache($cacheLifetime);
356
    }
357
358
    /**
359
     * @param int $roundSeconds How may seconds to round time
360
     *
361
     * @return RoundDateTime
362
     */
363
    public static function roundDateTimeParams(int $roundSeconds): RoundDateTime
364
    {
365
        return new RoundDateTime($roundSeconds);
366
    }
367
368
    // Filters
369
370
    /**
371
     * @param Operand|string $field
372
     * @param string|null    $context
373
     *
374
     * @return IsNull
375
     */
376
    public static function isNull($field, ?string $context = null): IsNull
377
    {
378
        return new IsNull($field, $context);
379
    }
380
381
    /**
382
     * @param Operand|string $field
383
     * @param string|null    $context
384
     *
385
     * @return IsNotNull
386
     */
387
    public static function isNotNull($field, ?string $context = null): IsNotNull
388
    {
389
        return new IsNotNull($field, $context);
390
    }
391
392
    /**
393
     * Make sure the $field has a value equals to $value.
394
     *
395
     * @param Operand|string $field
396
     * @param Operand|mixed  $value
397
     * @param string|null    $context
398
     *
399
     * @return In
400
     */
401
    public static function in($field, $value, ?string $context = null): In
402
    {
403
        return new In($field, $value, $context);
404
    }
405
406
    /**
407
     * @param Operand|string $field
408
     * @param Operand|mixed  $value
409
     * @param string|null    $context
410
     *
411
     * @return Not
412
     */
413
    public static function notIn($field, $value, ?string $context = null): Not
414
    {
415
        return new Not(new In($field, $value, $context));
416
    }
417
418
    /**
419
     * @param Operand|string $field
420
     * @param Operand|mixed  $value
421
     * @param string|null    $context
422
     *
423
     * @return Equals
424
     */
425
    public static function eq($field, $value, ?string $context = null): Equals
426
    {
427
        return new Equals($field, $value, $context);
428
    }
429
430
    /**
431
     * @param Operand|string $field
432
     * @param Operand|mixed  $value
433
     * @param string|null    $context
434
     *
435
     * @return NotEquals
436
     */
437
    public static function neq($field, $value, ?string $context = null): NotEquals
438
    {
439
        return new NotEquals($field, $value, $context);
440
    }
441
442
    /**
443
     * @param Operand|string $field
444
     * @param Operand|mixed  $value
445
     * @param string|null    $context
446
     *
447
     * @return LessThan
448
     */
449
    public static function lt($field, $value, ?string $context = null): LessThan
450
    {
451
        return new LessThan($field, $value, $context);
452
    }
453
454
    /**
455
     * @param Operand|string $field
456
     * @param Operand|mixed  $value
457
     * @param string|null    $context
458
     *
459
     * @return LessOrEqualThan
460
     */
461
    public static function lte($field, $value, ?string $context = null): LessOrEqualThan
462
    {
463
        return new LessOrEqualThan($field, $value, $context);
464
    }
465
466
    /**
467
     * @param Operand|string $field
468
     * @param Operand|mixed  $value
469
     * @param string|null    $context
470
     *
471
     * @return GreaterThan
472
     */
473
    public static function gt($field, $value, ?string $context = null): GreaterThan
474
    {
475
        return new GreaterThan($field, $value, $context);
476
    }
477
478
    /**
479
     * @param Operand|string $field
480
     * @param Operand|mixed  $value
481
     * @param string|null    $context
482
     *
483
     * @return GreaterOrEqualThan
484
     */
485
    public static function gte($field, $value, ?string $context = null): GreaterOrEqualThan
486
    {
487
        return new GreaterOrEqualThan($field, $value, $context);
488
    }
489
490
    /**
491
     * @param Operand|string     $field
492
     * @param LikePattern|string $value
493
     * @param string             $format
494
     * @param string|null        $context
495
     *
496
     * @return Like
497
     */
498
    public static function like($field, $value, string $format = Like::CONTAINS, ?string $context = null): Like
499
    {
500
        return new Like($field, $value, $format, $context);
501
    }
502
503
    /**
504
     * @param string      $value
505
     * @param string|null $context
506
     *
507
     * @return InstanceOfX
508
     */
509
    public static function instanceOfX($value, ?string $context = null): InstanceOfX
510
    {
511
        return new InstanceOfX($value, $context);
512
    }
513
514
    /**
515
     * @param Operand|mixed  $value
516
     * @param Operand|string $field
517
     * @param string|null    $context
518
     *
519
     * @return MemberOfX
520
     */
521
    public static function memberOfX($value, $field, ?string $context = null): MemberOfX
522
    {
523
        return new MemberOfX($value, $field, $context);
524
    }
525
526
    // Specifications
527
528
    /**
529
     * @param Filter|QueryModifier $spec
530
     *
531
     * @return CountOf
532
     */
533
    public static function countOf($spec): CountOf
534
    {
535
        return new CountOf($spec);
536
    }
537
538
    /**
539
     * @param Filter $spec
540
     *
541
     * @return Having
542
     */
543
    public static function having(Filter $spec): Having
544
    {
545
        return new Having($spec);
546
    }
547
548
    // Operands
549
550
    /**
551
     * @param string      $fieldName
552
     * @param string|null $context
553
     *
554
     * @return Field
555
     */
556
    public static function field(string $fieldName, ?string $context = null): Field
557
    {
558
        return new Field($fieldName, $context);
559
    }
560
561
    /**
562
     * @param mixed           $value
563
     * @param int|string|null $valueType
564
     *
565
     * @return Value
566
     */
567
    public static function value($value, $valueType = null): Value
568
    {
569
        return new Value($value, $valueType);
570
    }
571
572
    /**
573
     * @param mixed[]         $values
574
     * @param int|string|null $valueType
575
     *
576
     * @return Values
577
     */
578
    public static function values(array $values, $valueType = null): Values
579
    {
580
        return new Values($values, $valueType);
581
    }
582
583
    /**
584
     * @param string $value
585
     * @param string $format
586
     *
587
     * @return LikePattern
588
     */
589
    public static function likePattern(string $value, string $format = LikePattern::CONTAINS): LikePattern
590
    {
591
        return new LikePattern($value, $format);
592
    }
593
594
    // Arithmetic operands
595
596
    /**
597
     * @param Operand|string $field
598
     * @param Operand|mixed  $value
599
     *
600
     * @return Addition
601
     */
602
    public static function add($field, $value): Addition
603
    {
604
        return new Addition($field, $value);
605
    }
606
607
    /**
608
     * @param Operand|string $field
609
     * @param Operand|mixed  $value
610
     *
611
     * @return Subtraction
612
     */
613
    public static function sub($field, $value): Subtraction
614
    {
615
        return new Subtraction($field, $value);
616
    }
617
618
    /**
619
     * @param Operand|string $field
620
     * @param Operand|mixed  $value
621
     *
622
     * @return Multiplication
623
     */
624
    public static function mul($field, $value): Multiplication
625
    {
626
        return new Multiplication($field, $value);
627
    }
628
629
    /**
630
     * @param Operand|string $field
631
     * @param Operand|mixed  $value
632
     *
633
     * @return Division
634
     */
635
    public static function div($field, $value): Division
636
    {
637
        return new Division($field, $value);
638
    }
639
640
    // Platform functions
641
642
    /**
643
     * @param Operand|string $field
644
     * @param bool           $distinct
645
     *
646
     * @return Count
647
     */
648
    public static function COUNT($field, bool $distinct = false): Count
649
    {
650
        return new Count($field, $distinct);
651
    }
652
653
    /**
654
     * @param Operand|string $field
655
     * @param bool           $distinct
656
     *
657
     * @return Avg
658
     */
659
    public static function AVG($field, bool $distinct = false): Avg
660
    {
661
        return new Avg($field, $distinct);
662
    }
663
664
    /**
665
     * @param Operand|string $field
666
     * @param bool           $distinct
667
     *
668
     * @return Min
669
     */
670
    public static function MIN($field, bool $distinct = false): Min
671
    {
672
        return new Min($field, $distinct);
673
    }
674
675
    /**
676
     * @param Operand|string $field
677
     * @param bool           $distinct
678
     *
679
     * @return Max
680
     */
681
    public static function MAX($field, bool $distinct = false): Max
682
    {
683
        return new Max($field, $distinct);
684
    }
685
686
    /**
687
     * @param Operand|string $field
688
     * @param bool           $distinct
689
     *
690
     * @return Sum
691
     */
692
    public static function SUM($field, bool $distinct = false): Sum
693
    {
694
        return new Sum($field, $distinct);
695
    }
696
697
    /**
698
     * Add the number of days to a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND).
699
     *
700
     * @param \DateTimeInterface|string|Operand $date
701
     * @param int|Operand                       $value
702
     * @param string                            $unit
703
     *
704
     * @return DateAdd
705
     */
706
    public static function DATE_ADD($date, $value, string $unit): DateAdd
707
    {
708
        return new DateAdd($date, $value, $unit);
709
    }
710
711
    /**
712
     * Substract the number of days from a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND).
713
     *
714
     * @param \DateTimeInterface|string|Operand $date
715
     * @param int|Operand                       $value
716
     * @param string                            $unit
717
     *
718
     * @return DateSub
719
     */
720
    public static function DATE_SUB($date, $value, string $unit): DateSub
721
    {
722
        return new DateSub($date, $value, $unit);
723
    }
724
725
    /**
726
     * Trim the string by the given trim char, defaults to whitespaces.
727
     *
728
     * @param Operand|string $string
729
     * @param string         $mode
730
     * @param string         $characters
731
     */
732
    public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
733
    {
734
        return new Trim($string, $mode, $characters);
735
    }
736
737
    /**
738
     * Call DQL function.
739
     *
740
     * Usage:
741
     *  Spec::fun('CURRENT_DATE')
742
     *  Spec::fun('DATE_DIFF', $date1, $date2)
743
     *
744
     * @param string $functionName
745
     * @param mixed  ...$arguments
746
     *
747
     * @return PlatformFunction
748
     */
749
    public static function fun(string $functionName, ...$arguments): PlatformFunction
750
    {
751
        return new PlatformFunction($functionName, ...$arguments);
752
    }
753
754
    /**
755
     * @param string $alias
756
     *
757
     * @return Alias
758
     */
759
    public static function alias(string $alias): Alias
760
    {
761
        return new Alias($alias);
762
    }
763
}
764