• 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

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

16
use ApiPlatform\Doctrine\Common\Filter\NumericFilterTrait;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Parameter;
21
use Doctrine\DBAL\Types\Types;
22
use Doctrine\ORM\Query\Expr\Join;
23
use Doctrine\ORM\QueryBuilder;
24

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

115
    /**
116
     * Type of numeric in Doctrine.
117
     *
118
     * @see http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/types.html
119
     */
120
    public const DOCTRINE_NUMERIC_TYPES = [
121
        Types::BIGINT => true,
122
        Types::DECIMAL => true,
123
        Types::FLOAT => true,
124
        Types::INTEGER => true,
125
        Types::SMALLINT => true,
126
    ];
127

128
    /**
129
     * {@inheritdoc}
130
     */
131
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
132
    {
133
        if (
UNCOV
134
            !$this->isPropertyEnabled($property, $resourceClass)
9✔
UNCOV
135
            || !$this->isPropertyMapped($property, $resourceClass)
9✔
UNCOV
136
            || !$this->isNumericField($property, $resourceClass)
9✔
137
        ) {
138
            return;
×
139
        }
140

UNCOV
141
        $values = $this->normalizeValues($value, $property);
9✔
UNCOV
142
        if (null === $values) {
9✔
UNCOV
143
            return;
6✔
144
        }
145

UNCOV
146
        $alias = $queryBuilder->getRootAliases()[0];
3✔
UNCOV
147
        $field = $property;
3✔
148

UNCOV
149
        if ($this->isPropertyNested($property, $resourceClass)) {
3✔
150
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
×
151
        }
152

UNCOV
153
        $valueParameter = $queryNameGenerator->generateParameterName($field);
3✔
154

UNCOV
155
        if (1 === \count($values)) {
3✔
UNCOV
156
            $queryBuilder
3✔
UNCOV
157
                ->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
3✔
UNCOV
158
                ->setParameter($valueParameter, $values[0], (string) $this->getDoctrineFieldType($property, $resourceClass));
3✔
159
        } else {
160
            $queryBuilder
×
161
                ->andWhere(\sprintf('%s.%s IN (:%s)', $alias, $field, $valueParameter))
×
162
                ->setParameter($valueParameter, $values);
×
163
        }
164
    }
165

166
    /**
167
     * {@inheritdoc}
168
     */
169
    protected function getType(?string $doctrineType = null): string
170
    {
UNCOV
171
        if (null === $doctrineType || Types::DECIMAL === $doctrineType) {
5✔
UNCOV
172
            return 'string';
5✔
173
        }
174

UNCOV
175
        if (Types::FLOAT === $doctrineType) {
5✔
UNCOV
176
            return 'float';
5✔
177
        }
178

179
        return 'int';
×
180
    }
181

182
    public function getSchema(Parameter $parameter): array
183
    {
UNCOV
184
        return ['type' => 'number'];
1✔
185
    }
186
}
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