Completed
Push — 1.0 ( f155d9...8e07ac )
by Peter
08:03
created

Spec::__callStatic()   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
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\Alias;
16
use Happyr\DoctrineSpecification\Operand\Field;
17
use Happyr\DoctrineSpecification\Operand\LikePattern;
18
use Happyr\DoctrineSpecification\Operand\Operand;
19
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
20
use Happyr\DoctrineSpecification\Operand\Value;
21
use Happyr\DoctrineSpecification\Operand\Values;
22
use Happyr\DoctrineSpecification\Query\AddSelect;
23
use Happyr\DoctrineSpecification\Query\GroupBy;
24
use Happyr\DoctrineSpecification\Query\InnerJoin;
25
use Happyr\DoctrineSpecification\Query\Join;
26
use Happyr\DoctrineSpecification\Query\LeftJoin;
27
use Happyr\DoctrineSpecification\Query\Limit;
28
use Happyr\DoctrineSpecification\Query\Offset;
29
use Happyr\DoctrineSpecification\Query\OrderBy;
30
use Happyr\DoctrineSpecification\Query\QueryModifier;
31
use Happyr\DoctrineSpecification\Query\Select;
32
use Happyr\DoctrineSpecification\Query\Selection\SelectAs;
33
use Happyr\DoctrineSpecification\Query\Selection\SelectEntity;
34
use Happyr\DoctrineSpecification\Query\Selection\SelectHiddenAs;
35
use Happyr\DoctrineSpecification\Query\Slice;
36
use Happyr\DoctrineSpecification\Result\AsArray;
37
use Happyr\DoctrineSpecification\Result\AsSingleScalar;
38
use Happyr\DoctrineSpecification\Result\Cache;
39
use Happyr\DoctrineSpecification\Result\RoundDateTime;
40
use Happyr\DoctrineSpecification\Specification\CountOf;
41
use Happyr\DoctrineSpecification\Specification\Having;
42
43
/**
44
 * Factory class for the specifications.
45
 *
46
 * @method static PlatformFunction CONCAT($str1, $str2)
47
 * @method static PlatformFunction SUBSTRING($str, $start, $length = null) Return substring of given string.
48
 * @method static PlatformFunction TRIM($str) Trim the string by the given trim char, defaults to whitespaces.
49
 * @method static PlatformFunction LOWER($str) Returns the string lowercased.
50
 * @method static PlatformFunction UPPER($str) Return the upper-case of the given string.
51
 * @method static PlatformFunction IDENTITY($expression, $fieldMapping = null) Retrieve the foreign key column of association of the owning side
52
 * @method static PlatformFunction LENGTH($str) Returns the length of the given string
53
 * @method static PlatformFunction LOCATE($needle, $haystack, $offset = 0) Locate the first occurrence of the substring in the string.
54
 * @method static PlatformFunction ABS($expression)
55
 * @method static PlatformFunction SQRT($q) Return the square-root of q.
56
 * @method static PlatformFunction MOD($a, $b) Return a MOD b.
57
 * @method static PlatformFunction SIZE($collection) Return the number of elements in the specified collection
58
 * @method static PlatformFunction DATE_DIFF($date1, $date2) Calculate the difference in days between date1-date2.
59
 * @method static PlatformFunction BIT_AND($a, $b)
60
 * @method static PlatformFunction BIT_OR($a, $b)
61
 * @method static PlatformFunction MIN($a)
62
 * @method static PlatformFunction MAX($a)
63
 * @method static PlatformFunction AVG($a)
64
 * @method static PlatformFunction SUM($a)
65
 * @method static PlatformFunction COUNT($a)
66
 * @method static PlatformFunction CURRENT_DATE() Return the current date
67
 * @method static PlatformFunction CURRENT_TIME() Returns the current time
68
 * @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
69
 * @method static PlatformFunction DATE_ADD($date, $days, $unit) Add the number of days to a given date. (Supported units are DAY, MONTH)
70
 * @method static PlatformFunction DATE_SUB($date, $days, $unit) Substract the number of days from a given date. (Supported units are DAY, MONTH)
71
 */
