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