Completed
Push — 2.0 ( 3fe951...5877a9 )
by Peter
07:19 queued 12s
created

Spec::select()   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
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\CountDistinct;
36
use Happyr\DoctrineSpecification\Operand\Division;
37
use Happyr\DoctrineSpecification\Operand\Field;
38
use Happyr\DoctrineSpecification\Operand\LikePattern;
39
use Happyr\DoctrineSpecification\Operand\Multiplication;
40
use Happyr\DoctrineSpecification\Operand\Operand;
41
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
42
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateAdd;
43
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateSub;
44
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Trim;
45
use Happyr\DoctrineSpecification\Operand\Subtraction;
46
use Happyr\DoctrineSpecification\Operand\Value;
47
use Happyr\DoctrineSpecification\Operand\Values;
48
use Happyr\DoctrineSpecification\Query\AddSelect;
49
use Happyr\DoctrineSpecification\Query\Distinct;
50
use Happyr\DoctrineSpecification\Query\GroupBy;
51
use Happyr\DoctrineSpecification\Query\Having;
52
use Happyr\DoctrineSpecification\Query\IndexBy;
53
use Happyr\DoctrineSpecification\Query\InnerJoin;
54
use Happyr\DoctrineSpecification\Query\Join;
55
use Happyr\DoctrineSpecification\Query\LeftJoin;
56
use Happyr\DoctrineSpecification\Query\Limit;
57
use Happyr\DoctrineSpecification\Query\Offset;
58
use Happyr\DoctrineSpecification\Query\OrderBy;
59
use Happyr\DoctrineSpecification\Query\QueryModifier;
60
use Happyr\DoctrineSpecification\Query\Select;
61
use Happyr\DoctrineSpecification\Query\Selection\SelectAs;
62
use Happyr\DoctrineSpecification\Query\Selection\SelectEntity;
63
use Happyr\DoctrineSpecification\Query\Selection\SelectHiddenAs;
64
use Happyr\DoctrineSpecification\Query\Selection\Selection;
65
use Happyr\DoctrineSpecification\Query\Slice;
66
use Happyr\DoctrineSpecification\Result\AsArray;
67
use Happyr\DoctrineSpecification\Result\AsScalar;
68
use Happyr\DoctrineSpecification\Result\AsSingleScalar;
69
use Happyr\DoctrineSpecification\Result\Cache;
70
use Happyr\DoctrineSpecification\Result\RoundDateTime;
71
use Happyr\DoctrineSpecification\Specification\CountOf;
72
73
/**
74
 * Factory class for the specifications.
75
 *
76
 * @method static PlatformFunction CONCAT($str1, $str2)
77
 * @method static PlatformFunction SUBSTRING($str, $start, $length = null) Return substring of given string.
78
 * @method static PlatformFunction LOWER($str) Returns the string lowercased.
79
 * @method static PlatformFunction UPPER($str) Return the upper-case of the given string.
80
 * @method static PlatformFunction IDENTITY($expression, $fieldMapping = null) Retrieve the foreign key column of association of the owning side
81
 * @method static PlatformFunction LENGTH($str) Returns the length of the given string
82
 * @method static PlatformFunction LOCATE($needle, $haystack, $offset = 0) Locate the first occurrence of the substring in the string.
83
 * @method static PlatformFunction ABS($expression)
84
 * @method static PlatformFunction SQRT($q) Return the square-root of q.
85
 * @method static PlatformFunction MOD($a, $b) Return a MOD b.
86
 * @method static PlatformFunction SIZE($collection) Return the number of elements in the specified collection
87
 * @method static PlatformFunction DATE_DIFF($date1, $date2) Calculate the difference in days between date1-date2.
88
 * @method static PlatformFunction BIT_AND($a, $b)
89
 * @method static PlatformFunction BIT_OR($a, $b)
90
 * @method static PlatformFunction MIN($a)
91
 * @method static PlatformFunction MAX($a)
92
 * @method static PlatformFunction AVG($a)
93
 * @method static PlatformFunction SUM($a)
94
 * @method static PlatformFunction COUNT($a)
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 $context
279
     *
280
     * @return SelectEntity
281
     */
282
    public static function selectEntity(string $context): SelectEntity
283
    {
284
        return new SelectEntity($context);
285
    }
286
287
    /**
288
     * @param Filter|Operand|string $expression
289
     * @param string                $alias
290
     *
291
     * @return SelectAs
292
     */
293
    public static function selectAs($expression, string $alias): SelectAs
294
    {
295
        return new SelectAs($expression, $alias);
296
    }
297
298
    /**
299
     * @param Filter|Operand|string $expression
300
     * @param string                $alias
301
     *
302
     * @return SelectHiddenAs
303
     */
304
    public static function selectHiddenAs($expression, string $alias): SelectHiddenAs
305
    {
306
        return new SelectHiddenAs($expression, $alias);
307
    }
308
309
    // Result modifier
310
311
    /**
312
     * @return AsArray
313
     */
314
    public static function asArray(): AsArray