72
class Spec
73
{
74
    /**
75
     * @param string $name
76
     * @param array  $arguments
77
     *
78
     * @return PlatformFunction
79
     */
80
    public static function __callStatic($name, array $arguments = [])
81
    {
82
        return self::fun($name, $arguments);
83
    }
84
85
    /*
86
     * Logic
87
     */
88
89
    /**
90
     * @return AndX
91
     */
92
    public static function andX()
93
    {
94
        $args = func_get_args();
95
        $reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\AndX');
96
97
        return $reflection->newInstanceArgs($args);
98
    }
99
100
    /**
101
     * @return OrX
102
     */
103
    public static function orX()
104
    {
105
        $args = func_get_args();
106
        $reflection = new \ReflectionClass('Happyr\DoctrineSpecification\Logic\OrX');
107
108
        return $reflection->newInstanceArgs($args);
109
    }
110
111
    /**
112
     * @param Filter $spec
113
     *
114
     * @return Not
115
     */
116
    public static function not(Filter $spec)
117
    {
118
        return new Not($spec);
119
    }
120
121
    /*
122
     * Query modifier
123
     */
124
125
    /**
126
     * @param string $field
127
     * @param string $newAlias
128
     * @param string $dqlAlias
129
     *
130
     * @return Join
131
     */
132
    public static function join($field, $newAlias, $dqlAlias = null)
133
    {
134
        return new Join($field, $newAlias, $dqlAlias);
135
    }
136
137
    /**
138
     * @param string $field
139
     * @param string $newAlias
140
     * @param string $dqlAlias
141
     *
142
     * @return LeftJoin
143
     */
144
    public static function leftJoin($field, $newAlias, $dqlAlias = null)
145
    {
146
        return new LeftJoin($field, $newAlias, $dqlAlias);
147
    }
148
149
    /**
150
     * @param string $field
151
     * @param string $newAlias
152
     * @param string $dqlAlias
153
     *
154
     * @return InnerJoin
155
     */
156
    public static function innerJoin($field, $newAlias, $dqlAlias = null)
157
    {
158
        return new InnerJoin($field, $newAlias, $dqlAlias);
159
    }
160
161
    /**
162
     * @param int $count
163
     *
164
     * @return Limit
165
     */
166
    public static function limit($count)
167
    {
168
        return new Limit($count);
169
    }
170
171
    /**
172
     * @param int $count
173
     *
174
     * @return Offset
175
     */
176
    public static function offset($count)
177
    {
178
        return new Offset($count);
179
    }
180
181
    /**
182
     * @param int $sliceSize
183
     * @param int $sliceNumber
184
     *
185
     * @return Slice
186
     */
187
    public static function slice($sliceSize, $sliceNumber = 0)
188
    {
189
        return new Slice($sliceSize, $sliceNumber);
190
    }
191
192
    /**
193
     * @param string      $field
194
     * @param string      $order
195
     * @param string|null $dqlAlias
196
     *
197
     * @return OrderBy
198
     */
199
    public static function orderBy($field, $order = 'ASC', $dqlAlias = null)
200
    {
201
        return new OrderBy($field, $order, $dqlAlias);
202
    }
203
204
    /**
205
     * @param string $field
206
     * @param string $dqlAlias
207
     *
208
     * @return GroupBy
209
     */
210
    public static function groupBy($field, $dqlAlias = null)
211
    {
212
        return new GroupBy($field, $dqlAlias);
213
    }
214
215
    /*
216
     * Selection
217
     */
218
219
    /**
220
     * @param mixed $field
221
     *
222
     * @return Select
223
     */
224
    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...
225
    {
226
        return new Select(func_get_args());
227
    }
228
229
    /**
230
     * @param mixed $field
231
     *
232
     * @return AddSelect
233
     */
234
    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...
235
    {
236
        return new AddSelect(func_get_args());
237
    }
238
239
    /**
240
     * @param string $dqlAlias
241
     *
242
     * @return SelectEntity
243
     */
244
    public static function selectEntity($dqlAlias)
245
    {
246
        return new SelectEntity($dqlAlias);
247
    }
248
249
    /**
250
     * @param Filter|Operand|string $expression
251
     * @param string                $alias
252
     *
253
     * @return SelectAs
254
     */
255
    public static function selectAs($expression, $alias)
256
    {
257
        return new SelectAs($expression, $alias);
258
    }
259
260
    /**
261
     * @param Filter|Operand|string $expression
262
     * @param string                $alias
263
     *
264
     * @return SelectHiddenAs
265
     */
266
    public static function selectHiddenAs($expression, $alias)
267
    {
268
        return new SelectHiddenAs($expression, $alias);
269
    }
270
271
    /*
272
     * Result modifier
273
     */
274
275
    /**
276
     * @return AsArray
277
     */
278
    public static function asArray()
279
    {
280
        return new AsArray();
281
    }
282
283
    /**
284
     * @return AsSingleScalar
285
     */
286
    public static function asSingleScalar()
287
    {
288
        return new AsSingleScalar();
289
    }
290
291
    /**
292
     * @param int $cacheLifetime How many seconds the cached entry is valid
293
     *
294
     * @return Cache
295
     */
296
    public static function cache($cacheLifetime)
297
    {
298
        return new Cache($cacheLifetime);
299
    }
300
301
    /**
302
     * @param int $roundSeconds How may seconds to round time
303
     *
304
     * @return RoundDateTime
305
     */
306
    public static function roundDateTimeParams($roundSeconds)
307
    {
308
        return new RoundDateTime($roundSeconds);
309
    }
310
311
    /*
312
     * Filters
313
     */
314
315
    /**
316
     * @param Operand|string $field
317
     * @param string|null    $dqlAlias
318
     *
319
     * @return IsNull
320
     */
321
    public static function isNull($field, $dqlAlias = null)
322
    {
323
        return new IsNull($field, $dqlAlias);
324
    }
325
326
    /**
327
     * @param Operand|string $field
328
     * @param string|null    $dqlAlias
329
     *
330
     * @return IsNotNull
331
     */
332
    public static function isNotNull($field, $dqlAlias = null)
333
    {
334
        return new IsNotNull($field, $dqlAlias);
335
    }
336
337
    /**
338
     * Make sure the $field has a value equals to $value.
339
     *
340
     * @param string $field
341
     * @param mixed  $value
342
     * @param string $dqlAlias
343
     *
344
     * @return In
345
     */
346
    public static function in($field, $value, $dqlAlias = null)
347
    {
348
        return new In($field, $value, $dqlAlias);
349
    }
350
351
    /**
352
     * @param string $field
353
     * @param mixed  $value
354
     * @param string $dqlAlias
355
     *
356
     * @return Not
357
     */
358
    public static function notIn($field, $value, $dqlAlias = null)
359
    {
360
        return new Not(new In($field, $value, $dqlAlias));
361
    }
362
363
    /**
364
     * @param Operand|string $field
365
     * @param Operand|mixed  $value
366
     * @param string|null    $dqlAlias
367
     *
368
     * @return Comparison
369
     */
370
    public static function eq($field, $value, $dqlAlias = null)
371
    {
372
        return new Comparison(Comparison::EQ, $field, $value, $dqlAlias);
373
    }
374
375
    /**
376
     * @param Operand|string $field
377
     * @param Operand|mixed  $value
378
     * @param string|null    $dqlAlias
379
     *
380
     * @return Comparison
381
     */
382
    public static function neq($field, $value, $dqlAlias = null)
383
    {
384
        return new Comparison(Comparison::NEQ, $field, $value, $dqlAlias);
385
    }
386
387
    /**
388
     * @param Operand|string $field
389
     * @param Operand|mixed  $value
390
     * @param string|null    $dqlAlias
391
     *
392
     * @return Comparison
393
     */
394
    public static function lt($field, $value, $dqlAlias = null)
395
    {
396
        return new Comparison(Comparison::LT, $field, $value, $dqlAlias);
397
    }
398
399
    /**
400
     * @param Operand|string $field
401
     * @param Operand|mixed  $value
402
     * @param string|null    $dqlAlias
403
     *
404
     * @return Comparison
405
     */
406
    public static function lte($field, $value, $dqlAlias = null)
407
    {
408
        return new Comparison(Comparison::LTE, $field, $value, $dqlAlias);
409
    }
410
411
    /**
412
     * @param Operand|string $field
413
     * @param Operand|mixed  $value
414
     * @param string|null    $dqlAlias
415
     *
416
     * @return Comparison
417
     */
418
    public static function gt($field, $value, $dqlAlias = null)
419
    {
420
        return new Comparison(Comparison::GT, $field, $value, $dqlAlias);
421
    }
422
423
    /**
424
     * @param Operand|string $field
425
     * @param Operand|mixed  $value
426
     * @param string|null    $dqlAlias
427
     *
428
     * @return Comparison
429
     */
430
    public static function gte($field, $value, $dqlAlias = null)
431
    {
432
        return new Comparison(Comparison::GTE, $field, $value, $dqlAlias);
433
    }
434
435
    /**
436
     * @param Operand|string $field
437
     * @param string         $value
438
     * @param string         $format
439
     * @param string|null    $dqlAlias
440
     *
441
     * @return Like
442
     */
443
    public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null)
