• 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.3
/src/Doctrine/Odm/Filter/SearchFilter.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\SearchFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\SearchFilterTrait;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
20
use ApiPlatform\Metadata\IriConverterInterface;
21
use ApiPlatform\Metadata\Operation;
22
use Doctrine\ODM\MongoDB\Aggregation\Builder;
23
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDBClassMetadata;
24
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
25
use Doctrine\Persistence\ManagerRegistry;
26
use Doctrine\Persistence\Mapping\ClassMetadata;
27
use MongoDB\BSON\Regex;
28
use Psr\Log\LoggerInterface;
29
use Symfony\Component\PropertyAccess\PropertyAccess;
30
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
31
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
32

33
/**
34
 * The search filter allows to filter a collection by given properties.
35
 *
36
 * The search filter supports `exact`, `partial`, `start`, `end`, and `word_start` matching strategies:
37
 * - `exact` strategy searches for fields that exactly match the value
38
 * - `partial` strategy uses `LIKE %value%` to search for fields that contain the value
39
 * - `start` strategy uses `LIKE value%` to search for fields that start with the value
40
 * - `end` strategy uses `LIKE %value` to search for fields that end with the value
41
 * - `word_start` strategy uses `LIKE value% OR LIKE % value%` to search for fields that contain words starting with the value
42
 *
43
 * Note: it is possible to filter on properties and relations too.
44
 *
45
 * Prepend the letter `i` to the filter if you want it to be case-insensitive. For example `ipartial` or `iexact`.
46
 * Note that this will use the `LOWER` function and *will* impact performance if there is no proper index.
47
 *
48
 * Case insensitivity may already be enforced at the database level depending on the [collation](https://en.wikipedia.org/wiki/Collation) used.
49
 * If you are using MySQL, note that the commonly used `utf8_unicode_ci` collation (and its sibling `utf8mb4_unicode_ci`)
50
 * are already case-insensitive, as indicated by the `_ci` part in their names.
51
 *
52
 * Note: Search filters with the `exact` strategy can have multiple values for the same property (in this case the
53
 * condition will be similar to a SQL IN clause).
54
 *
55
 * Syntax: `?property[]=foo&property[]=bar`.
56
 *
57
 * <div data-code-selector>
58
 *
59
 * ```php
60
 * <?php
61
 * // api/src/Entity/Book.php
62
 * use ApiPlatform\Metadata\ApiFilter;
63
 * use ApiPlatform\Metadata\ApiResource;
64
 * use ApiPlatform\Doctrine\Odm\Filter\SearchFilter;
65
 *
66
 * #[ApiResource]
67
 * #[ApiFilter(SearchFilter::class, properties: ['isbn' => 'exact', 'description' => 'partial'])]
68
 * class Book
69
 * {
70
 *     // ...
71
 * }
72
 * ```
73
 *
74
 * ```yaml
75
 * # config/services.yaml
76
 * services:
77
 *     book.search_filter:
78
 *         parent: 'api_platform.doctrine.odm.search_filter'
79
 *         arguments: [ { isbn: 'exact', description: 'partial' } ]
80
 *         tags:  [ 'api_platform.filter' ]
81
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
82
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
83
 *         autowire: false
84
 *         autoconfigure: false
85
 *         public: false
86
 *
87
 * # api/config/api_platform/resources.yaml
88
 * resources:
89
 *     App\Entity\Book:
90
 *         - operations:
91
 *               ApiPlatform\Metadata\GetCollection:
92
 *                   filters: ['book.search_filter']
93
 * ```
94
 *
95
 * ```xml
96
 * <?xml version="1.0" encoding="UTF-8" ?>
97
 * <!-- api/config/services.xml -->
98
 * <?xml version="1.0" encoding="UTF-8" ?>
99
 * <container
100
 *         xmlns="http://symfony.com/schema/dic/services"
101
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
102
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
103
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
104
 *     <services>
105
 *         <service id="book.search_filter" parent="api_platform.doctrine.odm.search_filter">
106
 *             <argument type="collection">
107
 *                 <argument key="isbn">exact</argument>
108
 *                 <argument key="description">partial</argument>
109
 *             </argument>
110
 *             <tag name="api_platform.filter"/>
111
 *         </service>
112
 *     </services>
113
 * </container>
114
 * <!-- api/config/api_platform/resources.xml -->
115
 * <resources
116
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
117
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
118
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
119
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
120
 *     <resource class="App\Entity\Book">
121
 *         <operations>
122
 *             <operation class="ApiPlatform\Metadata\GetCollection">
123
 *                 <filters>
124
 *                     <filter>book.search_filter</filter>
125
 *                 </filters>
126
 *             </operation>
127
 *         </operations>
128
 *     </resource>
129
 * </resources>
130
 * ```
131
 *
132
 * </div>
133
 *
134
 * @author Kévin Dunglas <dunglas@gmail.com>
135
 * @author Alan Poulain <contact@alanpoulain.eu>
136
 */
