-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
use Twig\TwigFunction; | ||
|
||
final class FilterExtension extends AbstractExtension | ||
{ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('is_filter_active', [FilterRuntime::class, 'isFilterActive']), | ||
]; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('merge_filter_value_with_current_active_filters', [FilterRuntime::class, 'mergeFilterValueWithCurrentActiveFilters']), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Twig; | ||
|
||
use Twig\Extension\RuntimeExtensionInterface; | ||
use Webgriffe\SyliusElasticsearchPlugin\Model\FilterInterface; | ||
use Webgriffe\SyliusElasticsearchPlugin\Model\FilterValueInterface; | ||
|
||
final class FilterRuntime implements RuntimeExtensionInterface | ||
{ | ||
/** | ||
* @param array<string, array<array-key, array{code: string, value: string}>> $activeFilters | ||
*/ | ||
public function isFilterActive(FilterValueInterface $filterValue, FilterInterface $filter, array $activeFilters): bool | ||
{ | ||
if (!array_key_exists($filter->getType(), $activeFilters)) { | ||
return false; | ||
} | ||
$appliedFilterValues = $activeFilters[$filter->getType()]; | ||
foreach ($appliedFilterValues as $appliedFilterValue) { | ||
if ($appliedFilterValue['code'] === $filter->getKeyCode() && | ||
$appliedFilterValue['value'] === $filterValue->getKey() | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param array<string, array<array-key, array{code: string, value: string}>> $activeFilters | ||
*/ | ||
public function mergeFilterValueWithCurrentActiveFilters( | ||
FilterValueInterface $filterValue, | ||
FilterInterface $filter, | ||
array $activeFilters, | ||
): array { | ||
$filterType = $filter->getType(); | ||
if (!array_key_exists($filterType, $activeFilters)) { | ||
$activeFilters[$filterType] = []; | ||
} | ||
foreach ($activeFilters[$filterType] as $appliedFilterValue) { | ||
if ($appliedFilterValue['code'] === $filter->getKeyCode() && | ||
$appliedFilterValue['value'] === $filterValue->getKey()) { | ||
return $activeFilters; | ||
} | ||
} | ||
$activeFilters[$filterType][] = [ | ||
'code' => $filter->getKeyCode(), | ||
'value' => $filterValue->getKey(), | ||
]; | ||
|
||
return $activeFilters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters