AST
9 months ago
Exec
9 months ago
Expr
9 months ago
Filter
9 months ago
Expr.php
9 months ago
FilterCollection.php
9 months ago
Lexer.php
9 months ago
OutputWalker.php
9 months ago
Parameter.php
9 months ago
ParameterTypeInferer.php
9 months ago
Parser.php
9 months ago
ParserResult.php
9 months ago
Printer.php
9 months ago
QueryException.php
9 months ago
QueryExpressionVisitor.php
9 months ago
ResultSetMapping.php
9 months ago
ResultSetMappingBuilder.php
9 months ago
SqlOutputWalker.php
9 months ago
SqlWalker.php
9 months ago
TokenType.php
9 months ago
TreeWalker.php
9 months ago
TreeWalkerAdapter.php
9 months ago
TreeWalkerChain.php
9 months ago
TreeWalkerChainIterator.php
9 months ago
index.php
9 months ago
QueryExpressionVisitor.php
126 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Query; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\Common\Collections\ArrayCollection; |
| 6 | use MailPoetVendor\Doctrine\Common\Collections\Expr\Comparison; |
| 7 | use MailPoetVendor\Doctrine\Common\Collections\Expr\CompositeExpression; |
| 8 | use MailPoetVendor\Doctrine\Common\Collections\Expr\ExpressionVisitor; |
| 9 | use MailPoetVendor\Doctrine\Common\Collections\Expr\Value; |
| 10 | use RuntimeException; |
| 11 | use function count; |
| 12 | use function defined; |
| 13 | use function str_replace; |
| 14 | use function str_starts_with; |
| 15 | class QueryExpressionVisitor extends ExpressionVisitor |
| 16 | { |
| 17 | private static $operatorMap = [Comparison::GT => Expr\Comparison::GT, Comparison::GTE => Expr\Comparison::GTE, Comparison::LT => Expr\Comparison::LT, Comparison::LTE => Expr\Comparison::LTE]; |
| 18 | private $queryAliases; |
| 19 | private $expr; |
| 20 | private $parameters = []; |
| 21 | public function __construct($queryAliases) |
| 22 | { |
| 23 | $this->queryAliases = $queryAliases; |
| 24 | $this->expr = new Expr(); |
| 25 | } |
| 26 | public function getParameters() |
| 27 | { |
| 28 | return new ArrayCollection($this->parameters); |
| 29 | } |
| 30 | public function clearParameters() |
| 31 | { |
| 32 | $this->parameters = []; |
| 33 | } |
| 34 | private static function convertComparisonOperator($criteriaOperator) |
| 35 | { |
| 36 | return self::$operatorMap[$criteriaOperator] ?? null; |
| 37 | } |
| 38 | public function walkCompositeExpression(CompositeExpression $expr) |
| 39 | { |
| 40 | $expressionList = []; |
| 41 | foreach ($expr->getExpressionList() as $child) { |
| 42 | $expressionList[] = $this->dispatch($child); |
| 43 | } |
| 44 | switch ($expr->getType()) { |
| 45 | case CompositeExpression::TYPE_AND: |
| 46 | return new Expr\Andx($expressionList); |
| 47 | case CompositeExpression::TYPE_OR: |
| 48 | return new Expr\Orx($expressionList); |
| 49 | default: |
| 50 | // Multiversion support for `doctrine/collections` before and after v2.1.0 |
| 51 | if (defined(CompositeExpression::class . '::TYPE_NOT') && $expr->getType() === CompositeExpression::TYPE_NOT) { |
| 52 | return $this->expr->not($expressionList[0]); |
| 53 | } |
| 54 | throw new RuntimeException('Unknown composite ' . $expr->getType()); |
| 55 | } |
| 56 | } |
| 57 | public function walkComparison(Comparison $comparison) |
| 58 | { |
| 59 | if (!isset($this->queryAliases[0])) { |
| 60 | throw new QueryException('No aliases are set before invoking walkComparison().'); |
| 61 | } |
| 62 | $field = $this->queryAliases[0] . '.' . $comparison->getField(); |
| 63 | foreach ($this->queryAliases as $alias) { |
| 64 | if (str_starts_with($comparison->getField() . '.', $alias . '.')) { |
| 65 | $field = $comparison->getField(); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | $parameterName = str_replace('.', '_', $comparison->getField()); |
| 70 | foreach ($this->parameters as $parameter) { |
| 71 | if ($parameter->getName() === $parameterName) { |
| 72 | $parameterName .= '_' . count($this->parameters); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | $parameter = new Parameter($parameterName, $this->walkValue($comparison->getValue())); |
| 77 | $placeholder = ':' . $parameterName; |
| 78 | switch ($comparison->getOperator()) { |
| 79 | case Comparison::IN: |
| 80 | $this->parameters[] = $parameter; |
| 81 | return $this->expr->in($field, $placeholder); |
| 82 | case Comparison::NIN: |
| 83 | $this->parameters[] = $parameter; |
| 84 | return $this->expr->notIn($field, $placeholder); |
| 85 | case Comparison::EQ: |
| 86 | case Comparison::IS: |
| 87 | if ($this->walkValue($comparison->getValue()) === null) { |
| 88 | return $this->expr->isNull($field); |
| 89 | } |
| 90 | $this->parameters[] = $parameter; |
| 91 | return $this->expr->eq($field, $placeholder); |
| 92 | case Comparison::NEQ: |
| 93 | if ($this->walkValue($comparison->getValue()) === null) { |
| 94 | return $this->expr->isNotNull($field); |
| 95 | } |
| 96 | $this->parameters[] = $parameter; |
| 97 | return $this->expr->neq($field, $placeholder); |
| 98 | case Comparison::CONTAINS: |
| 99 | $parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType()); |
| 100 | $this->parameters[] = $parameter; |
| 101 | return $this->expr->like($field, $placeholder); |
| 102 | case Comparison::MEMBER_OF: |
| 103 | return $this->expr->isMemberOf($comparison->getField(), $comparison->getValue()->getValue()); |
| 104 | case Comparison::STARTS_WITH: |
| 105 | $parameter->setValue($parameter->getValue() . '%', $parameter->getType()); |
| 106 | $this->parameters[] = $parameter; |
| 107 | return $this->expr->like($field, $placeholder); |
| 108 | case Comparison::ENDS_WITH: |
| 109 | $parameter->setValue('%' . $parameter->getValue(), $parameter->getType()); |
| 110 | $this->parameters[] = $parameter; |
| 111 | return $this->expr->like($field, $placeholder); |
| 112 | default: |
| 113 | $operator = self::convertComparisonOperator($comparison->getOperator()); |
| 114 | if ($operator) { |
| 115 | $this->parameters[] = $parameter; |
| 116 | return new Expr\Comparison($field, $operator, $placeholder); |
| 117 | } |
| 118 | throw new RuntimeException('Unknown comparison operator: ' . $comparison->getOperator()); |
| 119 | } |
| 120 | } |
| 121 | public function walkValue(Value $value) |
| 122 | { |
| 123 | return $value->getValue(); |
| 124 | } |
| 125 | } |
| 126 |