137
final class SearchFilter extends AbstractFilter implements SearchFilterInterface
138
{
139
    use SearchFilterTrait;
140

141
    public const DOCTRINE_INTEGER_TYPE = [MongoDbType::INTEGER, MongoDbType::INT];
142

143
    public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface $iriConverter, ?IdentifiersExtractorInterface $identifiersExtractor, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null)
144
    {
UNCOV
145
        parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
318✔
146

UNCOV
147
        $this->iriConverter = $iriConverter;
318✔
UNCOV
148
        $this->identifiersExtractor = $identifiersExtractor;
318✔
UNCOV
149
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
318✔
150
    }
151

152
    protected function getIriConverter(): IriConverterInterface
153
    {
UNCOV
154
        return $this->iriConverter;
8✔
155
    }
156

157
    protected function getPropertyAccessor(): PropertyAccessorInterface
158
    {
159
        return $this->propertyAccessor;
×
160
    }
161

162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
166
    {
167
        if (
UNCOV
168
            null === $value
118✔
UNCOV
169
            || !$this->isPropertyEnabled($property, $resourceClass)
118✔
UNCOV
170
            || !$this->isPropertyMapped($property, $resourceClass, true)
118✔
171
        ) {
UNCOV
172
            return;
86✔
173
        }
174

UNCOV
175
        $matchField = $field = $property;
40✔
176

UNCOV
177
        $values = $this->normalizeValues((array) $value, $property);
40✔
UNCOV
178
        if (null === $values) {
40✔
UNCOV
179
            return;
1✔
180
        }
181

UNCOV
182
        $associations = [];
39✔
UNCOV
183
        if ($this->isPropertyNested($property, $resourceClass)) {
39✔
UNCOV
184
            [$matchField, $field, $associations] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
12✔
185
        }
186

UNCOV
187
        $caseSensitive = true;
37✔
UNCOV
188
        $strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
37✔
189

190
        // prefixing the strategy with i makes it case insensitive
UNCOV
191
        if (str_starts_with($strategy, 'i')) {
37✔
UNCOV
192
            $strategy = substr($strategy, 1);
2✔
UNCOV
193
            $caseSensitive = false;
2✔
194
        }
195

196
        /** @var MongoDBClassMetadata */
UNCOV
197
        $metadata = $this->getNestedMetadata($resourceClass, $associations);
37✔
198

UNCOV
199
        if ($metadata->hasField($field) && !$metadata->hasAssociation($field)) {
37✔
UNCOV
200
            if ('id' === $field) {
34✔
UNCOV
201
                $values = array_map($this->getIdFromValue(...), $values);
5✔
202
            }
203

UNCOV
204
            if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
34✔
UNCOV
205
                $this->logger->notice('Invalid filter ignored', [
1✔
UNCOV
206
                    'exception' => new InvalidArgumentException(\sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
1✔
UNCOV
207
                ]);
1✔
208

UNCOV
209
                return;
1✔
210
            }
211

UNCOV
212
            $this->addEqualityMatchStrategy($strategy, $aggregationBuilder, $field, $matchField, $values, $caseSensitive, $metadata);
33✔
213

UNCOV
214
            return;
33✔
215
        }
216

217
        // metadata doesn't have the field, nor an association on the field
UNCOV
218
        if (!$metadata->hasAssociation($field)) {
3✔
219
            return;
×
220
        }
221

UNCOV
222
        $values = array_map($this->getIdFromValue(...), $values);
3✔
223

UNCOV
224
        $associationResourceClass = $metadata->getAssociationTargetClass($field);
3✔
UNCOV
225
        $associationFieldIdentifier = $metadata->getIdentifierFieldNames()[0];
3✔
UNCOV
226
        $doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass);
