Skip to content

Commit 2c74782

Browse files
Fix value function (#174)
1 parent c68bb8e commit 2c74782

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Search/ExpressionLanguage/MassiveSearchExpressionLanguage.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
1515
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16+
use Symfony\Component\PropertyAccess\PropertyAccessor;
1617

1718
/**
1819
* Expression language for massive search bundle.
@@ -95,14 +96,17 @@ function(array $values, $elements, $expression) {
9596
*/
9697
private function createValueFunction()
9798
{
99+
$accessor = new PropertyAccessor();
100+
98101
return new ExpressionFunction(
99102
'massive_search_value',
100103
function($elements, $expression) {
101104
throw new \Exception('Value function does not support compilation');
102105
},
103-
function(array $values, $variable, $default) {
104-
if (isset($values[$variable])) {
105-
return $values[$variable];
106+
function(array $values, $propertyPath, $default = null) use ($accessor) {
107+
$value = $accessor->getValue($values, $propertyPath);
108+
if (null !== $value) {
109+
return $value;
106110
}
107111

108112
return $default;

Tests/Unit/Search/ExpressionLanguage/MassiveSearchExpressionLanguageTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,45 @@ public function provideExpression()
3737
['one', 'two', 'three'],
3838
],
3939
[
40-
'massive_search_value("three", null)',
40+
'massive_search_value("[three]")',
4141
null,
4242
['one' => 'X', 'two' => 'Y'],
4343
],
4444
[
45-
'massive_search_value("three", "default")',
45+
'massive_search_value("[three]", null)',
46+
null,
47+
['one' => 'X', 'two' => 'Y'],
48+
],
49+
[
50+
'massive_search_value("[three]", "default")',
4651
'default',
4752
['one' => 'X', 'two' => 'Y'],
4853
],
4954
[
50-
'massive_search_value("three", null)',
55+
'massive_search_value("[three]", null)',
5156
'Z',
5257
['one' => 'X', 'two' => 'Y', 'three' => 'Z'],
5358
],
5459
[
55-
'massive_search_value("three", {"test": true})["test"]',
60+
'massive_search_value("[three]", {"test": true})["test"]',
5661
true,
5762
['one' => 'X', 'two' => 'Y'],
5863
],
5964
[
60-
'massive_search_value("three", {"test": true})["test"]',
65+
'massive_search_value("[three]", {"test": true})["test"]',
6166
false,
6267
['one' => 'X', 'two' => 'Y', 'three' => ['test' => false]],
6368
],
69+
[
70+
'massive_search_value("[three][test]", true)',
71+
true,
72+
['one' => 'X', 'two' => 'Y'],
73+
],
74+
[
75+
'massive_search_value("[XX][test]", true)',
76+
false,
77+
['one' => 'X', 'two' => 'Y', 'XX' => ['test' => false]],
78+
],
6479
];
6580
}
6681

0 commit comments

Comments
 (0)