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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

0.0
/src/Doctrine/Odm/Filter/NumericFilter.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\NumericFilterTrait;
17
use ApiPlatform\Metadata\Operation;
18
use Doctrine\ODM\MongoDB\Aggregation\Builder;
19
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
20

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

111
    /**
112
     * Type of numeric in Doctrine.
113
     */
114
    public const DOCTRINE_NUMERIC_TYPES = [
115
        MongoDbType::INT => true,
116
        MongoDbType::INTEGER => true,
117
        MongoDbType::FLOAT => true,
118
    ];
119

120
    /**
121
     * {@inheritdoc}
122
     */
123
    protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
124
    {
125
        if (
126
            !$this->isPropertyEnabled($property, $resourceClass)
×
127
            || !$this->isPropertyMapped($property, $resourceClass)
×
128
            || !$this->isNumericField($property, $resourceClass)
×
129
        ) {
130
            return;
×
131
        }
132

133
        $values = $this->normalizeValues($value, $property);
×
134
        if (null === $values) {
×
135
            return;
×
136
        }
137

138
        $matchField = $property;
×
139

140
        if ($this->isPropertyNested($property, $resourceClass)) {
×
141
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
×
142
        }
143

144
        if (1 === \count($values)) {
×
145
            $aggregationBuilder->match()->field($matchField)->equals($values[0]);
×
146
        } else {
147
            $aggregationBuilder->match()->field($matchField)->in($values);
×
148
        }
149
    }
150

151
    /**
152
     * {@inheritdoc}
153
     */
154
    protected function getType(?string $doctrineType = null): string
155
    {
156
        if (null === $doctrineType) {
×
157
            return 'string';
×
158
        }
159

160
        if (MongoDbType::FLOAT === $doctrineType) {
×
161
            return 'float';
×
162
        }
163

164
        return 'int';
×
165
    }
166
}
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