Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean price range values #222

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading