Completed
Push — master ( 0bc2a3...83b98a )
by Peter
05:59 queued 10s
created

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