From 04a3018e228eaeca64dc8926d4c333065b709b7c Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 17 Sep 2024 12:22:33 +0200 Subject: [PATCH] Clean range field values for price if needed --- src/Search/Request/RequestConfiguration.php | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Search/Request/RequestConfiguration.php b/src/Search/Request/RequestConfiguration.php index 5622cf5..b056a0c 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 + { + $price = $this->request->get($field, []); + if (!\is_array($price) || empty($price)) { + return; + } + + /** @var array $price */ + + // Reverse min and max if min is greater than max + if (isset($price['min'], $price['max'])) { + $min = (int) $price['min']; + $max = (int) $price['max']; + if ($min > $max) { + $price['min'] = (string) $max; + $price['max'] = (string) $min; + } + } + + // Remove min value is 0 or less + if (isset($price['min']) && 0 >= (int) $price['min']) { + unset($price['min']); + } + + // Remove max value if it is 0 or less + if (isset($price['max']) && 0 >= (int) $price['max']) { + unset($price['max']); + } + + $this->request->query->set($field, $price); + } + public function getLimit(): int { /** @phpstan-ignore-next-line */