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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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)
×
UNCOV
122
            || !$this->isPropertyMapped($property, $resourceClass)
×
UNCOV
123
            || !$this->isBackedEnumField($property, $resourceClass)
×
124
        ) {
125
            return;
×
126
        }
127

UNCOV
128
        $values = \is_array($value) ? $value : [$value];
×
129

UNCOV
130
        $normalizedValues = array_filter(array_map(
×
UNCOV
131
            fn ($v) => $this->normalizeValue($v, $property),
×
UNCOV
132
            $values
×
UNCOV
133
        ));
×
134

UNCOV
135
        if (empty($normalizedValues)) {
×
136
            return;
×
137
        }
138

UNCOV
139
        $alias = $queryBuilder->getRootAliases()[0];
×
UNCOV
140
        $field = $property;
×
141

UNCOV
142
        if ($this->isPropertyNested($property, $resourceClass)) {
×
143
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
×
144
        }
145

UNCOV
146
        $valueParameter = $queryNameGenerator->generateParameterName($field);
×
147

UNCOV
148
        if (1 === \count($values)) {
×
UNCOV
149
            $queryBuilder
×
UNCOV
150
                ->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
×
UNCOV
151
                ->setParameter($valueParameter, $values[0]);
×
152

UNCOV
153
            return;
×
154
        }
155

156
        $queryBuilder
×
157
            ->andWhere(\sprintf('%s.%s IN (:%s)', $alias, $field, $valueParameter))
×
158
            ->setParameter($valueParameter, $values);
×
159
    }
160

161
    /**
162
     * {@inheritdoc}
163
     */
164
    protected function isBackedEnumField(string $property, string $resourceClass): bool
165
    {
UNCOV
166
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
×
UNCOV
167
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
×
168

UNCOV
169
        if (!$metadata instanceof ClassMetadata) {
×
170
            return false;
×
171
        }
172

UNCOV
173
        $fieldMapping = $metadata->fieldMappings[$propertyParts['field']];
×
174

175
        // Doctrine ORM 2.x returns an array and Doctrine ORM 3.x returns a FieldMapping object
UNCOV
176
        if ($fieldMapping instanceof FieldMapping) {
×
UNCOV
177
            $fieldMapping = (array) $fieldMapping;
×
178
        }
179

UNCOV
180
        if (!($enumType = $fieldMapping['enumType'] ?? null)) {
×
181
            return false;
×
182
        }
183

UNCOV
184
        if (!($enumType::cases()[0] ?? null) instanceof \BackedEnum) {
×
185
            return false;
×
186
        }
187

UNCOV
188
        $this->enumTypes[$property] = $enumType;
×
189

UNCOV
190
        return true;
×
191
    }
192
}
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