315
    {
316
        return new AsArray();
317
    }
318
319
    /**
320
     * @return AsSingleScalar
321
     */
322
    public static function asSingleScalar(): AsSingleScalar
323
    {
324
        return new AsSingleScalar();
325
    }
326
327
    /**
328
     * @return AsScalar
329
     */
330
    public static function asScalar(): AsScalar
331
    {
332
        return new AsScalar();
333
    }
334
335
    /**
336
     * @param int $cacheLifetime How many seconds the cached entry is valid
337
     *
338
     * @return Cache
339
     */
340
    public static function cache(int $cacheLifetime): Cache
341
    {
342
        return new Cache($cacheLifetime);
343
    }
344
345
    /**
346
     * @param int $roundSeconds How may seconds to round time
347
     *
348
     * @return RoundDateTime
349
     */
350
    public static function roundDateTimeParams(int $roundSeconds): RoundDateTime
351
    {
352
        return new RoundDateTime($roundSeconds);
353
    }
354
355
    // Filters
356
357
    /**
358
     * @param Operand|string $field
359
     * @param string|null    $context
360
     *
361
     * @return IsNull
362
     */
363
    public static function isNull($field, ?string $context = null): IsNull
364
    {
365
        return new IsNull($field, $context);
366
    }
367
368
    /**
369
     * @param Operand|string $field
370
     * @param string|null    $context
371
     *
372
     * @return IsNotNull
373
     */
374
    public static function isNotNull($field, ?string $context = null): IsNotNull
375
    {
376
        return new IsNotNull($field, $context);
377
    }
378
379
    /**
380
     * Make sure the $field has a value equals to $value.
381
     *
382
     * @param Operand|string $field
383
     * @param Operand|mixed  $value
384
     * @param string|null    $context
385
     *
386
     * @return In
387
     */
388
    public static function in($field, $value, ?string $context = null): In
389
    {
390
        return new In($field, $value, $context);
391
    }
392
393
    /**
394
     * @param Operand|string $field
395
     * @param Operand|mixed  $value
396
     * @param string|null    $context
397
     *
398
     * @return Not
399
     */
400
    public static function notIn($field, $value, ?string $context = null): Not
401
    {
402
        return new Not(new In($field, $value, $context));
403
    }
404
405
    /**
406
     * @param Operand|string $field
407
     * @param Operand|mixed  $value
408
     * @param string|null    $context
409
     *
410
     * @return Equals
411
     */
412
    public static function eq($field, $value, ?string $context = null): Equals
413
    {
414
        return new Equals($field, $value, $context);
415
    }
416
417
    /**
418
     * @param Operand|string $field
419
     * @param Operand|mixed  $value
420
     * @param string|null    $context
421
     *
422
     * @return NotEquals
423
     */
424
    public static function neq($field, $value, ?string $context = null): NotEquals
425
    {
426
        return new NotEquals($field, $value, $context);
427
    }
428
429
    /**
430
     * @param Operand|string $field
431
     * @param Operand|mixed  $value
432
     * @param string|null    $context
433
     *
434
     * @return LessThan
435
     */
436
    public static function lt($field, $value, ?string $context = null): LessThan
437
    {
438
        return new LessThan($field, $value, $context);
439
    }
440
441
    /**
442
     * @param Operand|string $field
443
     * @param Operand|mixed  $value
444
     * @param string|null    $context
445
     *
446
     * @return LessOrEqualThan
447
     */
448
    public static function lte($field, $value, ?string $context = null): LessOrEqualThan
449
    {
450
        return new LessOrEqualThan($field, $value, $context);
451
    }
452
453
    /**
454
     * @param Operand|string $field
455
     * @param Operand|mixed  $value
456
     * @param string|null    $context
457
     *
458
     * @return GreaterThan
459
     */
460
    public static function gt($field, $value, ?string $context = null): GreaterThan
461
    {
462
        return new GreaterThan($field, $value, $context);
463
    }
464
465
    /**
466
     * @param Operand|string $field
467
     * @param Operand|mixed  $value
468
     * @param string|null    $context
469
     *
470
     * @return GreaterOrEqualThan
471
     */
472
    public static function gte($field, $value, ?string $context = null): GreaterOrEqualThan
473
    {
474
        return new GreaterOrEqualThan($field, $value, $context);
475
    }
476
477
    /**
478
     * @param Operand|string     $field
479
     * @param LikePattern|string $value
480
     * @param string             $format
481
     * @param string|null        $context
482
     *
483
     * @return Like
484
     */
485
    public static function like($field, $value, string $format = Like::CONTAINS, ?string $context = null): Like
486
    {
487
        return new Like($field, $value, $format, $context);
488
    }
489
490
    /**
491
     * @param string      $value
492
     * @param string|null $context
493
     *
494
     * @return InstanceOfX
495
     */
496
    public static function instanceOfX($value, ?string $context = null): InstanceOfX
497
    {
498
        return new InstanceOfX($value, $context);
499
    }
