Completed
Push — master ( 68be21...073c54 )
by Kévin
02:41
created

GenericSqlVisitor::visitParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace RulerZ\Target;
4
5
use Hoa\Ruler\Model as AST;
6
7
use RulerZ\Compiler\Context;
8
use RulerZ\Exception\OperatorNotFoundException;
9
use RulerZ\Model;
10
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions;
11
12
/**
13
 * Base class for sql-related visitors.
14
 */
15
class GenericSqlVisitor extends GenericVisitor
16
{
17
    use Polyfill\AccessPath;
18
19
    /**
20
     * @var Context
21
     */
22
    protected $context;
23
24
    /**
25
     * Allow star operator.
26
     *
27
     * @var bool
28
     */
29
    protected $allowStarOperator = true;
30
31
    /**
32
     * @param bool $allowStarOperator Whether to allow the star operator or not (ie: implicit support of unknown operators).
33
     */
34
    public function __construct(Context $context, OperatorsDefinitions $operators, $allowStarOperator = true)
35
    {
36
        parent::__construct($operators);
37
38
        $this->context = $context;
39
        $this->allowStarOperator = (bool) $allowStarOperator;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function visitModel(AST\Model $element, &$handle = null, $eldnah = null)
46
    {
47
        $sql = parent::visitModel($element, $handle, $eldnah);
48
49
        return '"' . $sql . '"';
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function visitParameter(Model\Parameter $element, &$handle = null, $eldnah = null)
56
    {
57
        // make it a placeholder
58
        return ':' . $element->getName();
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function visitAccess(AST\Bag\Context $element, &$handle = null, $eldnah = null)
65
    {
66
        return $this->flattenAccessPath($element);
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function visitArray(AST\Bag\RulerArray $element, &$handle = null, $eldnah = null)
73
    {
74
        $array = parent::visitArray($element, $handle, $eldnah);
75
76
        return sprintf('(%s)', implode(', ', $array));
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function visitOperator(AST\Operator $element, &$handle = null, $eldnah = null)
83
    {
84
        try {
85
            return parent::visitOperator($element, $handle, $eldnah);
86
        } catch (OperatorNotFoundException $e) {
87
            if (!$this->allowStarOperator) {
88
                throw $e;
89
            }
90
        }
91
92
        $arguments = array_map(function ($argument) use (&$handle, $eldnah) {
93
            return $argument->accept($this, $handle, $eldnah);
94
        }, $element->getArguments());
95
96
        return sprintf('%s(%s)', $element->getName(), implode(', ', $arguments));
97
    }
98
}
99