ExpressionDecorator::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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