vendor/sylius/grid-bundle/src/Component/Filter/MoneyFilter.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Sylius Sp. z o.o.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\Grid\Filter;
  12. use Sylius\Component\Grid\Data\DataSourceInterface;
  13. use Sylius\Component\Grid\Filtering\FilterInterface;
  14. \trigger_deprecation('sylius/grid''1.8''%s is deprecated, replace it with your own implementation.'MoneyFilter::class);
  15. final class MoneyFilter implements FilterInterface
  16. {
  17.     public const DEFAULT_SCALE 2;
  18.     public function apply(DataSourceInterface $dataSourcestring $name$data, array $options): void
  19.     {
  20.         if (empty($data)) {
  21.             return;
  22.         }
  23.         $field $options['field'] ?? $name;
  24.         $scale = (int) ($options['scale'] ?? self::DEFAULT_SCALE);
  25.         $greaterThan $this->getDataValue($data'greaterThan');
  26.         $lessThan $this->getDataValue($data'lessThan');
  27.         $expressionBuilder $dataSource->getExpressionBuilder();
  28.         if (!empty($data['currency'])) {
  29.             $dataSource->restrict($expressionBuilder->equals($options['currency_field'], $data['currency']));
  30.         }
  31.         if ('' !== $greaterThan) {
  32.             $dataSource->restrict($expressionBuilder->greaterThan($field$this->normalizeAmount((float) $greaterThan$scale)));
  33.         }
  34.         if ('' !== $lessThan) {
  35.             $dataSource->restrict($expressionBuilder->lessThan($field$this->normalizeAmount((float) $lessThan$scale)));
  36.         }
  37.     }
  38.     private function normalizeAmount(float $amountint $scale): int
  39.     {
  40.         return (int) round($amount * (10 ** $scale));
  41.     }
  42.     /**
  43.      * @param string[] $data
  44.      */
  45.     private function getDataValue(array $datastring $key): string
  46.     {
  47.         return $data[$key] ?? '';
  48.     }
  49. }