Completed
Push — master ( eff795...9418cb )
by Oliver
08:18
created

SymfonyExpression::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 2.0438
1
<?php
2
3
namespace Metabor\Callback;
4
5
use MetaborStd\CallbackInterface;
6
use Symfony\Component\ExpressionLanguage\Expression;
7
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
8
use Symfony\Component\ExpressionLanguage\ParsedExpression;
9
10
/**
11
 * @author otischlinger
12
 */
13
class SymfonyExpression extends Expression implements CallbackInterface
14
{
15
    /**
16
     * @var ExpressionLanguage
17
     */
18
    private $expressionLanguage;
19
20
    /**
21
     * @var ParsedExpression
22
     */
23
    private $parsedExpression;
24
25
    /**
26
     * @var array
27
     */
28
    private $keys;
29
30
    /**
31
     * @param string             $expression
32
     * @param array              $keys
33
     * @param ExpressionLanguage $expressionLanguage
34
     */
35 3
    public function __construct($expression, array $keys = array(), ExpressionLanguage $expressionLanguage = null)
36 1
    {
37 3
        parent::__construct($expression);
38 3
        $this->keys = $keys;
39 3
        if ($expressionLanguage) {
40
            $this->expressionLanguage = $expressionLanguage;
41
        } else {
42 3
            $this->expressionLanguage = new ExpressionLanguage();
43
        }
44 3
    }
45
46
    /**
47
     * @return \Symfony\Component\ExpressionLanguage\ParsedExpression
48
     */
49 2
    protected function getExpression()
50
    {
51 2
        if (!$this->parsedExpression) {
52 2
            $this->parsedExpression = $this->expressionLanguage->parse($this, $this->keys);
53 2
        }
54
55 2
        return $this->parsedExpression;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 2
    public function __invoke()
62
    {
63 2
        if (empty($this->keys)) {
64 1
            $values = array();
65 1
        } else {
66 1
            $args = func_get_args();
67 1
            $values = array_combine($this->keys, $args);
68
        }
69
70 2
        return $this->expressionLanguage->evaluate($this->getExpression(), $values);
71
    }
72
73
    /**
74
     * @return array
75
     */
76 1
    public function getKeys()
77
    {
78 1
        return $this->keys;
79
    }
80
}
81