3✔
227

UNCOV
228
        if (!$this->hasValidValues($values, $doctrineTypeField)) {
3✔
229
            $this->logger->notice('Invalid filter ignored', [
×
230
                'exception' => new InvalidArgumentException(\sprintf('Values for field "%s" are not valid according to the doctrine type.', $property)),
×
231
            ]);
×
232

233
            return;
×
234
        }
235

UNCOV
236
        $this->addEqualityMatchStrategy($strategy, $aggregationBuilder, $field, $matchField, $values, $caseSensitive, $metadata);
3✔
237
    }
238

239
    /**
240
     * Add equality match stage according to the strategy.
241
     */
242
    private function addEqualityMatchStrategy(string $strategy, Builder $aggregationBuilder, string $field, string $matchField, array $values, bool $caseSensitive, ClassMetadata $metadata): void
243
    {
UNCOV
244
        $inValues = [];
36✔
UNCOV
245
        foreach ($values as $inValue) {
36✔
UNCOV
246
            $inValues[] = $this->getEqualityMatchStrategyValue($strategy, $field, $inValue, $caseSensitive, $metadata);
36✔
247
        }
248

UNCOV
249
        $aggregationBuilder
36✔
UNCOV
250
            ->match()
36✔
UNCOV
251
            ->field($matchField)
36✔
UNCOV
252
            ->in($inValues);
36✔
253
    }
254

255
    /**
256
     * Get equality match value according to the strategy.
257
     *
258
     * @throws InvalidArgumentException If strategy does not exist
259
     */
260
    private function getEqualityMatchStrategyValue(string $strategy, string $field, mixed $value, bool $caseSensitive, ClassMetadata $metadata): mixed
261
    {
UNCOV
262
        $type = $metadata->getTypeOfField($field);
36✔
263

UNCOV
264
        if (!MongoDbType::hasType($type)) {
36✔
UNCOV
265
            return $value;
3✔
266
        }
UNCOV
267
        if (MongoDbType::STRING !== $type) {
33✔
UNCOV
268
            return MongoDbType::getType($type)->convertToDatabaseValue($value);
8✔
269
        }
270

UNCOV
271
        $quotedValue = preg_quote($value);
26✔
272

UNCOV
273
        return match ($strategy) {
UNCOV
274
            self::STRATEGY_EXACT => $caseSensitive ? $value : new Regex("^$quotedValue$", 'i'),
26✔
UNCOV
275
            self::STRATEGY_PARTIAL => new Regex($quotedValue, $caseSensitive ? '' : 'i'),
24✔
UNCOV
276
            self::STRATEGY_START => new Regex("^$quotedValue", $caseSensitive ? '' : 'i'),
5✔
UNCOV
277
            self::STRATEGY_END => new Regex("$quotedValue$", $caseSensitive ? '' : 'i'),
2✔
UNCOV
278
            self::STRATEGY_WORD_START => new Regex("(^$quotedValue.*|.*\s$quotedValue.*)", $caseSensitive ? '' : 'i'),
2✔
UNCOV
279
            default => throw new InvalidArgumentException(\sprintf('strategy %s does not exist.', $strategy)),
26✔
UNCOV
280
        };
281
    }
282

283
    /**
284
     * {@inheritdoc}
285
     */
286
    protected function getType(string $doctrineType): string
287
    {
UNCOV
288
        return match ($doctrineType) {
UNCOV
289
            MongoDbType::INT, MongoDbType::INTEGER => 'int',
232✔
UNCOV
290
            MongoDbType::BOOL, MongoDbType::BOOLEAN => 'bool',
232✔
UNCOV
291
            MongoDbType::DATE, MongoDbType::DATE_IMMUTABLE => \DateTimeInterface::class,
232✔
UNCOV
292
            MongoDbType::FLOAT => 'float',
227✔
UNCOV
293
            default => 'string',
232✔
UNCOV
294
        };
295
    }
296
}
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