Skip to content

Commit 0d79b90

Browse files
committed
feat(doctrine): add ODM PartialSearchFilter
Continues the work at api-platform#7079 and before at api-platform#6865
1 parent 205141e commit 0d79b90

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Metadata\OpenApiParameterFilterInterface;
17+
use ApiPlatform\Metadata\Operation;
18+
use ApiPlatform\Metadata\Parameter;
19+
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
20+
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21+
use MongoDB\BSON\Regex;
22+
23+
final class PartialSearchFilter implements FilterInterface, OpenApiParameterFilterInterface
24+
{
25+
public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
26+
{
27+
if (!$parameter = $context['parameter'] ?? null) {
28+
return;
29+
}
30+
31+
\assert($parameter instanceof Parameter);
32+
33+
$value = $parameter->getValue();
34+
if (!\is_string($value) || '' === $value) {
35+
return;
36+
}
37+
38+
$matchField = $parameter->getProperty();
39+
$escapedValue = preg_quote($value, '/');
40+
41+
$aggregationBuilder
42+
->match()
43+
->field($matchField)
44+
->equals(new Regex($escapedValue, 'i'));
45+
}
46+
47+
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
48+
{
49+
return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true);
50+
}
51+
52+
public function getDescription(string $resourceClass): array
53+
{
54+
return [];
55+
}
56+
}

0 commit comments

Comments
 (0)