Completed
Push — master ( 8c259e...a3e53b )
by Guilherme
27:10 queued 12:02
created

SqlExpressionVisitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 71
ccs 22
cts 27
cp 0.8148
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A walkComparison() 0 17 5
A walkCompositeExpression() 0 15 4
A walkValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Persisters;
6
7
use Doctrine\Common\Collections\Expr\Comparison;
8
use Doctrine\Common\Collections\Expr\CompositeExpression;
9
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
10
use Doctrine\Common\Collections\Expr\Value;
11
use Doctrine\ORM\Mapping\AssociationMetadata;
12
use Doctrine\ORM\Mapping\ClassMetadata;
13
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
14
use RuntimeException;
15
use function implode;
16
use function in_array;
17
use function is_object;
18
19
/**
20
 * Visit Expressions and generate SQL WHERE conditions from them.
21
 */
22
class SqlExpressionVisitor extends ExpressionVisitor
23
{
24
    /** @var BasicEntityPersister */
25
    private $persister;
26
27
    /** @var ClassMetadata */
28
    private $classMetadata;
29
30 38
    public function __construct(BasicEntityPersister $persister, ClassMetadata $classMetadata)
31
    {
32 38
        $this->persister     = $persister;
33 38
        $this->classMetadata = $classMetadata;
34 38
    }
35
36
    /**
37
     * Converts a comparison expression into the target query language output.
38
     *
39
     * @return mixed
40
     */
41 38
    public function walkComparison(Comparison $comparison)
42
    {
43 38
        $field    = $comparison->getField();
44 38
        $value    = $comparison->getValue()->getValue(); // shortcut for walkValue()
45 38
        $property = $this->classMetadata->getProperty($field);
46
47 38
        if ($property instanceof AssociationMetadata &&
48 38
            $value !== null &&
49 38
            ! is_object($value) &&
50 38
            ! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true)) {
51 1
            throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation(
52 1
                $this->classMetadata->getClassName(),
53
                $field
54
            );
55
        }
56
57 37
        return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator());
58
    }
59
60
    /**
61
     * Converts a composite expression into the target query language output.
62
     *
63
     * @return mixed
64
     *
65
     * @throws RuntimeException
66
     */
67 4
    public function walkCompositeExpression(CompositeExpression $expr)
68
    {
69 4
        $expressionList = [];
70
71 4
        foreach ($expr->getExpressionList() as $child) {
72 4
            $expressionList[] = $this->dispatch($child);
73
        }
74
75 4
        switch ($expr->getType()) {
76 4
            case CompositeExpression::TYPE_AND:
77 4
                return '(' . implode(' AND ', $expressionList) . ')';
78
            case CompositeExpression::TYPE_OR:
79
                return '(' . implode(' OR ', $expressionList) . ')';
80
            default:
81
                throw new RuntimeException('Unknown composite ' . $expr->getType());
82
        }
83
    }
84
85
    /**
86
     * Converts a value expression into the target query language part.
87
     *
88
     * @return mixed
89
     */
90
    public function walkValue(Value $value)
91
    {
92
        return '?';
93
    }
94
}
95