Completed
Push — master ( 21023f...3b4212 )
by Kévin
02:35
created

SolariumVisitor::defineBuiltInOperators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace RulerZ\Compiler\Target\Solr;
4
5
use Hoa\Ruler\Model as AST;
6
use Solarium\Client as SolariumClient;
7
8
use RulerZ\Compiler\Target\GenericVisitor;
9
use RulerZ\Model;
10
11
class SolariumVisitor extends GenericVisitor
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    public function supports($target, $mode)
17
    {
18
        return $target instanceof SolariumClient;
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24
    protected function getExecutorTraits()
25
    {
26
        return [
27
            '\RulerZ\Executor\Solr\SolariumFilterTrait',
28
            '\RulerZ\Executor\Polyfill\FilterBasedSatisfaction',
29
        ];
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function visitModel(AST\Model $element, &$handle = null, $eldnah = null)
36
    {
37
        $searchQuery = parent::visitModel($element, $handle, $eldnah);
38
39
        return "'" . $searchQuery . "'";
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function visitAccess(AST\Bag\Context $element, &$handle = null, $eldnah = null)
46
    {
47
        return $element->getId();
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function visitScalar(AST\Bag\Scalar $element, &$handle = null, $eldnah = null)
54
    {
55
        $value = $element->getValue();
56
57
        return is_numeric($value) ? $value : sprintf('"%s"', $value);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function visitParameter(Model\Parameter $element, &$handle = null, $eldnah = null)
64
    {
65
        // FIXME the parameters handling is REALLY hacky
66
        $parameterName = $element->getName();
67
68
        return "'. \$parameters['$parameterName'] .'";
69
    }
70
71
    /**
72
     * Define the built-in operators.
73
     */
74
    protected function defineBuiltInOperators()
75
    {
76
        $this->setInlineOperator('and',  function ($a, $b) { return sprintf('(%s AND %s)', $a, $b); });
77
        $this->setInlineOperator('or',   function ($a, $b) { return sprintf('(%s OR %s)', $a, $b); });
78
        $this->setInlineOperator('not',  function ($a)     { return sprintf('-(%s)', $a); });
79
        $this->setInlineOperator('=',    function ($a, $b) { return sprintf('%s:%s', $a, $b); });
80
        $this->setInlineOperator('!=',   function ($a, $b) { return sprintf('-%s:%s', $a, $b); });
81
        $this->setInlineOperator('>',    function ($a, $b) { return sprintf('%s:{%s TO *]', $a,  $b); });
82
        $this->setInlineOperator('>=',   function ($a, $b) { return sprintf('%s:[%s TO *]', $a,  $b); });
83
        $this->setInlineOperator('<',    function ($a, $b) { return sprintf('%s:[* TO %s}', $a,  $b); });
84
        $this->setInlineOperator('<=',   function ($a, $b) { return sprintf('%s:[* TO %s]', $a,  $b); });
85
        $this->setInlineOperator('in',   function ($a, $b) { return sprintf('%s:(%s)', $a, implode(' OR ', $b)); });
86
    }
87
}
88