Skip to content

Commit

Permalink
Merge pull request #222 from maximehuran/feature/clean-price-range
Browse files Browse the repository at this point in the history
Clean price range values
  • Loading branch information
maximehuran authored Sep 18, 2024
2 parents 76bf3aa + cf41679 commit cd43289
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/src/Resources/config/search/taxons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand Down
37 changes: 37 additions & 0 deletions src/Search/Request/RequestConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 */
Expand Down

0 comments on commit cd43289

Please sign in to comment.