diff --git a/dist/src/Resources/config/search/taxons.yaml b/dist/src/Resources/config/search/taxons.yaml index 39a93c9..5602a4e 100644 --- a/dist/src/Resources/config/search/taxons.yaml +++ b/dist/src/Resources/config/search/taxons.yaml @@ -16,7 +16,7 @@ monsieurbiz_sylius_search: instant: '@MonsieurBizSyliusSearchPlugin/Instant/Taxon/_box.html.twig' #mapping_provider: '...' # by default MonsieurBiz\SyliusSearchPlugin\Mapping\YamlWithLocaleProvider datasource: 'App\Search\Model\Datasource\TaxonDatasource' # by default MonsieurBiz\SyliusSearchPlugin\Model\Datasource\RepositoryDatasource - position: -1 + position: 2 automapper_classes: sources: taxon: '%sylius.model.taxon.class%' diff --git a/src/Search/Request/RequestConfiguration.php b/src/Search/Request/RequestConfiguration.php index 5622cf5..526c9a0 100644 --- a/src/Search/Request/RequestConfiguration.php +++ b/src/Search/Request/RequestConfiguration.php @@ -67,6 +67,8 @@ public function getAppliedFilters(string $type = null): array return \is_array($query) ? array_filter($query) : $query; }, $requestQuery); + $this->manageRangeField('price'); + return null !== $type ? ($requestQuery[$type] ?? []) : $requestQuery; } @@ -82,6 +84,41 @@ public function getPage(): int return (int) $this->request->get('page', 1); } + /** + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + public function manageRangeField(string $field): void + { + $range = $this->request->get($field, []); + if (!\is_array($range) || empty($range)) { + return; + } + + /** @var array $range */ + + // Reverse min and max if min is greater than max + if (isset($range['min'], $range['max'])) { + $min = (float) $range['min']; + $max = (float) $range['max']; + if ($min > $max) { + $range['min'] = $range['max']; + $range['max'] = $range['min']; + } + } + + // Remove min value is 0 or less + if (isset($range['min']) && 0 >= (float) $range['min']) { + unset($range['min']); + } + + // Remove max value if it is 0 or less + if (isset($range['max']) && 0 >= (float) $range['max']) { + unset($range['max']); + } + + $this->request->query->set($field, $range); + } + public function getLimit(): int { /** @phpstan-ignore-next-line */