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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

75.0
/src/Doctrine/Orm/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\Orm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\RangeFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\RangeFilterTrait;
18
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
19
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\Parameter;
22
use ApiPlatform\Metadata\QueryParameter;
23
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
24
use Doctrine\ORM\Query\Expr\Join;
25
use Doctrine\ORM\QueryBuilder;
26

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

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

UNCOV
129
        $values = $this->normalizeValues($values, $property);
18✔
UNCOV
130
        if (null === $values) {
18✔
131
            return;
×
132
        }
133

UNCOV
134
        $alias = $queryBuilder->getRootAliases()[0];
18✔
UNCOV
135
        $field = $property;
18✔
136

UNCOV
137
        if ($this->isPropertyNested($property, $resourceClass)) {
18✔
138
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
×
139
        }
140

UNCOV
141
        foreach ($values as $operator => $value) {
18✔
UNCOV
142
            $this->addWhere(
18✔
UNCOV
143
                $queryBuilder,
18✔
UNCOV
144
                $queryNameGenerator,
18✔
UNCOV
145
                $alias,
18✔
UNCOV
146
                $field,
18✔
UNCOV
147
                $operator,
18✔
UNCOV
148
                $value
18✔
UNCOV
149
            );
18✔
150
        }
151
    }
152

153
    /**
154
     * Adds the where clause according to the operator.
155
     */
156
    protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value): void
157
    {
UNCOV
158
        $valueParameter = $queryNameGenerator->generateParameterName($field);
18✔
159

160
        switch ($operator) {
UNCOV
161
            case self::PARAMETER_BETWEEN:
18✔
UNCOV
162
                $rangeValue = explode('..', $value, 2);
5✔
163

UNCOV
164
                $rangeValue = $this->normalizeBetweenValues($rangeValue);
5✔
UNCOV
165
                if (null === $rangeValue) {
5✔
166
                    return;
×
167
                }
168

UNCOV
169
                if ($rangeValue[0] === $rangeValue[1]) {
5✔
170
                    $queryBuilder
×
171
                        ->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
×
172
                        ->setParameter($valueParameter, $rangeValue[0]);
×
173

174
                    return;
×
175
                }
176

UNCOV
177
                $queryBuilder
5✔
UNCOV
178
                    ->andWhere(\sprintf('%1$s.%2$s BETWEEN :%3$s_1 AND :%3$s_2', $alias, $field, $valueParameter))
5✔
UNCOV
179
                    ->setParameter(\sprintf('%s_1', $valueParameter), $rangeValue[0])
5✔
UNCOV
180
                    ->setParameter(\sprintf('%s_2', $valueParameter), $rangeValue[1]);
5✔
181

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

UNCOV
189
                $queryBuilder
4✔
UNCOV
190
                    ->andWhere(\sprintf('%s.%s > :%s', $alias, $field, $valueParameter))
4✔
UNCOV
191
                    ->setParameter($valueParameter, $value);
4✔
192

UNCOV
193
                break;
4✔
UNCOV
194
            case self::PARAMETER_GREATER_THAN_OR_EQUAL:
14✔
UNCOV
195
                $value = $this->normalizeValue($value, $operator);
6✔
UNCOV
196
                if (null === $value) {
6✔
197
                    return;
×
198
                }
199

UNCOV
200
                $queryBuilder
6✔
UNCOV
201
                    ->andWhere(\sprintf('%s.%s >= :%s', $alias, $field, $valueParameter))
6✔
UNCOV
202
                    ->setParameter($valueParameter, $value);
6✔
203

UNCOV
204
                break;
6✔
UNCOV
205
            case self::PARAMETER_LESS_THAN:
11✔
UNCOV
206
                $value = $this->normalizeValue($value, $operator);
5✔
UNCOV
207
                if (null === $value) {
5✔
208
                    return;
×
209
                }
210

UNCOV
211
                $queryBuilder
5✔
UNCOV
212
                    ->andWhere(\sprintf('%s.%s < :%s', $alias, $field, $valueParameter))
5✔
UNCOV
213
                    ->setParameter($valueParameter, $value);
5✔
214

UNCOV
215
                break;
5✔
UNCOV
216
            case self::PARAMETER_LESS_THAN_OR_EQUAL:
6✔
UNCOV
217
                $value = $this->normalizeValue($value, $operator);
6✔
UNCOV
218
                if (null === $value) {
6✔
219
                    return;
×
220
                }
221

UNCOV
222
                $queryBuilder
6✔
UNCOV
223
                    ->andWhere(\sprintf('%s.%s <= :%s', $alias, $field, $valueParameter))
6✔
UNCOV
224
                    ->setParameter($valueParameter, $value);
6✔
225

UNCOV
226
                break;
6✔
227
        }
228
    }
229

230
    public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
231
    {
232
        $in = $parameter instanceof QueryParameter ? 'query' : 'header';
×
233
        $key = $parameter->getKey();
×
234

235
        return [
×
236
            new OpenApiParameter(name: $key.'[gt]', in: $in),
×
237
            new OpenApiParameter(name: $key.'[lt]', in: $in),
×
238
            new OpenApiParameter(name: $key.'[gte]', in: $in),
×
239
            new OpenApiParameter(name: $key.'[lte]', in: $in),
×
240
        ];
×
241
    }
242
}
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