Completed
Push — 2.0 ( d7d09e...57ce97 )
by Peter
06:52 queued 12s
created

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