Skip to content

Commit cd43289

Browse files
authored
Merge pull request #222 from maximehuran/feature/clean-price-range
Clean price range values
2 parents 76bf3aa + cf41679 commit cd43289

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

dist/src/Resources/config/search/taxons.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ monsieurbiz_sylius_search:
1616
instant: '@MonsieurBizSyliusSearchPlugin/Instant/Taxon/_box.html.twig'
1717
#mapping_provider: '...' # by default MonsieurBiz\SyliusSearchPlugin\Mapping\YamlWithLocaleProvider
1818
datasource: 'App\Search\Model\Datasource\TaxonDatasource' # by default MonsieurBiz\SyliusSearchPlugin\Model\Datasource\RepositoryDatasource
19-
position: -1
19+
position: 2
2020
automapper_classes:
2121
sources:
2222
taxon: '%sylius.model.taxon.class%'

src/Search/Request/RequestConfiguration.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function getAppliedFilters(string $type = null): array
6767
return \is_array($query) ? array_filter($query) : $query;
6868
}, $requestQuery);
6969

70+
$this->manageRangeField('price');
71+
7072
return null !== $type ? ($requestQuery[$type] ?? []) : $requestQuery;
7173
}
7274

@@ -82,6 +84,41 @@ public function getPage(): int
8284
return (int) $this->request->get('page', 1);
8385
}
8486

87+
/**
88+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
89+
*/
90+
public function manageRangeField(string $field): void
91+
{
92+
$range = $this->request->get($field, []);
93+
if (!\is_array($range) || empty($range)) {
94+
return;
95+
}
96+
97+
/** @var array $range */
98+
99+
// Reverse min and max if min is greater than max
100+
if (isset($range['min'], $range['max'])) {
101+
$min = (float) $range['min'];
102+
$max = (float) $range['max'];
103+
if ($min > $max) {
104+
$range['min'] = $range['max'];
105+
$range['max'] = $range['min'];
106+
}
107+
}
108+
109+
// Remove min value is 0 or less
110+
if (isset($range['min']) && 0 >= (float) $range['min']) {
111+
unset($range['min']);
112+
}
113+
114+
// Remove max value if it is 0 or less
115+
if (isset($range['max']) && 0 >= (float) $range['max']) {
116+
unset($range['max']);
117+
}
118+
119+
$this->request->query->set($field, $range);
120+
}
121+
85122
public function getLimit(): int
86123
{
87124
/** @phpstan-ignore-next-line */

0 commit comments

Comments
 (0)