1 | <?php |
||
22 | final class MoneyFilter implements FilterInterface |
||
23 | { |
||
24 | public const DEFAULT_SCALE = 2; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void |
||
30 | { |
||
31 | if (empty($data)) { |
||
32 | return; |
||
33 | } |
||
34 | |||
35 | $field = $options['field'] ?? $name; |
||
36 | $scale = isset($options['scale']) ? (int) $options['scale'] : self::DEFAULT_SCALE; |
||
37 | |||
38 | $greaterThan = $this->getDataValue($data, 'greaterThan'); |
||
39 | $lessThan = $this->getDataValue($data, 'lessThan'); |
||
40 | |||
41 | $expressionBuilder = $dataSource->getExpressionBuilder(); |
||
42 | |||
43 | if (!empty($data['currency'])) { |
||
44 | $dataSource->restrict($expressionBuilder->equals($options['currency_field'], $data['currency'])); |
||
45 | } |
||
46 | if ('' !== $greaterThan) { |
||
47 | $expressionBuilder->greaterThan($field, $this->normalizeAmount((float) $greaterThan, $scale)); |
||
48 | } |
||
49 | if ('' !== $lessThan) { |
||
50 | $expressionBuilder->lessThan($field, $this->normalizeAmount((float) $lessThan, $scale)); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param float $amount |
||
56 | * @param int $scale |
||
57 | * |
||
58 | * @return int |
||
59 | */ |
||
60 | private function normalizeAmount(float $amount, int $scale): int |
||
64 | |||
65 | /** |
||
66 | * @param string[] $data |
||
67 | * @param string $key |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | private function getDataValue(array $data, string $key): string |
||
75 | } |
||
76 |