Passed
Push — master ( 9adc1b...ec3431 )
by Pavel
03:44
created

SimpleCriteriaVisitor::walkComparison()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 8
cp 0.625
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.4746
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Persister;
4
5
use Doctrine\Common\Collections\Expr\Comparison;
6
use Doctrine\Common\Collections\Expr\CompositeExpression;
7
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
8
use Doctrine\Common\Collections\Expr\Value;
9
10
final class SimpleCriteriaVisitor extends ExpressionVisitor
11
{
12
    /**
13
     * Converts a comparison expression into the target query language output.
14
     *
15
     * @param Comparison $comparison
16
     *
17
     * @return mixed
18
     */
19 1
    public function walkComparison(Comparison $comparison)
20
    {
21 1
        switch ($comparison->getOperator()) {
22 1
            case Comparison::EQ:
23 1
            case Comparison::IN:
24 1
                return [$comparison->getField() => $this->walkValue($comparison->getValue())];
25
            default:
26
                throw new \InvalidArgumentException('Simple API queries support only EQ and IN operators');
27
        }
28
    }
29
30
    /**
31
     * Converts a value expression into the target query language part.
32
     *
33
     * @param Value $value
34
     *
35
     * @return mixed
36
     */
37 1
    public function walkValue(Value $value)
38
    {
39 1
        return $value->getValue();
40
    }
41
42
    /**
43
     * Converts a composite expression into the target query language output.
44
     *
45
     * @param CompositeExpression $expr
46
     *
47
     * @return mixed
48
     */
49
    public function walkCompositeExpression(CompositeExpression $expr)
50
    {
51
        $expressions = [];
52
        switch ($expr->getType()) {
53
            case CompositeExpression::TYPE_AND:
54
                foreach ($expr->getExpressionList() as $expression) {
55
                    $expressions[] = $this->dispatch($expression);
56
                }
57
                break;
58
            default:
59
                throw new \InvalidArgumentException('Simple API queries support only AND composite expression');
60
        }
61
62
        return call_user_func_array('array_replace', $expressions);
63
    }
64
}
65