Failed Conditions
Pull Request — master (#6747)
by
unknown
11:55 queued 03:29
created

Expr::any()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query;
6
7
use function func_get_args;
8
use function implode;
9
use function is_array;
10
use function is_bool;
11
use function is_numeric;
12
use function is_string;
13
use function str_replace;
14
15
/**
16
 * This class is used to generate DQL expressions via a set of PHP static functions.
17
 *
18
 * @todo Rename: ExpressionBuilder
19
 */
20
class Expr
21
{
22
    /**
23
     * Creates a conjunction of the given boolean expressions.
24
     *
25
     * Example:
26
     *
27
     *     [php]
28
     *     // (u.type = ?1) AND (u.role = ?2)
29
     *     $expr->andX($expr->eq('u.type', ':1'), $expr->eq('u.role', ':2'));
30
     *
31
     * @param Expr\Comparison|Expr\Func|Expr\Orx|string $x Optional clause. Defaults to null, but requires at least one
32
     *                                                     defined when converting to string.
33
     *
34
     * @return Expr\Andx
35
     */
36 5
    public function andX($x = null)
37
    {
38 5
        return new Expr\Andx(func_get_args());
39
    }
40
41
    /**
42
     * Creates a disjunction of the given boolean expressions.
43
     *
44
     * Example:
45
     *
46
     *     [php]
47
     *     // (u.type = ?1) OR (u.role = ?2)
48
     *     $q->where($q->expr()->orX('u.type = ?1', 'u.role = ?2'));
49
     *
50
     * @param mixed $x Optional clause. Defaults to null, but requires
51
     *                 at least one defined when converting to string.
52
     *
53
     * @return Expr\Orx
54
     */
55 8
    public function orX($x = null)
56
    {
57 8
        return new Expr\Orx(func_get_args());
58
    }
59
60
    /**
61
     * Creates an ASCending order expression.
62
     *
63
     * @param mixed $expr
64
     *
65
     * @return Expr\OrderBy
66
     */
67 2
    public function asc($expr)
68
    {
69 2
        return new Expr\OrderBy($expr, 'ASC');
70
    }
71
72
    /**
73
     * Creates a DESCending order expression.
74
     *
75
     * @param mixed $expr
76
     *
77
     * @return Expr\OrderBy
78
     */
79 3
    public function desc($expr)
80
    {
81 3
        return new Expr\OrderBy($expr, 'DESC');
82
    }
83
84
    /**
85
     * Creates an equality comparison expression with the given arguments.
86
     *
87
     * First argument is considered the left expression and the second is the right expression.
88
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
89
     *
90
     *     [php]
91
     *     // u.id = ?1
92
     *     $expr->eq('u.id', '?1');
93
     *
94
     * @param mixed $x Left expression.
95
     * @param mixed $y Right expression.
96
     *
97
     * @return Expr\Comparison
98
     */
99 27
    public function eq($x, $y)
100
    {
101 27
        return new Expr\Comparison($x, Expr\Comparison::EQ, $y);
102
    }
103
104
    /**
105
     * Creates an instance of Expr\Comparison, with the given arguments.
106
     * First argument is considered the left expression and the second is the right expression.
107
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
108
     *
109
     *     [php]
110
     *     // u.id <> ?1
111
     *     $q->where($q->expr()->neq('u.id', '?1'));
112
     *
113
     * @param mixed $x Left expression.
114
     * @param mixed $y Right expression.
115
     *
116
     * @return Expr\Comparison
117
     */
118 1
    public function neq($x, $y)
119
    {
120 1
        return new Expr\Comparison($x, Expr\Comparison::NEQ, $y);
121
    }
122
123
    /**
124
     * Creates an instance of Expr\Comparison, with the given arguments.
125
     * First argument is considered the left expression and the second is the right expression.
126
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
127
     *
128
     *     [php]
129
     *     // u.id < ?1
130
     *     $q->where($q->expr()->lt('u.id', '?1'));
131
     *
132
     * @param mixed $x Left expression.
133
     * @param mixed $y Right expression.
134
     *
135
     * @return Expr\Comparison
136
     */
137 3
    public function lt($x, $y)
138
    {
139 3
        return new Expr\Comparison($x, Expr\Comparison::LT, $y);
140
    }
141
142
    /**
143
     * Creates an instance of Expr\Comparison, with the given arguments.
144
     * First argument is considered the left expression and the second is the right expression.
145
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
146
     *
147
     *     [php]
148
     *     // u.id <= ?1
149
     *     $q->where($q->expr()->lte('u.id', '?1'));
150
     *
151
     * @param mixed $x Left expression.
152
     * @param mixed $y Right expression.
153
     *
154
     * @return Expr\Comparison
155
     */
156 1
    public function lte($x, $y)
157
    {
158 1
        return new Expr\Comparison($x, Expr\Comparison::LTE, $y);
159
    }
160
161
    /**
162
     * Creates an instance of Expr\Comparison, with the given arguments.
163
     * First argument is considered the left expression and the second is the right expression.
164
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
165
     *
166
     *     [php]
167
     *     // u.id > ?1
168
     *     $q->where($q->expr()->gt('u.id', '?1'));
169
     *
170
     * @param mixed $x Left expression.
171
     * @param mixed $y Right expression.
172
     *
173
     * @return Expr\Comparison
174
     */
175 2
    public function gt($x, $y)
176
    {
177 2
        return new Expr\Comparison($x, Expr\Comparison::GT, $y);
178
    }
179
180
    /**
181
     * Creates an instance of Expr\Comparison, with the given arguments.
182
     * First argument is considered the left expression and the second is the right expression.
183
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
184
     *
185
     *     [php]
186
     *     // u.id >= ?1
187
     *     $q->where($q->expr()->gte('u.id', '?1'));
188
     *
189
     * @param mixed $x Left expression.
190
     * @param mixed $y Right expression.
191
     *
192
     * @return Expr\Comparison
193
     */
194 1
    public function gte($x, $y)
195
    {
196 1
        return new Expr\Comparison($x, Expr\Comparison::GTE, $y);
197
    }
198
199
    /**
200
     * Creates an instance of AVG() function, with the given argument.
201
     *
202
     * @param mixed $x Argument to be used in AVG() function.
203
     *
204
     * @return Expr\Func
205
     */
206 1
    public function avg($x)
207
    {
208 1
        return new Expr\Func('AVG', [$x]);
209
    }
210
211
    /**
212
     * Creates an instance of MAX() function, with the given argument.
213
     *
214
     * @param mixed $x Argument to be used in MAX() function.
215
     *
216
     * @return Expr\Func
217
     */
218 2
    public function max($x)
219
    {
220 2
        return new Expr\Func('MAX', [$x]);
221
    }
222
223
    /**
224
     * Creates an instance of MIN() function, with the given argument.
225
     *
226
     * @param mixed $x Argument to be used in MIN() function.
227
     *
228
     * @return Expr\Func
229
     */
230 1
    public function min($x)
231
    {
232 1
        return new Expr\Func('MIN', [$x]);
233
    }
234
235
    /**
236
     * Creates an instance of COUNT() function, with the given argument.
237
     *
238
     * @param mixed $x Argument to be used in COUNT() function.
239
     *
240
     * @return Expr\Func
241
     */
242 1
    public function count($x)
243
    {
244 1
        return new Expr\Func('COUNT', [$x]);
245
    }
246
247
    /**
248
     * Creates an instance of COUNT(DISTINCT) function, with the given argument.
249
     *
250
     * @param mixed $x Argument to be used in COUNT(DISTINCT) function.
251
     *
252
     * @return string
253
     */
254 2
    public function countDistinct($x)
255
    {
256 2
        return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
257
    }
258
259
    /**
260
     * Creates an instance of EXISTS() function, with the given DQL Subquery.
261
     *
262
     * @param mixed $subquery DQL Subquery to be used in EXISTS() function.
263
     *
264
     * @return Expr\Func
265
     */
266 1
    public function exists($subquery)
267
    {
268 1
        return new Expr\Func('EXISTS', [$subquery]);
269
    }
270
271
    /**
272
     * Creates an instance of ALL() function, with the given DQL Subquery.
273
     *
274
     * @param mixed $subquery DQL Subquery to be used in ALL() function.
275
     *
276
     * @return Expr\Func
277
     */
278 2
    public function all($subquery)
279
    {
280 2
        return new Expr\Func('ALL', [$subquery]);
281
    }
282
283
    /**
284
     * Creates a SOME() function expression with the given DQL subquery.
285
     *
286
     * @param mixed $subquery DQL Subquery to be used in SOME() function.
287
     *
288
     * @return Expr\Func
289
     */
290 1
    public function some($subquery)
291
    {
292 1
        return new Expr\Func('SOME', [$subquery]);
293
    }
294
295
    /**
296
     * Creates an ANY() function expression with the given DQL subquery.
297
     *
298
     * @param mixed $subquery DQL Subquery to be used in ANY() function.
299
     *
300
     * @return Expr\Func
301
     */
302 1
    public function any($subquery)
303
    {
304 1
        return new Expr\Func('ANY', [$subquery]);
305
    }
306
307
    /**
308
     * Creates a negation expression of the given restriction.
309
     *
310
     * @param mixed $restriction Restriction to be used in NOT() function.
311
     *
312
     * @return Expr\Func
313
     */
314 2
    public function not($restriction)
315
    {
316 2
        return new Expr\Func('NOT', [$restriction]);
317
    }
318
319
    /**
320
     * Creates an ABS() function expression with the given argument.
321
     *
322
     * @param mixed $x Argument to be used in ABS() function.
323
     *
324
     * @return Expr\Func
325
     */
326 1
    public function abs($x)
327
    {
328 1
        return new Expr\Func('ABS', [$x]);
329
    }
330
331
    /**
332
     * Creates a MOD($x, $y) function expression to return the remainder of $x divided by $y.
333
     *
334
     * @param mixed $x
335
     * @param mixed $y
336
     *
337
     * @return Expr\Func
338
     */
339
    public function mod($x, $y)
340
    {
341
        return new Expr\Func('MOD', array($x, $y));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
342
    }
343
344
    /**
345
     * Creates a product mathematical expression with the given arguments.
346
     *
347
     * First argument is considered the left expression and the second is the right expression.
348
     * When converted to string, it will generated a <left expr> * <right expr>. Example:
349
     *
350
     *     [php]
351
     *     // u.salary * u.percentAnnualSalaryIncrease
352
     *     $q->expr()->prod('u.salary', 'u.percentAnnualSalaryIncrease')
353
     *
354
     * @param mixed $x Left expression.
355
     * @param mixed $y Right expression.
356
     *
357
     * @return Expr\Math
358
     */
359 1
    public function prod($x, $y)
360
    {
361 1
        return new Expr\Math($x, '*', $y);
362
    }
363
364
    /**
365
     * Creates a difference mathematical expression with the given arguments.
366
     * First argument is considered the left expression and the second is the right expression.
367
     * When converted to string, it will generated a <left expr> - <right expr>. Example:
368
     *
369
     *     [php]
370
     *     // u.monthlySubscriptionCount - 1
371
     *     $q->expr()->diff('u.monthlySubscriptionCount', '1')
372
     *
373
     * @param mixed $x Left expression.
374
     * @param mixed $y Right expression.
375
     *
376
     * @return Expr\Math
377
     */
378 2
    public function diff($x, $y)
379
    {
380 2
        return new Expr\Math($x, '-', $y);
381
    }
382
383
    /**
384
     * Creates a sum mathematical expression with the given arguments.
385
     * First argument is considered the left expression and the second is the right expression.
386
     * When converted to string, it will generated a <left expr> + <right expr>. Example:
387
     *
388
     *     [php]
389
     *     // u.numChildren + 1
390
     *     $q->expr()->sum('u.numChildren', '1')
391
     *
392
     * @param mixed $x Left expression.
393
     * @param mixed $y Right expression.
394
     *
395
     * @return Expr\Math
396
     */
397 1
    public function sum($x, $y)
398
    {
399 1
        return new Expr\Math($x, '+', $y);
400
    }
401
402
    /**
403
     * Creates a quotient mathematical expression with the given arguments.
404
     * First argument is considered the left expression and the second is the right expression.
405
     * When converted to string, it will generated a <left expr> / <right expr>. Example:
406
     *
407
     *     [php]
408
     *     // u.total / u.period
409
     *     $expr->quot('u.total', 'u.period')
410
     *
411
     * @param mixed $x Left expression.
412
     * @param mixed $y Right expression.
413
     *
414
     * @return Expr\Math
415
     */
416 3
    public function quot($x, $y)
417
    {
418 3
        return new Expr\Math($x, '/', $y);
419
    }
420
421
    /**
422
     * Creates a SQRT() function expression with the given argument.
423
     *
424
     * @param mixed $x Argument to be used in SQRT() function.
425
     *
426
     * @return Expr\Func
427
     */
428 1
    public function sqrt($x)
429
    {
430 1
        return new Expr\Func('SQRT', [$x]);
431
    }
432
433
    /**
434
     * Creates an IN() expression with the given arguments.
435
     *
436
     * @param string $x Field in string format to be restricted by IN() function.
437
     * @param mixed  $y Argument to be used in IN() function.
438
     *
439
     * @return Expr\Func
440
     */
441 9
    public function in($x, $y)
442
    {
443 9
        if (is_array($y)) {
444 8
            foreach ($y as &$literal) {
445 8
                if (! ($literal instanceof Expr\Literal)) {
446 7
                    $literal = $this->quoteLiteral($literal);
447
                }
448
            }
449
        }
450
451 9
        return new Expr\Func($x . ' IN', (array) $y);
452
    }
453
454
    /**
455
     * Creates a NOT IN() expression with the given arguments.
456
     *
457
     * @param string $x Field in string format to be restricted by NOT IN() function.
458
     * @param mixed  $y Argument to be used in NOT IN() function.
459
     *
460
     * @return Expr\Func
461
     */
462 5
    public function notIn($x, $y)
463
    {
464 5
        if (is_array($y)) {
465 4
            foreach ($y as &$literal) {
466 4
                if (! ($literal instanceof Expr\Literal)) {
467 4
                    $literal = $this->quoteLiteral($literal);
468
                }
469
            }
470
        }
471
472 5
        return new Expr\Func($x . ' NOT IN', (array) $y);
473
    }
474
475
    /**
476
     * Creates an IS NULL expression with the given arguments.
477
     *
478
     * @param string $x Field in string format to be restricted by IS NULL.
479
     *
480
     * @return string
481
     */
482 3
    public function isNull($x)
483
    {
484 3
        return $x . ' IS NULL';
485
    }
486
487
    /**
488
     * Creates an IS NOT NULL expression with the given arguments.
489
     *
490
     * @param string $x Field in string format to be restricted by IS NOT NULL.
491
     *
492
     * @return string
493
     */
494 2
    public function isNotNull($x)
495
    {
496 2
        return $x . ' IS NOT NULL';
497
    }
498
499
    /**
500
     * Creates a LIKE() comparison expression with the given arguments.
501
     *
502
     * @param string $x Field in string format to be inspected by LIKE() comparison.
503
     * @param mixed  $y Argument to be used in LIKE() comparison.
504
     *
505
     * @return Expr\Comparison
506
     */
507 4
    public function like($x, $y)
508
    {
509 4
        return new Expr\Comparison($x, 'LIKE', $y);
510
    }
511
512
    /**
513
     * Creates a NOT LIKE() comparison expression with the given arguments.
514
     *
515
     * @param string $x Field in string format to be inspected by LIKE() comparison.
516
     * @param mixed  $y Argument to be used in LIKE() comparison.
517
     *
518
     * @return Expr\Comparison
519
     */
520 1
    public function notLike($x, $y)
521
    {
522 1
        return new Expr\Comparison($x, 'NOT LIKE', $y);
523
    }
524
525
    /**
526
     * Creates a CONCAT() function expression with the given arguments.
527
     *
528
     * @param mixed $x     First argument to be used in CONCAT() function.
529
     * @param mixed $y,... Other arguments to be used in CONCAT() function.
530
     *
531
     * @return Expr\Func
532
     */
533 1
    public function concat($x, $y)
534
    {
535 1
        return new Expr\Func('CONCAT', func_get_args());
536
    }
537
538
    /**
539
     * Creates a SUBSTRING() function expression with the given arguments.
540
     *
541
     * @param mixed    $x    Argument to be used as string to be cropped by SUBSTRING() function.
542
     * @param int      $from Initial offset to start cropping string. May accept negative values.
543
     * @param int|null $len  Length of crop. May accept negative values.
544
     *
545
     * @return Expr\Func
546
     */
547 2
    public function substring($x, $from, $len = null)
548
    {
549 2
        $args = [$x, $from];
550 2
        if ($len !== null) {
551 1
            $args[] = $len;
552
        }
553
554 2
        return new Expr\Func('SUBSTRING', $args);
555
    }
556
557
    /**
558
     * Creates a LOWER() function expression with the given argument.
559
     *
560
     * @param mixed $x Argument to be used in LOWER() function.
561
     *
562
     * @return Expr\Func A LOWER function expression.
563
     */
564 1
    public function lower($x)
565
    {
566 1
        return new Expr\Func('LOWER', [$x]);
567
    }
568
569
    /**
570
     * Creates an UPPER() function expression with the given argument.
571
     *
572
     * @param mixed $x Argument to be used in UPPER() function.
573
     *
574
     * @return Expr\Func An UPPER function expression.
575
     */
576 1
    public function upper($x)
577
    {
578 1
        return new Expr\Func('UPPER', [$x]);
579
    }
580
581
    /**
582
     * Creates a LENGTH() function expression with the given argument.
583
     *
584
     * @param mixed $x Argument to be used as argument of LENGTH() function.
585
     *
586
     * @return Expr\Func A LENGTH function expression.
587
     */
588 1
    public function length($x)
589
    {
590 1
        return new Expr\Func('LENGTH', [$x]);
591
    }
592
593
    /**
594
     * Creates a literal expression of the given argument.
595
     *
596
     * @param mixed $literal Argument to be converted to literal.
597
     *
598
     * @return Expr\Literal
599
     */
600 7
    public function literal($literal)
601
    {
602 7
        return new Expr\Literal($this->quoteLiteral($literal));
603
    }
604
605
    /**
606
     * Quotes a literal value, if necessary, according to the DQL syntax.
607
     *
608
     * @param mixed $literal The literal value.
609
     *
610
     * @return string
611
     */
612 18
    private function quoteLiteral($literal)
613
    {
614 18
        if (is_numeric($literal) && ! is_string($literal)) {
615 10
            return (string) $literal;
616 8
        } elseif (is_bool($literal)) {
617 1
            return $literal ? 'true' : 'false';
618
        }
619
620 7
        return "'" . str_replace("'", "''", $literal) . "'";
621
    }
622
623
    /**
624
     * Creates an instance of BETWEEN() function, with the given argument.
625
     *
626
     * @param mixed      $val Valued to be inspected by range values.
627
     * @param int|string $x   Starting range value to be used in BETWEEN() function.
628
     * @param int|string $y   End point value to be used in BETWEEN() function.
629
     *
630
     * @return Expr\Func A BETWEEN expression.
631
     */
632 1
    public function between($val, $x, $y)
633
    {
634 1
        return $val . ' BETWEEN ' . $x . ' AND ' . $y;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $val . ' BETWEEN ' . $x . ' AND ' . $y returns the type string which is incompatible with the documented return type Doctrine\ORM\Query\Expr\Func.
Loading history...
635
    }
636
637
    /**
638
     * Creates an instance of TRIM() function, with the given argument.
639
     *
640
     * @param mixed $x Argument to be used as argument of TRIM() function.
641
     *
642
     * @return Expr\Func a TRIM expression.
643
     */
644 1
    public function trim($x)
645
    {
646 1
        return new Expr\Func('TRIM', $x);
647
    }
648
649
    /**
650
     * Creates an instance of MEMBER OF function, with the given arguments.
651
     *
652
     * @param string $x Value to be checked
653
     * @param string $y Value to be checked against
654
     *
655
     * @return Expr\Comparison
656
     */
657 1
    public function isMemberOf($x, $y)
658
    {
659 1
        return new Expr\Comparison($x, 'MEMBER OF', $y);
660
    }
661
662
    /**
663
     * Creates an instance of INSTANCE OF function, with the given arguments.
664
     *
665
     * @param string $x Value to be checked
666
     * @param string $y Value to be checked against
667
     *
668
     * @return Expr\Comparison
669
     */
670 1
    public function isInstanceOf($x, $y)
671
    {
672 1
        return new Expr\Comparison($x, 'INSTANCE OF', $y);
673
    }
674
}
675