summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php')
-rw-r--r--vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php98
1 files changed, 98 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php
new file mode 100644
index 0000000..efe307a
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php
@@ -0,0 +1,98 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Query\Expression;
6
7use Countable;
8
9use function array_merge;
10use function array_values;
11use function count;
12use function implode;
13
14/**
15 * Composite expression is responsible to build a group of similar expression.
16 *
17 * This class is immutable.
18 */
19class CompositeExpression implements Countable
20{
21 /**
22 * Constant that represents an AND composite expression.
23 */
24 final public const TYPE_AND = 'AND';
25
26 /**
27 * Constant that represents an OR composite expression.
28 */
29 final public const TYPE_OR = 'OR';
30
31 /**
32 * Each expression part of the composite expression.
33 *
34 * @var array<int, self|string>
35 */
36 private array $parts;
37
38 /** @internal Use the and() / or() factory methods. */
39 public function __construct(
40 private readonly string $type,
41 self|string $part,
42 self|string ...$parts,
43 ) {
44 $this->parts = array_merge([$part], array_values($parts));
45 }
46
47 public static function and(self|string $part, self|string ...$parts): self
48 {
49 return new self(self::TYPE_AND, $part, ...$parts);
50 }
51
52 public static function or(self|string $part, self|string ...$parts): self
53 {
54 return new self(self::TYPE_OR, $part, ...$parts);
55 }
56
57 /**
58 * Returns a new CompositeExpression with the given parts added.
59 */
60 public function with(self|string $part, self|string ...$parts): self
61 {
62 $that = clone $this;
63
64 $that->parts = array_merge($that->parts, [$part], array_values($parts));
65
66 return $that;
67 }
68
69 /**
70 * Retrieves the amount of expressions on composite expression.
71 *
72 * @psalm-return int<0, max>
73 */
74 public function count(): int
75 {
76 return count($this->parts);
77 }
78
79 /**
80 * Retrieves the string representation of this composite expression.
81 */
82 public function __toString(): string
83 {
84 if ($this->count() === 1) {
85 return (string) $this->parts[0];
86 }
87
88 return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
89 }
90
91 /**
92 * Returns the type of this composite expression (AND/OR).
93 */
94 public function getType(): string
95 {
96 return $this->type;
97 }
98}