|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\ExpressionLanguage; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
14
|
|
|
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ExpressionDecorator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Expression; |
|
23
|
|
|
*/ |
|
24
|
|
|
private $expression; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $variableNames; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $variables = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Expression $expression |
|
38
|
|
|
* @param array $variableNames |
|
39
|
|
|
*/ |
|
40
|
23 |
|
public function __construct(Expression $expression, array $variableNames = []) |
|
41
|
|
|
{ |
|
42
|
23 |
|
$this->expression = $expression; |
|
43
|
23 |
|
$this->variableNames = $variableNames; |
|
44
|
23 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return Expression |
|
48
|
|
|
*/ |
|
49
|
8 |
|
public function getExpression() |
|
50
|
|
|
{ |
|
51
|
8 |
|
return $this->expression; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
20 |
|
public function getVariableNames() |
|
58
|
|
|
{ |
|
59
|
20 |
|
return $this->variableNames; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
7 |
|
public function getVariables() |
|
66
|
|
|
{ |
|
67
|
7 |
|
return $this->variables; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $variables |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function setVariables(array $variables) |
|
74
|
|
|
{ |
|
75
|
9 |
|
$missed = array_diff($this->variableNames, array_keys($variables)); |
|
76
|
|
|
|
|
77
|
9 |
|
if (count($missed)) { |
|
78
|
1 |
|
throw new InvalidArgumentException(sprintf('Missed variables: "%s".', implode('", "', $missed))); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
8 |
|
$unknown = array_diff(array_keys($variables), $this->variableNames); |
|
82
|
|
|
|
|
83
|
8 |
|
if (count($unknown)) { |
|
84
|
1 |
|
throw new InvalidArgumentException(sprintf('Unknown variables: "%s".', implode('", "', $unknown))); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
$this->variables = $variables; |
|
88
|
7 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets the expression. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string The expression |
|
94
|
|
|
*/ |
|
95
|
14 |
|
public function __toString() |
|
96
|
|
|
{ |
|
97
|
14 |
|
return (string) $this->expression; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|