|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Khusseini\PimcoreRadBrickBundle\ExpressionLanguage; |
|
4
|
|
|
|
|
5
|
|
|
use Khusseini\PimcoreRadBrickBundle\Context; |
|
6
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
7
|
|
|
use Symfony\Component\PropertyAccess\Exception\AccessException; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
10
|
|
|
|
|
11
|
|
|
class ExpressionWrapper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ExpressionLanguage |
|
15
|
|
|
*/ |
|
16
|
|
|
private $expressionLanguage; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var PropertyAccessorInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $propAccess; |
|
22
|
|
|
|
|
23
|
14 |
|
private function getExpressionLanguage(): ExpressionLanguage |
|
24
|
|
|
{ |
|
25
|
14 |
|
if (null === $this->expressionLanguage) { |
|
26
|
14 |
|
$this->expressionLanguage = new ExpressionLanguage(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
14 |
|
return $this->expressionLanguage; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
8 |
|
private function getPropertyAccess(): PropertyAccessorInterface |
|
33
|
|
|
{ |
|
34
|
8 |
|
if (null === $this->propAccess) { |
|
35
|
8 |
|
$this->propAccess = PropertyAccess::createPropertyAccessorBuilder() |
|
36
|
8 |
|
->enableExceptionOnInvalidIndex() |
|
37
|
8 |
|
->getPropertyAccessor(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
8 |
|
return $this->propAccess; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array<mixed> $data |
|
45
|
|
|
* @param array<string> $attributes |
|
46
|
|
|
* |
|
47
|
|
|
* @return array<mixed> |
|
48
|
|
|
*/ |
|
49
|
8 |
|
public function evaluateExpressions( |
|
50
|
|
|
array $data, |
|
51
|
|
|
array $attributes, |
|
52
|
|
|
string $contextPath = '' |
|
53
|
|
|
): array { |
|
54
|
8 |
|
$context = $data; |
|
55
|
8 |
|
if ($contextPath) { |
|
56
|
7 |
|
$context = $this->getPropertyValue($data, $contextPath); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
foreach ($attributes as $attributePath) { |
|
60
|
|
|
try { |
|
61
|
7 |
|
$value = $this->getPropertyValue($data, $attributePath); |
|
62
|
2 |
|
} catch (AccessException $ex) { |
|
63
|
2 |
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
6 |
|
$value = $this->evaluateExpression($value, $context); |
|
67
|
6 |
|
$data = $this->setPropertyValue($data, $attributePath, $value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
return $data; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array<array>|object $objectOrArray |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
8 |
|
public function getPropertyValue($objectOrArray, string $propertyPath) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this |
|
81
|
8 |
|
->getPropertyAccess() |
|
82
|
8 |
|
->getValue($objectOrArray, $propertyPath); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array<array>|object $objectOrArray |
|
87
|
|
|
* @param mixed $value |
|
88
|
|
|
* |
|
89
|
|
|
* @return array<array>|object |
|
90
|
|
|
*/ |
|
91
|
7 |
|
public function setPropertyValue($objectOrArray, string $propertyPath, $value) |
|
92
|
|
|
{ |
|
93
|
7 |
|
$this->getPropertyAccess()->setValue($objectOrArray, $propertyPath, $value); |
|
94
|
|
|
|
|
95
|
7 |
|
return $objectOrArray; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array<array> $context |
|
100
|
|
|
* |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
14 |
|
public function evaluateExpression(string $value, array $context) |
|
104
|
|
|
{ |
|
105
|
|
|
try { |
|
106
|
14 |
|
return $this->getExpressionLanguage()->evaluate($value, $context); |
|
107
|
7 |
|
} catch (\Exception $ex) { |
|
108
|
7 |
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|