Completed
Pull Request — master (#6397)
by Vytautas
12:44
created

SqlValueVisitor::walkComparison()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 8.8571
cc 6
eloc 10
nc 3
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Persisters;
21
22
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
23
use Doctrine\Common\Collections\Expr\Comparison;
24
use Doctrine\Common\Collections\Expr\Value;
25
use Doctrine\Common\Collections\Expr\CompositeExpression;
26
27
/**
28
 * Extract the values from a criteria/expression
29
 *
30
 * @author Benjamin Eberlei <[email protected]>
31
 */
32
class SqlValueVisitor extends ExpressionVisitor
33
{
34
    /**
35
     * @var array
36
     */
37
    private $values = [];
38
39
    /**
40
     * @var array
41
     */
42
    private $types  = [];
43
44
    /**
45
     * Converts a comparison expression into the target query language output.
46
     *
47
     * @param \Doctrine\Common\Collections\Expr\Comparison $comparison
48
     *
49
     * @return mixed
50
     */
51 54
    public function walkComparison(Comparison $comparison)
52
    {
53 54
        $value          = $this->getValueFromComparison($comparison);
54 54
        $field          = $comparison->getField();
55 54
        $operator       = $comparison->getOperator();
56
57 54
        if (($operator === Comparison::EQ || $operator === Comparison::IS) && $value === null) {
58 3
            return;
59 51
        } else if ($operator === Comparison::NEQ && $value === null) {
60 1
            return;
61
        }
62
63 50
        $this->values[] = $value;
64 50
        $this->types[]  = [$field, $value];
65 50
    }
66
67
    /**
68
     * Converts a composite expression into the target query language output.
69
     *
70
     * @param \Doctrine\Common\Collections\Expr\CompositeExpression $expr
71
     *
72
     * @return mixed
73
     */
74 8
    public function walkCompositeExpression(CompositeExpression $expr)
75
    {
76 8
        foreach ($expr->getExpressionList() as $child) {
77 8
            $this->dispatch($child);
78
        }
79 8
    }
80
81
    /**
82
     * Converts a value expression into the target query language part.
83
     *
84
     * @param \Doctrine\Common\Collections\Expr\Value $value
85
     *
86
     * @return mixed
87
     */
88
    public function walkValue(Value $value)
89
    {
90
        return;
91
    }
92
93
    /**
94
     * Returns the Parameters and Types necessary for matching the last visited expression.
95
     *
96
     * @return array
97
     */
98 54
    public function getParamsAndTypes()
99
    {
100 54
        return [$this->values, $this->types];
101
    }
102
103
    /**
104
     * Returns the value from a Comparison. In case of a CONTAINS comparison,
105
     * the value is wrapped in %-signs, because it will be used in a LIKE clause.
106
     *
107
     * @param \Doctrine\Common\Collections\Expr\Comparison $comparison
108
     * @return mixed
109
     */
110 54
    protected function getValueFromComparison(Comparison $comparison)
111
    {
112 54
        $value = $comparison->getValue()->getValue();
113
114 54
        switch ($comparison->getOperator()) {
115 54
            case Comparison::CONTAINS:
116 2
                return "%{$value}%";
117
118 53
            case Comparison::STARTS_WITH:
119
                return "{$value}%";
120
121 53
            case Comparison::ENDS_WITH:
122
                return "%{$value}";
123
124
            default:
125 53
                return $value;
126
        }
127
    }
128
}
129