Skip to content

Commit

Permalink
Clean range field values for price if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Sep 17, 2024
1 parent 49e532e commit 04a3018
Showing 1 changed file with 37 additions and 0 deletions.
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
{
$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 */
Expand Down

0 comments on commit 04a3018

Please sign in to comment.