Completed
Push — master ( 41142f...0129b8 )
by Kévin
02:09
created

AbstractCompilationTarget::getRuleIdentifierHint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace RulerZ\Target;
4
5
use RulerZ\Compiler\Context;
6
use RulerZ\Compiler\CompilationTarget;
7
use RulerZ\Model;
8
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions;
9
10
/**
11
 * Generic visitor intended to be extended.
12
 */
13
abstract class AbstractCompilationTarget implements CompilationTarget
14
{
15
    /**
16
     * @var OperatorsDefinitions
17
     */
18
    private $customOperators;
19
20
    /**
21
     * Create a rule visitor for a given compilation context.
22
     *
23
     * @param Context $context The compilation context.
24
     *
25
     * @return \RulerZ\Compiler\RuleVisitor
26
     */
27
    abstract protected function createVisitor(Context $context);
28
29
    abstract protected function getExecutorTraits();
30
31
    /**
32
     * @param array<callable> $operators A list of additional operators to register.
33
     * @param array<callable> $inlineOperators A list of additional inline operators to register.
34
     */
35
    public function __construct(array $operators = [], array $inlineOperators = [])
36
    {
37
        $this->customOperators = new OperatorsDefinitions($operators, $inlineOperators);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function compile(Model\Rule $rule, Context $compilationContext)
44
    {
45
        $visitor = $this->createVisitor($compilationContext);
46
        $compiledCode = $visitor->visit($rule);
47
48
        return new Model\Executor(
49
            $this->getExecutorTraits(),
50
            $compiledCode,
51
            $visitor->getCompilationData()
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function createCompilationContext($target)
59
    {
60
        return new Context();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function defineOperator($name, callable $transformer)
67
    {
68
        $this->customOperators->defineOperator($name, $transformer);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function defineInlineOperator($name, callable $transformer)
75
    {
76
        $this->customOperators->defineInlineOperator($name, $transformer);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getOperators()
83
    {
84
        return $this->customOperators;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getRuleIdentifierHint($rule, Context $context)
91
    {
92
        return '';
93
    }
94
}
95