444
    {
445
        return new Like($field, $value, $format, $dqlAlias);
446
    }
447
448
    /**
449
     * @param string      $value
450
     * @param string|null $dqlAlias
451
     *
452
     * @return InstanceOfX
453
     */
454
    public static function instanceOfX($value, $dqlAlias = null)
455
    {
456
        return new InstanceOfX($value, $dqlAlias);
457
    }
458
459
    /*
460
     * Specifications
461
     */
462
463
    /**
464
     * @param Filter|QueryModifier $spec
465
     *
466
     * @return CountOf
467
     */
468
    public static function countOf($spec)
469
    {
470
        return new CountOf($spec);
471
    }
472
473
    /**
474
     * @param Filter|string $spec
475
     *
476
     * @return Having
477
     */
478
    public static function having($spec)
479
    {
480
        return new Having($spec);
481
    }
482
483
    /*
484
     * Operands
485
     */
486
487
    /**
488
     * @param string $fieldName
489
     *
490
     * @return Field
491
     */
492
    public static function field($fieldName)
493
    {
494
        return new Field($fieldName);
495
    }
496
497
    /**
498
     * @param mixed           $value
499
     * @param int|string|null $valueType
500
     *
501
     * @return Value
502
     */
503
    public static function value($value, $valueType = null)
504
    {
505
        return new Value($value, $valueType);
506
    }
507
508
    /**
509
     * @param array           $values
510
     * @param int|string|null $valueType
511
     *
512
     * @return Values
513
     */
514
    public static function values($values, $valueType = null)
515
    {
516
        return new Values($values, $valueType);
517
    }
518
519
    /**
520
     * @param string $value
521
     * @param string $format
522
     *
523
     * @return LikePattern
524
     */
525
    public static function likePattern($value, $format = LikePattern::CONTAINS)
526
    {
527
        return new LikePattern($value, $format);
528
    }
529
530
    /**
531
     * @param string $functionName
532
     * @param mixed  $arguments
533
     *
534
     * @return PlatformFunction
535
     */
536
    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...
537
    {
538
        $arguments = func_get_args();
539
540
        return new PlatformFunction(array_shift($arguments), $arguments);
541
    }
542
543
    /**
544
     * @param string $alias
545
     *
546
     * @return Alias
547
     */
548
    public static function alias($alias)
549
    {
550
        return new Alias($alias);
551
    }
552
}
553