• 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

80.56
/src/Doctrine/Orm/Filter/OrderFilter.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\OrderFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\OrderFilterTrait;
18
use ApiPlatform\Doctrine\Common\Filter\PropertyPlaceholderOpenApiParameterTrait;
19
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
20
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
21
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Parameter;
24
use Doctrine\ORM\Query\Expr\Join;
25
use Doctrine\ORM\QueryBuilder;
26
use Doctrine\Persistence\ManagerRegistry;
27
use Psr\Log\LoggerInterface;
28
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
29

30
/**
31
 * The order filter allows to sort a collection against the given properties.
32
 *
33
 * Syntax: `?order[property]=<asc|desc>`.
34
 *
35
 * <div data-code-selector>
36
 *
37
 * ```php
38
 * <?php
39
 * // api/src/Entity/Book.php
40
 * use ApiPlatform\Metadata\ApiFilter;
41
 * use ApiPlatform\Metadata\ApiResource;
42
 * use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
43
 *
44
 * #[ApiResource]
45
 * #[ApiFilter(OrderFilter::class, properties: ['id', 'title'], arguments: ['orderParameterName' => 'order'])]
46
 * class Book
47
 * {
48
 *     // ...
49
 * }
50
 * ```
51
 *
52
 * ```yaml
53
 * # config/services.yaml
54
 * services:
55
 *     book.order_filter:
56
 *         parent: 'api_platform.doctrine.orm.order_filter'
57
 *         arguments: [ $properties: { id: ~, title: ~ }, $orderParameterName: order ]
58
 *         tags:  [ 'api_platform.filter' ]
59
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
60
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
61
 *         autowire: false
62
 *         autoconfigure: false
63
 *         public: false
64
 *
65
 * # api/config/api_platform/resources.yaml
66
 * resources:
67
 *     App\Entity\Book:
68
 *         - operations:
69
 *               ApiPlatform\Metadata\GetCollection:
70
 *                   filters: ['book.order_filter']
71
 * ```
72
 *
73
 * ```xml
74
 * <?xml version="1.0" encoding="UTF-8" ?>
75
 * <!-- api/config/services.xml -->
76
 * <?xml version="1.0" encoding="UTF-8" ?>
77
 * <container
78
 *         xmlns="http://symfony.com/schema/dic/services"
79
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
80
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
81
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
82
 *     <services>
83
 *         <service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
84
 *             <argument type="collection" key="properties">
85
 *                 <argument key="id"/>
86
 *                 <argument key="title"/>
87
 *             </argument>
88
 *             <argument key="orderParameterName">order</argument>
89
 *             <tag name="api_platform.filter"/>
90
 *         </service>
91
 *     </services>
92
 * </container>
93
 * <!-- api/config/api_platform/resources.xml -->
94
 * <resources
95
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
96
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
97
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
98
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
99
 *     <resource class="App\Entity\Book">
100
 *         <operations>
101
 *             <operation class="ApiPlatform\Metadata\GetCollection">
102
 *                 <filters>
103
 *                     <filter>book.order_filter</filter>
104
 *                 </filters>
105
 *             </operation>
106
 *         </operations>
107
 *     </resource>
108
 * </resources>
109
 * ```
110
 *
111
 * </div>
112
 *
113
 * Given that the collection endpoint is `/books`, you can filter books by title in ascending order and then by ID in descending order with the following query: `/books?order[title]=desc&order[id]=asc`.
114
 *
115
 * By default, whenever the query does not specify the direction explicitly (e.g.: `/books?order[title]&order[id]`), filters will not be applied unless you configure a default order direction to use:
116
 *
117
 * <div data-code-selector>
118
 *
119
 * ```php
120
 * <?php
121
 * // api/src/Entity/Book.php
122
 * use ApiPlatform\Metadata\ApiFilter;
123
 * use ApiPlatform\Metadata\ApiResource;
124
 * use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
125
 *
126
 * #[ApiResource]
127
 * #[ApiFilter(OrderFilter::class, properties: ['id' => 'ASC', 'title' => 'DESC'])]
128
 * class Book
129
 * {
130
 *     // ...
131
 * }
132
 * ```
133
 * ```yaml
134
 * # config/services.yaml
135
 * services:
136
 *     book.order_filter:
137
 *         parent: 'api_platform.doctrine.orm.order_filter'
138
 *         arguments: [ { id: ASC, title: DESC } ]
139
 *         tags:  [ 'api_platform.filter' ]
140
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
141
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
142
 *         autowire: false
143
 *         autoconfigure: false
144
 *         public: false
145
 *
146
 * # api/config/api_platform/resources.yaml
147
 * resources:
148
 *     App\Entity\Book:
149
 *         - operations:
150
 *               ApiPlatform\Metadata\GetCollection:
151
 *                   filters: ['book.order_filter']
152
 * ```
153
 * ```xml
154
 * <?xml version="1.0" encoding="UTF-8" ?>
155
 * <!-- api/config/services.xml -->
156
 * <?xml version="1.0" encoding="UTF-8" ?>
157
 * <container
158
 *         xmlns="http://symfony.com/schema/dic/services"
159
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
160
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
161
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
162
 *     <services>
163
 *         <service id="book.order_filter" parent="api_platform.doctrine.orm.order_filter">
164
 *             <argument type="collection">
165
 *                 <argument key="id">ASC</argument>
166
 *                 <argument key="title">DESC</argument>
167
 *             </argument>
168
 *             <tag name="api_platform.filter"/>
169
 *         </service>
170
 *     </services>
171
 * </container>
172
 * <!-- api/config/api_platform/resources.xml -->
173
 * <resources
174
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
175
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
176
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
177
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
178
 *     <resource class="App\Entity\Book">
179
 *         <operations>
180
 *             <operation class="ApiPlatform\Metadata\GetCollection">
181
 *                 <filters>
182
 *                     <filter>book.order_filter</filter>
183
 *                 </filters>
184
 *             </operation>
185
 *         </operations>
186
 *     </resource>
187
 * </resources>
188
 * ```
189
 *
190
 * </div>
191
 *
192
 * When the property used for ordering can contain `null` values, you may want to specify how `null` values are treated in the comparison:
193
 * - Use the default behavior of the DBMS: use `null` strategy
194
 * - Exclude items: use `ApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_SMALLEST` (`nulls_smallest`) strategy
195
 * - Consider items as oldest: use `ApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_LARGEST` (`nulls_largest`) strategy
196
 * - Consider items as youngest: use `ApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_ALWAYS_FIRST` (`nulls_always_first`) strategy
197
 * - Always include items: use `ApiPlatform\Doctrine\Orm\Filter\OrderFilter::NULLS_ALWAYS_LAST` (`nulls_always_last`) strategy
198
 *
199
 * @author Kévin Dunglas <dunglas@gmail.com>
200
 * @author Théo FIDRY <theo.fidry@gmail.com>
201
 */
202
final class OrderFilter extends AbstractFilter implements OrderFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
203
{
204
    use OrderFilterTrait;
205
    use PropertyPlaceholderOpenApiParameterTrait;
206

207
    public function __construct(?ManagerRegistry $managerRegistry = null, string $orderParameterName = 'order', ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null, private readonly ?string $orderNullsComparison = null)
208
    {
UNCOV
209
        if (null !== $properties) {
157✔
UNCOV
210
            $properties = array_map(static function ($propertyOptions) {
157✔
211
                // shorthand for default direction
UNCOV
212
                if (\is_string($propertyOptions)) {
157✔
UNCOV
213
                    $propertyOptions = [
12✔
UNCOV
214
                        'default_direction' => $propertyOptions,
12✔
UNCOV
215
                    ];
12✔
216
                }
217

UNCOV
218
                return $propertyOptions;
157✔
UNCOV
219
            }, $properties);
157✔
220
        }
221

UNCOV
222
        parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
157✔
223

UNCOV
224
        $this->orderParameterName = $orderParameterName;
157✔
225
    }
226

227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
231
    {
232
        if (
UNCOV
233
            isset($context['filters'])
13✔
UNCOV
234
            && (!isset($context['filters'][$this->orderParameterName]) || !\is_array($context['filters'][$this->orderParameterName]))
13✔
UNCOV
235
            && !isset($context['parameter'])
13✔
236
        ) {
UNCOV
237
            return;
2✔
238
        }
239

UNCOV
240
        $parameter = $context['parameter'] ?? null;
11✔
UNCOV
241
        if (null !== ($value = $context['filters'][$parameter?->getProperty()] ?? null)) {
11✔
UNCOV
242
            $this->filterProperty($this->denormalizePropertyName($parameter->getProperty()), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
8✔
243

UNCOV
244
            return;
8✔
245
        }
246

UNCOV
247
        foreach ($context['filters'][$this->orderParameterName] as $property => $value) {
3✔
UNCOV
248
            $this->filterProperty($this->denormalizePropertyName($property), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
3✔
249
        }
250
    }
251

252
    /**
253
     * {@inheritdoc}
254
     */
255
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
256
    {
UNCOV
257
        if (!$this->isPropertyEnabled($property, $resourceClass) || !$this->isPropertyMapped($property, $resourceClass)) {
11✔
258
            return;
×
259
        }
260

UNCOV
261
        $direction = $this->normalizeValue($value, $property);
11✔
UNCOV
262
        if (null === $direction) {
11✔
263
            return;
×
264
        }
265

UNCOV
266
        $alias = $queryBuilder->getRootAliases()[0];
11✔
UNCOV
267
        $field = $property;
11✔
268

UNCOV
269
        if ($this->isPropertyNested($property, $resourceClass)) {
11✔
270
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::LEFT_JOIN);
×
271
        }
272

UNCOV
273
        if (null !== $nullsComparison = $this->properties[$property]['nulls_comparison'] ?? $this->orderNullsComparison) {
11✔
274
            $nullsDirection = self::NULLS_DIRECTION_MAP[$nullsComparison][$direction];
×
275

276
            $nullRankHiddenField = \sprintf('_%s_%s_null_rank', $alias, str_replace('.', '_', $field));
×
277

278
            $queryBuilder->addSelect(\sprintf('CASE WHEN %s.%s IS NULL THEN 0 ELSE 1 END AS HIDDEN %s', $alias, $field, $nullRankHiddenField));
×
279
            $queryBuilder->addOrderBy($nullRankHiddenField, $nullsDirection);
×
280
        }
281

UNCOV
282
        $queryBuilder->addOrderBy(\sprintf('%s.%s', $alias, $field), $direction);
11✔
283
    }
284

285
    /**
286
     * @return array<string, mixed>
287
     */
288
    public function getSchema(Parameter $parameter): array
289
    {
UNCOV
290
        return ['type' => 'string', 'enum' => ['asc', 'desc']];
2✔
291
    }
292
}
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