• 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

79.17
/src/Doctrine/Odm/Filter/ExistsFilter.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\ExistsFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\ExistsFilterTrait;
18
use ApiPlatform\Doctrine\Common\Filter\PropertyPlaceholderOpenApiParameterTrait;
19
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
20
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Parameter;
23
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
25
use Doctrine\Persistence\ManagerRegistry;
26
use Psr\Log\LoggerInterface;
27
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
28

29
/**
30
 * The exists filter allows you to select items based on a nullable field value. It will also check the emptiness of a collection association.
31
 *
32
 * Syntax: `?exists[property]=<true|false|1|0>`.
33
 *
34
 * <div data-code-selector>
35
 *
36
 * ```php
37
 * <?php
38
 * // api/src/Entity/Book.php
39
 * use ApiPlatform\Metadata\ApiFilter;
40
 * use ApiPlatform\Metadata\ApiResource;
41
 * use ApiPlatform\Doctrine\Odm\Filter\ExistFilter;
42
 *
43
 * #[ApiResource]
44
 * #[ApiFilter(ExistFilter::class, properties: ['comment'])]
45
 * class Book
46
 * {
47
 *     // ...
48
 * }
49
 * ```
50
 *
51
 * ```yaml
52
 * # config/services.yaml
53
 * services:
54
 *     book.exist_filter:
55
 *         parent: 'api_platform.doctrine.odm.exist_filter'
56
 *         arguments: [ { comment: ~ } ]
57
 *         tags:  [ 'api_platform.filter' ]
58
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
59
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
60
 *         autowire: false
61
 *         autoconfigure: false
62
 *         public: false
63
 *
64
 * # api/config/api_platform/resources.yaml
65
 * resources:
66
 *     App\Entity\Book:
67
 *         - operations:
68
 *               ApiPlatform\Metadata\GetCollection:
69
 *                   filters: ['book.exist_filter']
70
 * ```
71
 *
72
 * ```xml
73
 * <!-- api/config/services.xml -->
74
 * <?xml version="1.0" encoding="UTF-8" ?>
75
 * <container
76
 *         xmlns="http://symfony.com/schema/dic/services"
77
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
78
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
79
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
80
 *     <services>
81
 *         <service id="book.exist_filter" parent="api_platform.doctrine.odm.exist_filter">
82
 *             <argument type="collection">
83
 *                 <argument key="comment"/>
84
 *             </argument>
85
 *             <tag name="api_platform.filter"/>
86
 *         </service>
87
 *     </services>
88
 * </container>
89
 * <!-- api/config/api_platform/resources.xml -->
90
 * <resources
91
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
92
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
94
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
95
 *     <resource class="App\Entity\Book">
96
 *         <operations>
97
 *             <operation class="ApiPlatform\Metadata\GetCollection">
98
 *                 <filters>
99
 *                     <filter>book.exist_filter</filter>
100
 *                 </filters>
101
 *             </operation>
102
 *         </operations>
103
 *     </resource>
104
 * </resources>
105
 * ```
106
 *
107
 * </div>
108
 *
109
 * Given that the collection endpoint is `/books`, you can filter books with the following query: `/books?exists[comment]=true`.
110
 *
111
 * @author Teoh Han Hui <teohhanhui@gmail.com>
112
 * @author Alan Poulain <contact@alanpoulain.eu>
113
 */
114
final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
115
{
116
    use ExistsFilterTrait;
117
    use PropertyPlaceholderOpenApiParameterTrait;
118

119
    public function __construct(?ManagerRegistry $managerRegistry = null, ?LoggerInterface $logger = null, ?array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, ?NameConverterInterface $nameConverter = null)
120
    {
UNCOV
121
        parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
295✔
122

UNCOV
123
        $this->existsParameterName = $existsParameterName;
295✔
124
    }
125

126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
130
    {
UNCOV
131
        $parameter = $context['parameter'] ?? null;
123✔
UNCOV
132
        if (null !== ($value = $context['filters'][$parameter?->getProperty()] ?? null)) {
123✔
133
            $this->filterProperty($this->denormalizePropertyName($parameter->getProperty()), $value, $aggregationBuilder, $resourceClass, $operation, $context);
×
134

135
            return;
×
136
        }
137

UNCOV
138
        foreach ($context['filters'][$this->existsParameterName] ?? [] as $property => $value) {
123✔
UNCOV
139
            $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
6✔
140
        }
141
    }
142

143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
147
    {
148
        if (
UNCOV
149
            !$this->isPropertyEnabled($property, $resourceClass)
6✔
UNCOV
150
            || !$this->isPropertyMapped($property, $resourceClass, true)
6✔
UNCOV
151
            || !$this->isNullableField($property, $resourceClass)
6✔
152
        ) {
153
            return;
×
154
        }
155

UNCOV
156
        $value = $this->normalizeValue($value, $property);
6✔
UNCOV
157
        if (null === $value) {
6✔
158
            return;
×
159
        }
160

UNCOV
161
        $matchField = $property;
6✔
162

UNCOV
163
        if ($this->isPropertyNested($property, $resourceClass)) {
6✔
164
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
×
165
        }
166

UNCOV
167
        $aggregationBuilder->match()->field($matchField)->{$value ? 'notEqual' : 'equals'}(null);
6✔
168
    }
169

170
    /**
171
     * {@inheritdoc}
172
     */
173
    protected function isNullableField(string $property, string $resourceClass): bool
174
    {
UNCOV
175
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
213✔
UNCOV
176
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
213✔
177

UNCOV
178
        $field = $propertyParts['field'];
213✔
179

UNCOV
180
        return $metadata instanceof ClassMetadata && $metadata->hasField($field) ? $metadata->isNullable($field) : false;
213✔
181
    }
182

183
    public function getSchema(Parameter $parameter): array
184
    {
UNCOV
185
        return ['type' => 'boolean'];
1✔
186
    }
187
}
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