Skip to content

Commit

Permalink
Fix value function (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Jan 16, 2024
1 parent c68bb8e commit 2c74782
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 7 additions & 3 deletions Search/ExpressionLanguage/MassiveSearchExpressionLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccessor;

/**
* Expression language for massive search bundle.
Expand Down Expand Up @@ -95,14 +96,17 @@ function(array $values, $elements, $expression) {
*/
private function createValueFunction()
{
$accessor = new PropertyAccessor();

return new ExpressionFunction(
'massive_search_value',
function($elements, $expression) {
throw new \Exception('Value function does not support compilation');
},
function(array $values, $variable, $default) {
if (isset($values[$variable])) {
return $values[$variable];
function(array $values, $propertyPath, $default = null) use ($accessor) {
$value = $accessor->getValue($values, $propertyPath);
if (null !== $value) {
return $value;
}

return $default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,45 @@ public function provideExpression()
['one', 'two', 'three'],
],
[
'massive_search_value("three", null)',
'massive_search_value("[three]")',
null,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", "default")',
'massive_search_value("[three]", null)',
null,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("[three]", "default")',
'default',
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", null)',
'massive_search_value("[three]", null)',
'Z',
['one' => 'X', 'two' => 'Y', 'three' => 'Z'],
],
[
'massive_search_value("three", {"test": true})["test"]',
'massive_search_value("[three]", {"test": true})["test"]',
true,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", {"test": true})["test"]',
'massive_search_value("[three]", {"test": true})["test"]',
false,
['one' => 'X', 'two' => 'Y', 'three' => ['test' => false]],
],
[
'massive_search_value("[three][test]", true)',
true,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("[XX][test]", true)',
false,
['one' => 'X', 'two' => 'Y', 'XX' => ['test' => false]],
],
];
}

Expand Down

0 comments on commit 2c74782

Please sign in to comment.