• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.33
/src/Doctrine/Odm/Filter/RangeFilter.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\RangeFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\RangeFilterTrait;
18
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Parameter;
21
use ApiPlatform\Metadata\QueryParameter;
22
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
23
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24

25
/**
26
 * The range filter allows you to filter by a value lower than, greater than, lower than or equal, greater than or equal and between two values.
27
 *
28
 * Syntax: `?property[<lt|gt|lte|gte|between>]=value`.
29
 *
30
 * <div data-code-selector>
31
 *
32
 * ```php
33
 * <?php
34
 * // api/src/Entity/Book.php
35
 * use ApiPlatform\Metadata\ApiFilter;
36
 * use ApiPlatform\Metadata\ApiResource;
37
 * use ApiPlatform\Doctrine\Odm\Filter\RangeFilter;
38
 *
39
 * #[ApiResource]
40
 * #[ApiFilter(RangeFilter::class, properties: ['price'])]
41
 * class Book
42
 * {
43
 *     // ...
44
 * }
45
 * ```
46
 *
47
 * ```yaml
48
 * # config/services.yaml
49
 * services:
50
 *     book.range_filter:
51
 *         parent: 'api_platform.doctrine.odm.range_filter'
52
 *         arguments: [ { price: ~ } ]
53
 *         tags:  [ 'api_platform.filter' ]
54
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
55
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
56
 *         autowire: false
57
 *         autoconfigure: false
58
 *         public: false
59
 *
60
 * # api/config/api_platform/resources.yaml
61
 * resources:
62
 *     App\Entity\Book:
63
 *         - operations:
64
 *               ApiPlatform\Metadata\GetCollection:
65
 *                   filters: ['book.range_filter']
66
 * ```
67
 *
68
 * ```xml
69
 * <?xml version="1.0" encoding="UTF-8" ?>
70
 * <!-- api/config/services.xml -->
71
 * <?xml version="1.0" encoding="UTF-8" ?>
72
 * <container
73
 *         xmlns="http://symfony.com/schema/dic/services"
74
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
76
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
77
 *     <services>
78
 *         <service id="book.range_filter" parent="api_platform.doctrine.odm.range_filter">
79
 *             <argument type="collection">
80
 *                 <argument key="price"/>
81
 *             </argument>
82
 *             <tag name="api_platform.filter"/>
83
 *         </service>
84
 *     </services>
85
 * </container>
86
 * <!-- api/config/api_platform/resources.xml -->
87
 * <resources
88
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
89
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
91
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
92
 *     <resource class="App\Entity\Book">
93
 *         <operations>
94
 *             <operation class="ApiPlatform\Metadata\GetCollection">
95
 *                 <filters>
96
 *                     <filter>book.range_filter</filter>
97
 *                 </filters>
98
 *             </operation>
99
 *         </operations>
100
 *     </resource>
101
 * </resources>
102
 * ```
103
 *
104
 * </div>
105
 *
106
 * Given that the collection endpoint is `/books`, you can filter books with the following query: `/books?price[between]=12.99..15.99`.
107
 *
108
 * @author Lee Siong Chan <ahlee2326@me.com>
109
 * @author Alan Poulain <contact@alanpoulain.eu>
110
 */
