Debian Codegraph @

vendor/doctrine/collections/src/Expr/CompositeExpression.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Doctrine\Common\Collections\Expr;
6
7 use RuntimeException;
8
9 use function count;
10
11 /**
12 * Expression of Expressions combined by AND or OR operation.
13 *
14 * @final since 2.5
15 */
16 class CompositeExpression implements Expression
17 {
18 final public const TYPE_AND = 'AND';
19 final public const TYPE_OR = 'OR';
20 final public const TYPE_NOT = 'NOT';
21
22 /** @var list<Expression> */
23 private array $expressions = [];
24
25 /**
26 * @param Expression[] $expressions
27 *
28 * @throws RuntimeException
29 */
30 public function __construct(private readonly string $type, array $expressions)
31 {
32 foreach ($expressions as $expr) {
33 if ($expr instanceof Value) {
34 throw new RuntimeException('Values are not supported expressions as children of and/or expressions.');
35 }
36
37 if (! ($expr instanceof Expression)) {
38 throw new RuntimeException('No expression given to CompositeExpression.');
39 }
40
41 $this->expressions[] = $expr;
42 }
43
44 if ($type === self::TYPE_NOT && count($this->expressions) !== 1) {
45 throw new RuntimeException('Not expression only allows one expression as child.');
46 }
47 }
48
49 /**
50 * Returns the list of expressions nested in this composite.
51 *
52 * @return list<Expression>
53 */
54 public function getExpressionList()
55 {
56 return $this->expressions;
57 }
58
59 /** @return string */
60 public function getType()
61 {
62 return $this->type;
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 public function visit(ExpressionVisitor $visitor)
69 {
70 return $visitor->walkCompositeExpression($this);
71 }
72 }