diff options
Diffstat (limited to 'vendor/doctrine/collections/src/Expr')
6 files changed, 446 insertions, 0 deletions
diff --git a/vendor/doctrine/collections/src/Expr/ClosureExpressionVisitor.php b/vendor/doctrine/collections/src/Expr/ClosureExpressionVisitor.php new file mode 100644 index 0000000..4319b9b --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/ClosureExpressionVisitor.php | |||
@@ -0,0 +1,222 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Common\Collections\Expr; | ||
6 | |||
7 | use ArrayAccess; | ||
8 | use Closure; | ||
9 | use RuntimeException; | ||
10 | |||
11 | use function explode; | ||
12 | use function in_array; | ||
13 | use function is_array; | ||
14 | use function is_scalar; | ||
15 | use function iterator_to_array; | ||
16 | use function method_exists; | ||
17 | use function preg_match; | ||
18 | use function preg_replace_callback; | ||
19 | use function str_contains; | ||
20 | use function str_ends_with; | ||
21 | use function str_starts_with; | ||
22 | use function strtoupper; | ||
23 | |||
24 | /** | ||
25 | * Walks an expression graph and turns it into a PHP closure. | ||
26 | * | ||
27 | * This closure can be used with {@Collection#filter()} and is used internally | ||
28 | * by {@ArrayCollection#select()}. | ||
29 | */ | ||
30 | class ClosureExpressionVisitor extends ExpressionVisitor | ||
31 | { | ||
32 | /** | ||
33 | * Accesses the field of a given object. This field has to be public | ||
34 | * directly or indirectly (through an accessor get*, is*, or a magic | ||
35 | * method, __get, __call). | ||
36 | * | ||
37 | * @param object|mixed[] $object | ||
38 | * | ||
39 | * @return mixed | ||
40 | */ | ||
41 | public static function getObjectFieldValue(object|array $object, string $field) | ||
42 | { | ||
43 | if (str_contains($field, '.')) { | ||
44 | [$field, $subField] = explode('.', $field, 2); | ||
45 | $object = self::getObjectFieldValue($object, $field); | ||
46 | |||
47 | return self::getObjectFieldValue($object, $subField); | ||
48 | } | ||
49 | |||
50 | if (is_array($object)) { | ||
51 | return $object[$field]; | ||
52 | } | ||
53 | |||
54 | $accessors = ['get', 'is', '']; | ||
55 | |||
56 | foreach ($accessors as $accessor) { | ||
57 | $accessor .= $field; | ||
58 | |||
59 | if (method_exists($object, $accessor)) { | ||
60 | return $object->$accessor(); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | if (preg_match('/^is[A-Z]+/', $field) === 1 && method_exists($object, $field)) { | ||
65 | return $object->$field(); | ||
66 | } | ||
67 | |||
68 | // __call should be triggered for get. | ||
69 | $accessor = $accessors[0] . $field; | ||
70 | |||
71 | if (method_exists($object, '__call')) { | ||
72 | return $object->$accessor(); | ||
73 | } | ||
74 | |||
75 | if ($object instanceof ArrayAccess) { | ||
76 | return $object[$field]; | ||
77 | } | ||
78 | |||
79 | if (isset($object->$field)) { | ||
80 | return $object->$field; | ||
81 | } | ||
82 | |||
83 | // camelcase field name to support different variable naming conventions | ||
84 | $ccField = preg_replace_callback('/_(.?)/', static fn ($matches) => strtoupper((string) $matches[1]), $field); | ||
85 | |||
86 | foreach ($accessors as $accessor) { | ||
87 | $accessor .= $ccField; | ||
88 | |||
89 | if (method_exists($object, $accessor)) { | ||
90 | return $object->$accessor(); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | return $object->$field; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * Helper for sorting arrays of objects based on multiple fields + orientations. | ||
99 | * | ||
100 | * @return Closure | ||
101 | */ | ||
102 | public static function sortByField(string $name, int $orientation = 1, Closure|null $next = null) | ||
103 | { | ||
104 | if (! $next) { | ||
105 | $next = static fn (): int => 0; | ||
106 | } | ||
107 | |||
108 | return static function ($a, $b) use ($name, $next, $orientation): int { | ||
109 | $aValue = ClosureExpressionVisitor::getObjectFieldValue($a, $name); | ||
110 | |||
111 | $bValue = ClosureExpressionVisitor::getObjectFieldValue($b, $name); | ||
112 | |||
113 | if ($aValue === $bValue) { | ||
114 | return $next($a, $b); | ||
115 | } | ||
116 | |||
117 | return ($aValue > $bValue ? 1 : -1) * $orientation; | ||
118 | }; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * {@inheritDoc} | ||
123 | */ | ||
124 | public function walkComparison(Comparison $comparison) | ||
125 | { | ||
126 | $field = $comparison->getField(); | ||
127 | $value = $comparison->getValue()->getValue(); | ||
128 | |||
129 | return match ($comparison->getOperator()) { | ||
130 | Comparison::EQ => static fn ($object): bool => self::getObjectFieldValue($object, $field) === $value, | ||
131 | Comparison::NEQ => static fn ($object): bool => self::getObjectFieldValue($object, $field) !== $value, | ||
132 | Comparison::LT => static fn ($object): bool => self::getObjectFieldValue($object, $field) < $value, | ||
133 | Comparison::LTE => static fn ($object): bool => self::getObjectFieldValue($object, $field) <= $value, | ||
134 | Comparison::GT => static fn ($object): bool => self::getObjectFieldValue($object, $field) > $value, | ||
135 | Comparison::GTE => static fn ($object): bool => self::getObjectFieldValue($object, $field) >= $value, | ||
136 | Comparison::IN => static function ($object) use ($field, $value): bool { | ||
137 | $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); | ||
138 | |||
139 | return in_array($fieldValue, $value, is_scalar($fieldValue)); | ||
140 | }, | ||
141 | Comparison::NIN => static function ($object) use ($field, $value): bool { | ||
142 | $fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field); | ||
143 | |||
144 | return ! in_array($fieldValue, $value, is_scalar($fieldValue)); | ||
145 | }, | ||
146 | Comparison::CONTAINS => static fn ($object): bool => str_contains((string) self::getObjectFieldValue($object, $field), (string) $value), | ||
147 | Comparison::MEMBER_OF => static function ($object) use ($field, $value): bool { | ||
148 | $fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field); | ||
149 | |||
150 | if (! is_array($fieldValues)) { | ||
151 | $fieldValues = iterator_to_array($fieldValues); | ||
152 | } | ||
153 | |||
154 | return in_array($value, $fieldValues, true); | ||
155 | }, | ||
156 | Comparison::STARTS_WITH => static fn ($object): bool => str_starts_with((string) self::getObjectFieldValue($object, $field), (string) $value), | ||
157 | Comparison::ENDS_WITH => static fn ($object): bool => str_ends_with((string) self::getObjectFieldValue($object, $field), (string) $value), | ||
158 | default => throw new RuntimeException('Unknown comparison operator: ' . $comparison->getOperator()), | ||
159 | }; | ||
160 | } | ||
161 | |||
162 | /** | ||
163 | * {@inheritDoc} | ||
164 | */ | ||
165 | public function walkValue(Value $value) | ||
166 | { | ||
167 | return $value->getValue(); | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * {@inheritDoc} | ||
172 | */ | ||
173 | public function walkCompositeExpression(CompositeExpression $expr) | ||
174 | { | ||
175 | $expressionList = []; | ||
176 | |||
177 | foreach ($expr->getExpressionList() as $child) { | ||
178 | $expressionList[] = $this->dispatch($child); | ||
179 | } | ||
180 | |||
181 | return match ($expr->getType()) { | ||
182 | CompositeExpression::TYPE_AND => $this->andExpressions($expressionList), | ||
183 | CompositeExpression::TYPE_OR => $this->orExpressions($expressionList), | ||
184 | CompositeExpression::TYPE_NOT => $this->notExpression($expressionList), | ||
185 | default => throw new RuntimeException('Unknown composite ' . $expr->getType()), | ||
186 | }; | ||
187 | } | ||
188 | |||
189 | /** @param callable[] $expressions */ | ||
190 | private function andExpressions(array $expressions): Closure | ||
191 | { | ||
192 | return static function ($object) use ($expressions): bool { | ||
193 | foreach ($expressions as $expression) { | ||
194 | if (! $expression($object)) { | ||
195 | return false; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | return true; | ||
200 | }; | ||
201 | } | ||
202 | |||
203 | /** @param callable[] $expressions */ | ||
204 | private function orExpressions(array $expressions): Closure | ||
205 | { | ||
206 | return static function ($object) use ($expressions): bool { | ||
207 | foreach ($expressions as $expression) { | ||
208 | if ($expression($object)) { | ||
209 | return true; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | return false; | ||
214 | }; | ||
215 | } | ||
216 | |||
217 | /** @param callable[] $expressions */ | ||
218 | private function notExpression(array $expressions): Closure | ||
219 | { | ||
220 | return static fn ($object) => ! $expressions[0]($object); | ||
221 | } | ||
222 | } | ||
diff --git a/vendor/doctrine/collections/src/Expr/Comparison.php b/vendor/doctrine/collections/src/Expr/Comparison.php new file mode 100644 index 0000000..f1ea07f --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/Comparison.php | |||
@@ -0,0 +1,62 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Common\Collections\Expr; | ||
6 | |||
7 | /** | ||
8 | * Comparison of a field with a value by the given operator. | ||
9 | */ | ||
10 | class Comparison implements Expression | ||
11 | { | ||
12 | final public const EQ = '='; | ||
13 | final public const NEQ = '<>'; | ||
14 | final public const LT = '<'; | ||
15 | final public const LTE = '<='; | ||
16 | final public const GT = '>'; | ||
17 | final public const GTE = '>='; | ||
18 | final public const IS = '='; // no difference with EQ | ||
19 | final public const IN = 'IN'; | ||
20 | final public const NIN = 'NIN'; | ||
21 | final public const CONTAINS = 'CONTAINS'; | ||
22 | final public const MEMBER_OF = 'MEMBER_OF'; | ||
23 | final public const STARTS_WITH = 'STARTS_WITH'; | ||
24 | final public const ENDS_WITH = 'ENDS_WITH'; | ||
25 | |||
26 | private readonly Value $value; | ||
27 | |||
28 | public function __construct(private readonly string $field, private readonly string $op, mixed $value) | ||
29 | { | ||
30 | if (! ($value instanceof Value)) { | ||
31 | $value = new Value($value); | ||
32 | } | ||
33 | |||
34 | $this->value = $value; | ||
35 | } | ||
36 | |||
37 | /** @return string */ | ||
38 | public function getField() | ||
39 | { | ||
40 | return $this->field; | ||
41 | } | ||
42 | |||
43 | /** @return Value */ | ||
44 | public function getValue() | ||
45 | { | ||
46 | return $this->value; | ||
47 | } | ||
48 | |||
49 | /** @return string */ | ||
50 | public function getOperator() | ||
51 | { | ||
52 | return $this->op; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * {@inheritDoc} | ||
57 | */ | ||
58 | public function visit(ExpressionVisitor $visitor) | ||
59 | { | ||
60 | return $visitor->walkComparison($this); | ||
61 | } | ||
62 | } | ||
diff --git a/vendor/doctrine/collections/src/Expr/CompositeExpression.php b/vendor/doctrine/collections/src/Expr/CompositeExpression.php new file mode 100644 index 0000000..b7b4544 --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/CompositeExpression.php | |||
@@ -0,0 +1,70 @@ | |||
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 | class CompositeExpression implements Expression | ||
15 | { | ||
16 | final public const TYPE_AND = 'AND'; | ||
17 | final public const TYPE_OR = 'OR'; | ||
18 | final public const TYPE_NOT = 'NOT'; | ||
19 | |||
20 | /** @var list<Expression> */ | ||
21 | private array $expressions = []; | ||
22 | |||
23 | /** | ||
24 | * @param Expression[] $expressions | ||
25 | * | ||
26 | * @throws RuntimeException | ||
27 | */ | ||
28 | public function __construct(private readonly string $type, array $expressions) | ||
29 | { | ||
30 | foreach ($expressions as $expr) { | ||
31 | if ($expr instanceof Value) { | ||
32 | throw new RuntimeException('Values are not supported expressions as children of and/or expressions.'); | ||
33 | } | ||
34 | |||
35 | if (! ($expr instanceof Expression)) { | ||
36 | throw new RuntimeException('No expression given to CompositeExpression.'); | ||
37 | } | ||
38 | |||
39 | $this->expressions[] = $expr; | ||
40 | } | ||
41 | |||
42 | if ($type === self::TYPE_NOT && count($this->expressions) !== 1) { | ||
43 | throw new RuntimeException('Not expression only allows one expression as child.'); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Returns the list of expressions nested in this composite. | ||
49 | * | ||
50 | * @return list<Expression> | ||
51 | */ | ||
52 | public function getExpressionList() | ||
53 | { | ||
54 | return $this->expressions; | ||
55 | } | ||
56 | |||
57 | /** @return string */ | ||
58 | public function getType() | ||
59 | { | ||
60 | return $this->type; | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * {@inheritDoc} | ||
65 | */ | ||
66 | public function visit(ExpressionVisitor $visitor) | ||
67 | { | ||
68 | return $visitor->walkCompositeExpression($this); | ||
69 | } | ||
70 | } | ||
diff --git a/vendor/doctrine/collections/src/Expr/Expression.php b/vendor/doctrine/collections/src/Expr/Expression.php new file mode 100644 index 0000000..70ad45f --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/Expression.php | |||
@@ -0,0 +1,14 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Common\Collections\Expr; | ||
6 | |||
7 | /** | ||
8 | * Expression for the {@link Selectable} interface. | ||
9 | */ | ||
10 | interface Expression | ||
11 | { | ||
12 | /** @return mixed */ | ||
13 | public function visit(ExpressionVisitor $visitor); | ||
14 | } | ||
diff --git a/vendor/doctrine/collections/src/Expr/ExpressionVisitor.php b/vendor/doctrine/collections/src/Expr/ExpressionVisitor.php new file mode 100644 index 0000000..afbd3d1 --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/ExpressionVisitor.php | |||
@@ -0,0 +1,52 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Common\Collections\Expr; | ||
6 | |||
7 | use RuntimeException; | ||
8 | |||
9 | /** | ||
10 | * An Expression visitor walks a graph of expressions and turns them into a | ||
11 | * query for the underlying implementation. | ||
12 | */ | ||
13 | abstract class ExpressionVisitor | ||
14 | { | ||
15 | /** | ||
16 | * Converts a comparison expression into the target query language output. | ||
17 | * | ||
18 | * @return mixed | ||
19 | */ | ||
20 | abstract public function walkComparison(Comparison $comparison); | ||
21 | |||
22 | /** | ||
23 | * Converts a value expression into the target query language part. | ||
24 | * | ||
25 | * @return mixed | ||
26 | */ | ||
27 | abstract public function walkValue(Value $value); | ||
28 | |||
29 | /** | ||
30 | * Converts a composite expression into the target query language output. | ||
31 | * | ||
32 | * @return mixed | ||
33 | */ | ||
34 | abstract public function walkCompositeExpression(CompositeExpression $expr); | ||
35 | |||
36 | /** | ||
37 | * Dispatches walking an expression to the appropriate handler. | ||
38 | * | ||
39 | * @return mixed | ||
40 | * | ||
41 | * @throws RuntimeException | ||
42 | */ | ||
43 | public function dispatch(Expression $expr) | ||
44 | { | ||
45 | return match (true) { | ||
46 | $expr instanceof Comparison => $this->walkComparison($expr), | ||
47 | $expr instanceof Value => $this->walkValue($expr), | ||
48 | $expr instanceof CompositeExpression => $this->walkCompositeExpression($expr), | ||
49 | default => throw new RuntimeException('Unknown Expression ' . $expr::class), | ||
50 | }; | ||
51 | } | ||
52 | } | ||
diff --git a/vendor/doctrine/collections/src/Expr/Value.php b/vendor/doctrine/collections/src/Expr/Value.php new file mode 100644 index 0000000..776770d --- /dev/null +++ b/vendor/doctrine/collections/src/Expr/Value.php | |||
@@ -0,0 +1,26 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Common\Collections\Expr; | ||
6 | |||
7 | class Value implements Expression | ||
8 | { | ||
9 | public function __construct(private readonly mixed $value) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | /** @return mixed */ | ||
14 | public function getValue() | ||
15 | { | ||
16 | return $this->value; | ||
17 | } | ||
18 | |||
19 | /** | ||
20 | * {@inheritDoc} | ||
21 | */ | ||
22 | public function visit(ExpressionVisitor $visitor) | ||
23 | { | ||
24 | return $visitor->walkValue($this); | ||
25 | } | ||
26 | } | ||