• 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

87.5
/src/Doctrine/Orm/Filter/BooleanFilter.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\BooleanFilterTrait;
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 boolean filter allows you to search on boolean fields and values.
27
 *
28
 * Syntax: `?property=<true|false|1|0>`.
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\BooleanFilter;
38
 *
39
 * #[ApiResource]
40
 * #[ApiFilter(BooleanFilter::class, properties: ['published'])]
41
 * class Book
42
 * {
43
 *     // ...
44
 * }
45
 * ```
46
 *
47
 * ```yaml
48
 * # config/services.yaml
49
 * services:
50
 *     book.boolean_filter:
51
 *         parent: 'api_platform.doctrine.orm.boolean_filter'
52
 *         arguments: [ { published: ~ } ]
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.boolean_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.boolean_filter" parent="api_platform.doctrine.orm.boolean_filter">
79
 *             <argument type="collection">
80
 *                 <argument key="published"/>
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.boolean_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?published=true`.
107
 *
108
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
109
 * @author Teoh Han Hui <teohhanhui@gmail.com>
110
 */
111
final class BooleanFilter extends AbstractFilter implements JsonSchemaFilterInterface
112
{
113
    use BooleanFilterTrait;
114

115
    public const DOCTRINE_BOOLEAN_TYPES = [
116
        Types::BOOLEAN => true,
117
    ];
118

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

UNCOV
132
        $value = $this->normalizeValue($value, $property);
12✔
UNCOV
133
        if (null === $value) {
12✔
UNCOV
134
            return;
4✔
135
        }
136

UNCOV
137
        $alias = $queryBuilder->getRootAliases()[0];
8✔
UNCOV
138
        $field = $property;
8✔
139

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

UNCOV
144
        $valueParameter = $queryNameGenerator->generateParameterName($field);
8✔
145

UNCOV
146
        $queryBuilder
8✔
UNCOV
147
            ->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
8✔
UNCOV
148
            ->setParameter($valueParameter, $value);
8✔
149
    }
150

151
    /**
152
     * @return array<string, string>
153
     */
154
    public function getSchema(Parameter $parameter): array
155
    {
UNCOV
156
        return ['type' => 'boolean'];
1✔
157
    }
158
}
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