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

SymfonyExpression   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 68
ccs 21
cts 23
cp 0.913
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpression() 0 8 2
A __invoke() 0 11 2
A getKeys() 0 4 1
A __construct() 0 10 2
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