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

api-platform / core / 10367357205

13 Aug 2024 09:38AM UTC coverage: 7.027% (-0.8%) from 7.84%
10367357205

push

github

soyuka
chore: align dependencies across components

11371 of 161813 relevant lines covered (7.03%)

22.93 hits per line

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

95.83
/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\Operation;
19
use Doctrine\DBAL\Types\Types;
20
use Doctrine\ORM\Query\Expr\Join;
21
use Doctrine\ORM\QueryBuilder;
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\Orm\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.orm.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
 * <?xml version="1.0" encoding="UTF-8" ?>
68
 * <!-- api/config/services.xml -->
69
 * <?xml version="1.0" encoding="UTF-8" ?>
70
 * <container
71
 *         xmlns="http://symfony.com/schema/dic/services"
72
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
73
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
74
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
75
 *     <services>
76
 *         <service id="book.numeric_filter" parent="api_platform.doctrine.orm.numeric_filter">
77
 *             <argument type="collection">
78
 *                 <argument key="price"/>
79
 *             </argument>
80
 *             <tag name="api_platform.filter"/>
81
 *         </service>
82
 *     </services>
83
 * </container>
84
 * <!-- api/config/api_platform/resources.xml -->
85
 * <resources
86
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
87
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
89
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
90
 *     <resource class="App\Entity\Book">
91
 *         <operations>
92
 *             <operation class="ApiPlatform\Metadata\GetCollection">
93
 *                 <filters>
94
 *                     <filter>book.numeric_filter</filter>
95
 *                 </filters>
96
 *             </operation>
97
 *         </operations>
98
 *     </resource>
99
 * </resources>
100
 * ```
101
 *
102
 * </div>
103
 *
104
 * Given that the collection endpoint is `/books`, you can filter books with the following query: `/books?price=10`.
105
 *
106
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
107
 * @author Teoh Han Hui <teohhanhui@gmail.com>
108
 */
109
final class NumericFilter extends AbstractFilter
110
{
111
    use NumericFilterTrait;
112

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

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

139
        $values = $this->normalizeValues($value, $property);
14✔
140
        if (null === $values) {
14✔
141
            return;
11✔
142
        }
143

144
        $alias = $queryBuilder->getRootAliases()[0];
3✔
145
        $field = $property;
3✔
146

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

151
        $valueParameter = $queryNameGenerator->generateParameterName($field);
3✔
152

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

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

173
        if (Types::FLOAT === $doctrineType) {
232✔
174
            return 'float';
228✔
175
        }
176

177
        return 'int';
162✔
178
    }
179
}
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

© 2026 Coveralls, Inc