Skip to content
Open
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
31 changes: 29 additions & 2 deletions src/Product/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,14 @@ private function labelRangeFilters(array $facets)

foreach ($facet->getFilters() as $filter) {
$filterValue = $filter->getValue();
$min = empty($filterValue[0]) ? $facet->getProperty('min') : $filterValue[0];
$max = empty($filterValue[1]) ? $facet->getProperty('max') : $filterValue[1];
$min = $this->getNumericRangeBoundary(
isset($filterValue[0]) ? $filterValue[0] : null,
$facet->getProperty('min')
);
$max = $this->getNumericRangeBoundary(
isset($filterValue[1]) ? $filterValue[1] : null,
$facet->getProperty('max')
);
if ($facet->getType() === 'weight') {
$unit = Configuration::get('PS_WEIGHT_UNIT');
$filter->setLabel(
Expand All @@ -468,6 +474,27 @@ private function labelRangeFilters(array $facets)
}
}

/**
* Keep range labels resilient to malformed facet values coming from the URL.
*
* @param mixed $value
* @param mixed $fallback
*
* @return int|float|string
*/
private function getNumericRangeBoundary($value, $fallback)
{
if (is_scalar($value) && is_numeric($value)) {
return $value;
}

if (is_scalar($fallback) && is_numeric($fallback)) {
return $fallback;
}

return 0;
}

/**
* This method generates a URL stub for each filter inside the given facets
* and assigns this stub to the filters.
Expand Down
10 changes: 10 additions & 0 deletions tests/php/FacetedSearch/Product/SearchProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,14 @@ public function testRenderFacetsWithFacetsCollectionAndFilters()
)
);
}

public function testNumericRangeBoundaryFallsBackWhenValueIsMalformed()
{
$method = new \ReflectionMethod(SearchProvider::class, 'getNumericRangeBoundary');
$method->setAccessible(true);

$this->assertSame('12.50', $method->invoke($this->provider, '12.50', '1'));
$this->assertSame('1', $method->invoke($this->provider, '<a><a><a><a><a>', '1'));
$this->assertSame(0, $method->invoke($this->provider, '<a><a><a><a><a>', '<b>'));
}
}
Loading