summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Query/Expr/Base.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Query/Expr/Base.php')
-rw-r--r--vendor/doctrine/orm/src/Query/Expr/Base.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Query/Expr/Base.php b/vendor/doctrine/orm/src/Query/Expr/Base.php
new file mode 100644
index 0000000..e0f2572
--- /dev/null
+++ b/vendor/doctrine/orm/src/Query/Expr/Base.php
@@ -0,0 +1,96 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Query\Expr;
6
7use InvalidArgumentException;
8use Stringable;
9
10use function array_key_exists;
11use function count;
12use function get_debug_type;
13use function implode;
14use function in_array;
15use function is_array;
16use function is_string;
17use function sprintf;
18
19/**
20 * Abstract base Expr class for building DQL parts.
21 *
22 * @link www.doctrine-project.org
23 */
24abstract class Base implements Stringable
25{
26 protected string $preSeparator = '(';
27 protected string $separator = ', ';
28 protected string $postSeparator = ')';
29
30 /** @var list<class-string> */
31 protected array $allowedClasses = [];
32
33 /** @var list<string|Stringable> */
34 protected array $parts = [];
35
36 public function __construct(mixed $args = [])
37 {
38 if (is_array($args) && array_key_exists(0, $args) && is_array($args[0])) {
39 $args = $args[0];
40 }
41
42 $this->addMultiple($args);
43 }
44
45 /**
46 * @param string[]|object[]|string|object $args
47 * @psalm-param list<string|object>|string|object $args
48 *
49 * @return $this
50 */
51 public function addMultiple(array|string|object $args = []): static
52 {
53 foreach ((array) $args as $arg) {
54 $this->add($arg);
55 }
56
57 return $this;
58 }
59
60 /**
61 * @return $this
62 *
63 * @throws InvalidArgumentException
64 */
65 public function add(mixed $arg): static
66 {
67 if ($arg !== null && (! $arg instanceof self || $arg->count() > 0)) {
68 // If we decide to keep Expr\Base instances, we can use this check
69 if (! is_string($arg) && ! in_array($arg::class, $this->allowedClasses, true)) {
70 throw new InvalidArgumentException(sprintf(
71 "Expression of type '%s' not allowed in this context.",
72 get_debug_type($arg),
73 ));
74 }
75
76 $this->parts[] = $arg;
77 }
78
79 return $this;
80 }
81
82 /** @psalm-return 0|positive-int */
83 public function count(): int
84 {
85 return count($this->parts);
86 }
87
88 public function __toString(): string
89 {
90 if ($this->count() === 1) {
91 return (string) $this->parts[0];
92 }
93
94 return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
95 }
96}