111
final class RangeFilter extends AbstractFilter implements RangeFilterInterface, OpenApiParameterFilterInterface
112
{
113
    use RangeFilterTrait;
114

115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function filterProperty(string $property, $values, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
119
    {
120
        if (
UNCOV
121
            !\is_array($values)
105✔
UNCOV
122
            || !$this->isPropertyEnabled($property, $resourceClass)
105✔
UNCOV
123
            || !$this->isPropertyMapped($property, $resourceClass)
105✔
124
        ) {
UNCOV
125
            return;
93✔
126
        }
127

UNCOV
128
        $values = $this->normalizeValues($values, $property);
13✔
UNCOV
129
        if (null === $values) {
13✔
UNCOV
130
            return;
2✔
131
        }
132

UNCOV
133
        $matchField = $field = $property;
11✔
134

UNCOV
135
        if ($this->isPropertyNested($property, $resourceClass)) {
11✔
136
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
×
137
        }
138

UNCOV
139
        foreach ($values as $operator => $value) {
11✔
UNCOV
140
            $this->addMatch(
11✔
UNCOV
141
                $aggregationBuilder,
11✔
UNCOV
142
                $field,
11✔
UNCOV
143
                $matchField,
11✔
UNCOV
144
                $operator,
11✔
UNCOV
145
                $value
11✔
UNCOV
146
            );
11✔
147
        }
148
    }
149

150
    /**
151
     * Adds the match stage according to the operator.
152
     */
153
    protected function addMatch(Builder $aggregationBuilder, string $field, string $matchField, string $operator, string $value): void
154
    {
155
        switch ($operator) {
UNCOV
156
            case self::PARAMETER_BETWEEN:
11✔
UNCOV
157
                $rangeValue = explode('..', $value, 2);
3✔
158

UNCOV
159
                $rangeValue = $this->normalizeBetweenValues($rangeValue);
3✔
UNCOV
160
                if (null === $rangeValue) {
3✔
UNCOV
161
                    return;
1✔
162
                }
163

UNCOV
164
                if ($rangeValue[0] === $rangeValue[1]) {
2✔
UNCOV
165
                    $aggregationBuilder->match()->field($matchField)->equals($rangeValue[0]);
1✔
166

UNCOV
167
                    return;
1✔
168
                }
169

UNCOV
170
                $aggregationBuilder->match()->field($matchField)->gte($rangeValue[0])->lte($rangeValue[1]);
1✔
171

UNCOV
172
                break;
1✔
UNCOV
173
            case self::PARAMETER_GREATER_THAN:
8✔
UNCOV
174
                $value = $this->normalizeValue($value, $operator);
4✔
UNCOV
175
                if (null === $value) {
4✔
176
                    return;
×
177
                }
178

UNCOV
179
                $aggregationBuilder->match()->field($matchField)->gt($value);
4✔
180

UNCOV
181
                break;
4✔
UNCOV
182
            case self::PARAMETER_GREATER_THAN_OR_EQUAL:
5✔
UNCOV
183
                $value = $this->normalizeValue($value, $operator);
1✔
UNCOV
184
                if (null === $value) {
1✔
185
                    return;
×
186
                }
187

UNCOV
188
                $aggregationBuilder->match()->field($matchField)->gte($value);
1✔
189

UNCOV
190
                break;
1✔
UNCOV
191
            case self::PARAMETER_LESS_THAN:
4✔
UNCOV
192
                $value = $this->normalizeValue($value, $operator);
2✔
UNCOV
193
                if (null === $value) {
2✔
194
                    return;
×
195
                }
196

UNCOV
197
                $aggregationBuilder->match()->field($matchField)->lt($value);
2✔
198

UNCOV
199
                break;
2✔
UNCOV
200
            case self::PARAMETER_LESS_THAN_OR_EQUAL:
2✔
UNCOV
201
                $value = $this->normalizeValue($value, $operator);
2✔
UNCOV
202
                if (null === $value) {
2✔
203
                    return;
×
204
                }
205

UNCOV
206
                $aggregationBuilder->match()->field($matchField)->lte($value);
2✔
207

UNCOV
208
                break;
2✔
209
        }
210
    }
211

212
    public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
213
    {
214
        $in = $parameter instanceof QueryParameter ? 'query' : 'header';
×
215
        $key = $parameter->getKey();
×
216

217
        return [
×
218
            new OpenApiParameter(name: $key.'[gt]', in: $in),
×
219
            new OpenApiParameter(name: $key.'[lt]', in: $in),
×
220
            new OpenApiParameter(name: $key.'[gte]', in: $in),
×
221
            new OpenApiParameter(name: $key.'[lte]', in: $in),
×
222
        ];
×
223
    }
224
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc