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

api-platform / core / 10963383095

20 Sep 2024 05:06PM UTC coverage: 7.834%. Remained the same
10963383095

push

github

soyuka
test(laravel): standard put model should update when it exists

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

342 existing lines in 15 files now uncovered.

12915 of 164861 relevant lines covered (7.83%)

27.03 hits per line

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

96.43
/src/Doctrine/Odm/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\Odm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\OrderFilterTrait;
18
use ApiPlatform\Metadata\Operation;
19
use Doctrine\ODM\MongoDB\Aggregation\Builder;
20
use Doctrine\Persistence\ManagerRegistry;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23

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

203
    public function __construct(ManagerRegistry $managerRegistry, string $orderParameterName = 'order', ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null)
204
    {
UNCOV
205
        if (null !== $properties) {
771✔
UNCOV
206
            $properties = array_map(static function ($propertyOptions) {
771✔
207
                // shorthand for default direction
UNCOV
208
                if (\is_string($propertyOptions)) {
771✔
UNCOV
209
                    $propertyOptions = [
598✔
UNCOV
210
                        'default_direction' => $propertyOptions,
598✔
UNCOV
211
                    ];
598✔
212
                }
213

UNCOV
214
                return $propertyOptions;
771✔
UNCOV
215
            }, $properties);
771✔
216
        }
217

UNCOV
218
        parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
771✔
219

UNCOV
220
        $this->orderParameterName = $orderParameterName;
771✔
221
    }
222

223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
227
    {
UNCOV
228
        if (isset($context['filters']) && !isset($context['filters'][$this->orderParameterName])) {
272✔
UNCOV
229
            return;
228✔
230
        }
231

UNCOV
232
        if (!isset($context['filters'][$this->orderParameterName]) || !\is_array($context['filters'][$this->orderParameterName])) {
44✔
UNCOV
233
            parent::apply($aggregationBuilder, $resourceClass, $operation, $context);
2✔
234

UNCOV
235
            return;
2✔
236
        }
237

UNCOV
238
        foreach ($context['filters'][$this->orderParameterName] as $property => $value) {
42✔
UNCOV
239
            $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
42✔
240
        }
241
    }
242

243
    /**
244
     * {@inheritdoc}
245
     */
246
    protected function filterProperty(string $property, $direction, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
247
    {
UNCOV
248
        if (!$this->isPropertyEnabled($property, $resourceClass) || !$this->isPropertyMapped($property, $resourceClass)) {
44✔
UNCOV
249
            return;
12✔
250
        }
251

UNCOV
252
        $direction = $this->normalizeValue($direction, $property);
34✔
UNCOV
253
        if (null === $direction) {
34✔
254
            return;
×
255
        }
256

UNCOV
257
        $matchField = $property;
34✔
258

UNCOV
259
        if ($this->isPropertyNested($property, $resourceClass)) {
34✔
UNCOV
260
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass, true);
6✔
261
        }
262

UNCOV
263
        $aggregationBuilder->sort(
34✔
UNCOV
264
            $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$matchField => $direction]
34✔
UNCOV
265
        );
34✔
266
    }
267
}
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