summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php')
-rw-r--r--vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php b/vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php
new file mode 100644
index 0000000..7f987ad
--- /dev/null
+++ b/vendor/doctrine/orm/src/Persisters/SqlValueVisitor.php
@@ -0,0 +1,88 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Persisters;
6
7use Doctrine\Common\Collections\Expr\Comparison;
8use Doctrine\Common\Collections\Expr\CompositeExpression;
9use Doctrine\Common\Collections\Expr\ExpressionVisitor;
10use Doctrine\Common\Collections\Expr\Value;
11
12/**
13 * Extract the values from a criteria/expression
14 */
15class SqlValueVisitor extends ExpressionVisitor
16{
17 /** @var mixed[] */
18 private array $values = [];
19
20 /** @var mixed[][] */
21 private array $types = [];
22
23 /**
24 * Converts a comparison expression into the target query language output.
25 *
26 * {@inheritDoc}
27 */
28 public function walkComparison(Comparison $comparison)
29 {
30 $value = $this->getValueFromComparison($comparison);
31
32 $this->values[] = $value;
33 $this->types[] = [$comparison->getField(), $value, $comparison->getOperator()];
34
35 return null;
36 }
37
38 /**
39 * Converts a composite expression into the target query language output.
40 *
41 * {@inheritDoc}
42 */
43 public function walkCompositeExpression(CompositeExpression $expr)
44 {
45 foreach ($expr->getExpressionList() as $child) {
46 $this->dispatch($child);
47 }
48
49 return null;
50 }
51
52 /**
53 * Converts a value expression into the target query language part.
54 *
55 * {@inheritDoc}
56 */
57 public function walkValue(Value $value)
58 {
59 return null;
60 }
61
62 /**
63 * Returns the Parameters and Types necessary for matching the last visited expression.
64 *
65 * @return mixed[][]
66 * @psalm-return array{0: array, 1: array<array<mixed>>}
67 */
68 public function getParamsAndTypes(): array
69 {
70 return [$this->values, $this->types];
71 }
72
73 /**
74 * Returns the value from a Comparison. In case of a CONTAINS comparison,
75 * the value is wrapped in %-signs, because it will be used in a LIKE clause.
76 */
77 protected function getValueFromComparison(Comparison $comparison): mixed
78 {
79 $value = $comparison->getValue()->getValue();
80
81 return match ($comparison->getOperator()) {
82 Comparison::CONTAINS => '%' . $value . '%',
83 Comparison::STARTS_WITH => $value . '%',
84 Comparison::ENDS_WITH => '%' . $value,
85 default => $value,
86 };
87 }
88}