500
501
    /**
502
     * @param Operand|mixed  $value
503
     * @param Operand|string $field
504
     * @param string|null    $context
505
     *
506
     * @return MemberOfX
507
     */
508
    public static function memberOfX($value, $field, ?string $context = null): MemberOfX
509
    {
510
        return new MemberOfX($value, $field, $context);
511
    }
512
513
    // Specifications
514
515
    /**
516
     * @param Filter|QueryModifier $spec
517
     *
518
     * @return CountOf
519
     */
520
    public static function countOf($spec): CountOf
521
    {
522
        return new CountOf($spec);
523
    }
524
525
    /**
526
     * @param Filter $spec
527
     *
528
     * @return Having
529
     */
530
    public static function having(Filter $spec): Having
531
    {
532
        return new Having($spec);
533
    }
534
535
    // Operands
536
537
    /**
538
     * @param string      $fieldName
539
     * @param string|null $context
540
     *
541
     * @return Field
542
     */
543
    public static function field(string $fieldName, ?string $context = null): Field
544
    {
545
        return new Field($fieldName, $context);
546
    }
547
548
    /**
549
     * @param mixed           $value
550
     * @param int|string|null $valueType
551
     *
552
     * @return Value
553
     */
554
    public static function value($value, $valueType = null): Value
555
    {
556
        return new Value($value, $valueType);
557
    }
558
559
    /**
560
     * @param mixed[]         $values
561
     * @param int|string|null $valueType
562
     *
563
     * @return Values
564
     */
565
    public static function values(array $values, $valueType = null): Values
566
    {
567
        return new Values($values, $valueType);
568
    }
569
570
    /**
571
     * @param string $value
572
     * @param string $format
573
     *
574
     * @return LikePattern
575
     */
576
    public static function likePattern(string $value, string $format = LikePattern::CONTAINS): LikePattern
577
    {
578
        return new LikePattern($value, $format);
579
    }
580
581
    /**
582
     * @param Operand|string $field
583
     *
584
     * @return CountDistinct
585
     */
586
    public static function countDistinct($field): CountDistinct
587
    {
588
        return new CountDistinct($field);
589
    }
590
591
    // Arithmetic operands
592
593
    /**
594
     * @param Operand|string $field
595
     * @param Operand|mixed  $value
596
     *
597
     * @return Addition
598
     */
599
    public static function add($field, $value): Addition
600
    {
601
        return new Addition($field, $value);
602
    }
603
604
    /**
605
     * @param Operand|string $field
606
     * @param Operand|mixed  $value
607
     *
608
     * @return Subtraction
609
     */
610
    public static function sub($field, $value): Subtraction
611
    {
612
        return new Subtraction($field, $value);
613
    }
614
615
    /**
616
     * @param Operand|string $field
617
     * @param Operand|mixed  $value
618
     *
619
     * @return Multiplication
620
     */
621
    public static function mul($field, $value): Multiplication
622
    {
623
        return new Multiplication($field, $value);
624
    }
625
626
    /**
627
     * @param Operand|string $field
628
     * @param Operand|mixed  $value
629
     *
630
     * @return Division
631
     */
632
    public static function div($field, $value): Division
633
    {
634
        return new Division($field, $value);
635
    }
636
637
    // Platform functions
638
639
    /**
640
     * Trim the string by the given trim char, defaults to whitespaces.
641
     *
642
     * @param Operand|string $string
643
     * @param string         $mode
644
     * @param string         $characters
645
     */
646
    public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
647
    {
648
        return new Trim($string, $mode, $characters);
649
    }
650
651
    /**
652
     * Add the number of days to a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND).
653
     *
654
     * @param \DateTimeInterface|string|Operand $date
655
     * @param int|Operand                       $value
656
     * @param string                            $unit
657
     *
658
     * @return DateAdd
659
     */
660
    public static function DATE_ADD($date, $value, string $unit): DateAdd
661
    {
662
        return new DateAdd($date, $value, $unit);
663
    }
664
665
    /**
666
     * Substract the number of days from a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND).
667
     *
668
     * @param \DateTimeInterface|string|Operand $date
669
     * @param int|Operand                       $value
670
     * @param string                            $unit
671
     *
672
     * @return DateSub
673
     */
674
    public static function DATE_SUB($date, $value, string $unit): DateSub
675
    {
676
        return new DateSub($date, $value, $unit);
677
    }
678
679
    /**
680
     * Call DQL function.
681
     *
682
     * Usage:
683
     *  Spec::fun('CURRENT_DATE')
684
     *  Spec::fun('DATE_DIFF', $date1, $date2)
685
     *
686
     * @param string $functionName
687
     * @param mixed  ...$arguments
688
     *
689
     * @return PlatformFunction
690
     */
691
    public static function fun(string $functionName, ...$arguments): PlatformFunction
692
    {
693
        return new PlatformFunction($functionName, ...$arguments);
694
    }
695
696
    /**
697
     * @param string $alias
698
     *
699
     * @return Alias
700
     */
701
    public static function alias(string $alias): Alias
702
    {
703
        return new Alias($alias);
704
    }
705
}
706