1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the API Platform project. |
||
5 | * |
||
6 | * (c) Kévin Dunglas <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter; |
||
15 | |||
16 | use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\RangeFilterInterface; |
||
17 | use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\RangeFilterTrait; |
||
18 | use Doctrine\ODM\MongoDB\Aggregation\Builder; |
||
19 | |||
20 | /** |
||
21 | * Filters the collection by range. |
||
22 | * |
||
23 | * @experimental |
||
24 | * |
||
25 | * @author Lee Siong Chan <[email protected]> |
||
26 | * @author Alan Poulain <[email protected]> |
||
27 | */ |
||
28 | final class RangeFilter extends AbstractFilter implements RangeFilterInterface |
||
29 | { |
||
30 | use RangeFilterTrait; |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | protected function filterProperty(string $property, $values, Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = []) |
||
36 | { |
||
37 | if ( |
||
38 | !\is_array($values) || |
||
39 | !$this->isPropertyEnabled($property, $resourceClass) || |
||
40 | !$this->isPropertyMapped($property, $resourceClass) |
||
41 | ) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | $values = $this->normalizeValues($values, $property); |
||
46 | if (null === $values) { |
||
47 | return; |
||
48 | } |
||
49 | |||
50 | $matchField = $field = $property; |
||
51 | |||
52 | if ($this->isPropertyNested($property, $resourceClass)) { |
||
53 | [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass); |
||
54 | } |
||
55 | |||
56 | foreach ($values as $operator => $value) { |
||
57 | $this->addMatch( |
||
58 | $aggregationBuilder, |
||
59 | $field, |
||
60 | $matchField, |
||
61 | $operator, |
||
62 | $value |
||
63 | ); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Adds the match stage according to the operator. |
||
69 | */ |
||
70 | protected function addMatch(Builder $aggregationBuilder, string $field, string $matchField, string $operator, string $value) |
||
0 ignored issues
–
show
|
|||
71 | { |
||
72 | switch ($operator) { |
||
73 | case self::PARAMETER_BETWEEN: |
||
74 | $rangeValue = explode('..', $value); |
||
75 | |||
76 | $rangeValue = $this->normalizeBetweenValues($rangeValue); |
||
77 | if (null === $rangeValue) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | $aggregationBuilder->match()->field($matchField)->gte($rangeValue[0])->lte($rangeValue[1]); |
||
82 | |||
83 | break; |
||
84 | case self::PARAMETER_GREATER_THAN: |
||
85 | $value = $this->normalizeValue($value, $operator); |
||
86 | if (null === $value) { |
||
87 | return; |
||
88 | } |
||
89 | |||
90 | $aggregationBuilder->match()->field($matchField)->gt($value); |
||
91 | |||
92 | break; |
||
93 | case self::PARAMETER_GREATER_THAN_OR_EQUAL: |
||
94 | $value = $this->normalizeValue($value, $operator); |
||
95 | if (null === $value) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | $aggregationBuilder->match()->field($matchField)->gte($value); |
||
100 | |||
101 | break; |
||
102 | case self::PARAMETER_LESS_THAN: |
||
103 | $value = $this->normalizeValue($value, $operator); |
||
104 | if (null === $value) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | $aggregationBuilder->match()->field($matchField)->lt($value); |
||
109 | |||
110 | break; |
||
111 | case self::PARAMETER_LESS_THAN_OR_EQUAL: |
||
112 | $value = $this->normalizeValue($value, $operator); |
||
113 | if (null === $value) { |
||
114 | return; |
||
115 | } |
||
116 | |||
117 | $aggregationBuilder->match()->field($matchField)->lte($value); |
||
118 | |||
119 | break; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.