• 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

89.47
/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\JsonSchemaFilterInterface;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Parameter;
20
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
22

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

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

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

UNCOV
135
        $values = $this->normalizeValues($value, $property);
14✔
UNCOV
136
        if (null === $values) {
14✔
UNCOV
137
            return;
11✔
138
        }
139

UNCOV
140
        $matchField = $property;
3✔
141

UNCOV
142
        if ($this->isPropertyNested($property, $resourceClass)) {
3✔
143
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
×
144
        }
145

UNCOV
146
        if (1 === \count($values)) {
3✔
UNCOV
147
            $aggregationBuilder->match()->field($matchField)->equals($values[0]);
1✔
148
        } else {
UNCOV
149
            $aggregationBuilder->match()->field($matchField)->in($values);
2✔
150
        }
151
    }
152

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

UNCOV
162
        if (MongoDbType::FLOAT === $doctrineType) {
214✔
UNCOV
163
            return 'float';
210✔
164
        }
165

UNCOV
166
        return 'int';
149✔
167
    }
168

169
    public function getSchema(Parameter $parameter): array
170
    {
UNCOV
171
        return ['type' => 'numeric'];
1✔
172
    }
173
}
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