|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter; |
| 15 | + |
| 16 | +use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface; |
| 17 | +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException; |
| 18 | +use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType; |
| 19 | +use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter; |
| 20 | +use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +final class NumberFilter implements FilterInterface |
| 26 | +{ |
| 27 | + public function apply(mixed $parameters, QueryInterface $query): QueryInterface |
| 28 | + { |
| 29 | + if (!$parameters instanceof ColumnFiltersParameterInterface) { |
| 30 | + return $query; |
| 31 | + } |
| 32 | + |
| 33 | + foreach ($parameters->getColumnFilterByType(ColumnType::SYSTEM_NUMBER->value) as $column) { |
| 34 | + $query = $this->applyNumberFilter($column, $query); |
| 35 | + } |
| 36 | + |
| 37 | + return $query; |
| 38 | + } |
| 39 | + |
| 40 | + private function applyNumberFilter(ColumnFilter $column, QueryInterface $query): QueryInterface |
| 41 | + { |
| 42 | + $fiterValue = $column->getFilterValue(); |
| 43 | + |
| 44 | + if (!isset($fiterValue['setting'])) { |
| 45 | + throw new InvalidArgumentException('This filter requires a setting value'); |
| 46 | + } |
| 47 | + $setting = $fiterValue['setting']; |
| 48 | + |
| 49 | + if (isset($fiterValue['is']) && $setting == 'is') { |
| 50 | + return $query->filterNumber($column->getKey(), $fiterValue['is']); |
| 51 | + } |
| 52 | + |
| 53 | + if (isset($fiterValue['to']) && $setting == 'less') { |
| 54 | + return $query->filterNumberRange($column->getKey(), null, $fiterValue['to']); |
| 55 | + } |
| 56 | + |
| 57 | + if (isset($fiterValue['from']) && $setting == 'more') { |
| 58 | + return $query->filterNumberRange($column->getKey(), $fiterValue['from'], null); |
| 59 | + } |
| 60 | + |
| 61 | + if ($setting == 'between') { |
| 62 | + return $query->filterNumberRange($column->getKey(), $fiterValue['from'], $fiterValue['to']); |
| 63 | + } |
| 64 | + |
| 65 | + throw new InvalidArgumentException('Unable to apply number filter no correct setting given'); |
| 66 | + } |
| 67 | +} |
0 commit comments