• 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

78.57
/src/Doctrine/Orm/Filter/BackedEnumFilter.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\BackedEnumFilterTrait;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\Operation;
19
use Doctrine\ORM\Mapping\ClassMetadata;
20
use Doctrine\ORM\Mapping\FieldMapping;
21
use Doctrine\ORM\Query\Expr\Join;
22
use Doctrine\ORM\QueryBuilder;
23

24
/**
25
 * The backed enum filter allows you to search on backed enum fields and values.
26
 *
27
 * Note: it is possible to filter on properties and relations too.
28
 *
29
 * Syntax: `?property=foo`.
30
 *
31
 * <div data-code-selector>
32
 *
33
 * ```php
34
 * <?php
35
 * // api/src/Entity/Book.php
36
 * use ApiPlatform\Metadata\ApiFilter;
37
 * use ApiPlatform\Metadata\ApiResource;
38
 * use ApiPlatform\Doctrine\Orm\Filter\BackedEnumFilter;
39
 *
40
 * #[ApiResource]
41
 * #[ApiFilter(BackedEnumFilter::class, properties: ['status'])]
42
 * class Book
43
 * {
44
 *     // ...
45
 * }
46
 * ```
47
 *
48
 * ```yaml
49
 * # config/services.yaml
50
 * services:
51
 *     book.backed_enum_filter:
52
 *         parent: 'api_platform.doctrine.orm.backed_enum_filter'
53
 *         arguments: [ { status: ~ } ]
54
 *         tags:  [ 'api_platform.filter' ]
55
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
56
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
57
 *         autowire: false
58
 *         autoconfigure: false
59
 *         public: false
60
 *
61
 * # api/config/api_platform/resources.yaml
62
 * resources:
63
 *     App\Entity\Book:
64
 *         - operations:
65
 *               ApiPlatform\Metadata\GetCollection:
66
 *                   filters: ['book.backed_enum_filter']
67
 * ```
68
 *
69
 * ```xml
70
 * <?xml version="1.0" encoding="UTF-8" ?>
71
 * <!-- api/config/services.xml -->
72
 * <?xml version="1.0" encoding="UTF-8" ?>
73
 * <container
74
 *         xmlns="http://symfony.com/schema/dic/services"
75
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
76
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
77
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
78
 *     <services>
79
 *         <service id="book.backed_enum_filter" parent="api_platform.doctrine.orm.backed_enum_filter">
80
 *             <argument type="collection">
81
 *                 <argument key="status"/>
82
 *             </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.backed_enum_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 with the following query: `/books?status=published`.
108
 *
109
 * @author Rémi Marseille <marseille.remi@gmail.com>
110
 */
111
final class BackedEnumFilter extends AbstractFilter
112
{
113
    use BackedEnumFilterTrait;
114

115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
119
    {
120
        if (
UNCOV
121
            !$this->isPropertyEnabled($property, $resourceClass)
2✔
UNCOV
122
            || !$this->isPropertyMapped($property, $resourceClass)
2✔
UNCOV
123
            || !$this->isBackedEnumField($property, $resourceClass)
2✔
124
        ) {
125
            return;
×
126
        }
127

UNCOV
128
        $value = $this->normalizeValue($value, $property);
2✔
UNCOV
129
        if (null === $value) {
2✔
130
            return;
×
131
        }
132

UNCOV
133
        $alias = $queryBuilder->getRootAliases()[0];
2✔
UNCOV
134
        $field = $property;
2✔
135

UNCOV
136
        if ($this->isPropertyNested($property, $resourceClass)) {
2✔
137
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
×
138
        }
139

UNCOV
140
        $valueParameter = $queryNameGenerator->generateParameterName($field);
2✔
141

UNCOV
142
        $queryBuilder
2✔
UNCOV
143
            ->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
2✔
UNCOV
144
            ->setParameter($valueParameter, $value);
2✔
145
    }
146

147
    /**
148
     * {@inheritdoc}
149
     */
150
    protected function isBackedEnumField(string $property, string $resourceClass): bool
151
    {
UNCOV
152
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
2✔
UNCOV
153
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
2✔
154

UNCOV
155
        if (!$metadata instanceof ClassMetadata) {
2✔
156
            return false;
×
157
        }
158

UNCOV
159
        $fieldMapping = $metadata->fieldMappings[$propertyParts['field']];
2✔
160

161
        // Doctrine ORM 2.x returns an array and Doctrine ORM 3.x returns a FieldMapping object
UNCOV
162
        if ($fieldMapping instanceof FieldMapping) {
2✔
UNCOV
163
            $fieldMapping = (array) $fieldMapping;
2✔
164
        }
165

UNCOV
166
        if (!$enumType = $fieldMapping['enumType']) {
2✔
167
            return false;
×
168
        }
169

UNCOV
170
        if (!($enumType::cases()[0] ?? null) instanceof \BackedEnum) {
2✔
171
            return false;
×
172
        }
173

UNCOV
174
        $this->enumTypes[$property] = $enumType;
2✔
175

UNCOV
176
        return true;
2✔
177
    }
178
}
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