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
|
|
|
|