diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Query/Expr/Base.php')
-rw-r--r-- | vendor/doctrine/orm/src/Query/Expr/Base.php | 96 |
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Query\Expr; | ||
6 | |||
7 | use InvalidArgumentException; | ||
8 | use Stringable; | ||
9 | |||
10 | use function array_key_exists; | ||
11 | use function count; | ||
12 | use function get_debug_type; | ||
13 | use function implode; | ||
14 | use function in_array; | ||
15 | use function is_array; | ||
16 | use function is_string; | ||
17 | use function sprintf; | ||
18 | |||
19 | /** | ||
20 | * Abstract base Expr class for building DQL parts. | ||
21 | * | ||
22 | * @link www.doctrine-project.org | ||
23 | */ | ||
24 | abstract 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 | } | ||