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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

91.67
/src/Doctrine/Odm/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\Odm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\BooleanFilterTrait;
17
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Parameter;
20
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
22

23
/**
24
 * The boolean filter allows you to search on boolean fields and values.
25
 *
26
 * Syntax: `?property=<true|false|1|0>`.
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\Odm\Filter\BooleanFilter;
36
 *
37
 * #[ApiResource]
38
 * #[ApiFilter(BooleanFilter::class, properties: ['published'])]
39
 * class Book
40
 * {
41
 *     // ...
42
 * }
43
 * ```
44
 *
45
 * ```yaml
46
 * # config/services.yaml
47
 * services:
48
 *     book.boolean_filter:
49
 *         parent: 'api_platform.doctrine.odm.boolean_filter'
50
 *         arguments: [ { published: ~ } ]
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.boolean_filter']
64
 * ```
65
 *
66
 * ```xml
67
 * <!-- api/config/services.xml -->
68
 * <?xml version="1.0" encoding="UTF-8" ?>
69
 * <container
70
 *         xmlns="http://symfony.com/schema/dic/services"
71
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
72
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
73
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
74
 *     <services>
75
 *         <service id="book.boolean_filter" parent="api_platform.doctrine.odm.boolean_filter">
76
 *             <argument type="collection">
77
 *                 <argument key="published"></argument>
78
 *             </argument>
79
 *             <tag name="api_platform.filter"/>
80
 *         </service>
81
 *     </services>
82
 * </container>
83
 * <!-- api/config/api_platform/resources.xml -->
84
 * <resources
85
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
86
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
87
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
88
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
89
 *     <resource class="App\Entity\Book">
90
 *         <operations>
91
 *             <operation class="ApiPlatform\Metadata\GetCollection">
92
 *                 <filters>
93
 *                     <filter>book.boolean_filter</filter>
94
 *                 </filters>
95
 *             </operation>
96
 *         </operations>
97
 *     </resource>
98
 * </resources>
99
 * ```
100
 *
101
 * </div>
102
 *
103
 * Given that the collection endpoint is `/books`, you can filter books with the following query: `/books?published=true`.
104
 *
105
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
106
 * @author Teoh Han Hui <teohhanhui@gmail.com>
107
 * @author Alan Poulain <contact@alanpoulain.eu>
108
 */
109
final class BooleanFilter extends AbstractFilter implements JsonSchemaFilterInterface
110
{
111
    use BooleanFilterTrait;
112

113
    public const DOCTRINE_BOOLEAN_TYPES = [
114
        MongoDbType::BOOL => true,
115
        MongoDbType::BOOLEAN => true,
116
    ];
117

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

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

UNCOV
136
        $matchField = $property;
12✔
137

UNCOV
138
        if ($this->isPropertyNested($property, $resourceClass)) {
12✔
UNCOV
139
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
5✔
140
        }
141

UNCOV
142
        $aggregationBuilder->match()->field($matchField)->equals($value);
12✔
143
    }
144

145
    /**
146
     * @return array<string, string>
147
     */
148
    public function getSchema(Parameter $parameter): array
149
    {
UNCOV
150
        return ['type' => 'boolean'];
1✔
151
    }
152
}
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