|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Doctrine\Odm\Filter;
|
15 | 15 |
|
| 16 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; |
16 | 18 | use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait;
|
17 | 19 | use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
|
18 | 20 | use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
|
19 | 21 | use ApiPlatform\Metadata\Operation;
|
20 | 22 | use Doctrine\ODM\MongoDB\Aggregation\Builder;
|
| 23 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 24 | +use Doctrine\ODM\MongoDB\LockException; |
| 25 | +use Doctrine\ODM\MongoDB\Mapping\MappingException; |
21 | 26 |
|
22 | 27 | /**
|
23 | 28 | * @author Vincent Amstoutz <[email protected]>
|
24 | 29 | */
|
25 |
| -final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterface |
| 30 | +final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterface, ManagerRegistryAwareInterface |
26 | 31 | {
|
27 | 32 | use BackwardCompatibleFilterDescriptionTrait;
|
| 33 | + use ManagerRegistryAwareTrait; |
28 | 34 | use OpenApiFilterTrait;
|
29 | 35 |
|
| 36 | + /** |
| 37 | + * @throws MappingException |
| 38 | + * @throws LockException |
| 39 | + */ |
30 | 40 | public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
|
31 | 41 | {
|
32 | 42 | $parameter = $context['parameter'];
|
33 | 43 | $property = $parameter->getProperty();
|
34 |
| - |
35 | 44 | $value = $parameter->getValue();
|
36 |
| - if (is_numeric($value)) { |
37 |
| - $property .= '.id'; |
| 45 | + |
| 46 | + $documentManager = $this->getManagerRegistry()?->getManagerForClass($resourceClass); |
| 47 | + if (!$documentManager instanceof DocumentManager) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $classMetadata = $documentManager->getClassMetadata($resourceClass); |
| 52 | + |
| 53 | + if (!$classMetadata->hasReference($property)) { |
| 54 | + $aggregationBuilder |
| 55 | + ->match() |
| 56 | + ->field($property) |
| 57 | + ->{is_iterable($value) ? 'in' : 'equals'}($value); |
| 58 | + |
| 59 | + return; |
38 | 60 | }
|
39 | 61 |
|
| 62 | + $mapping = $classMetadata->getFieldMapping($property); |
| 63 | + $targetDocument = $mapping['targetDocument']; |
| 64 | + $repository = $documentManager->getRepository($targetDocument); |
| 65 | + $method = $classMetadata->isCollectionValuedAssociation($property) ? 'includesReferenceTo' : 'references'; |
| 66 | + |
| 67 | + if (is_iterable($value)) { |
| 68 | + $documents = []; |
| 69 | + foreach ($value as $v) { |
| 70 | + if ($doc = $repository->find($v)) { |
| 71 | + $documents[] = $doc; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $match = $aggregationBuilder->match(); |
| 76 | + $or = $match->expr(); |
| 77 | + foreach ($documents as $doc) { |
| 78 | + $or->addOr($match->expr()->field($property)->{$method}($doc)); |
| 79 | + } |
| 80 | + $match->addAnd($or); |
| 81 | + |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $referencedDoc = $repository->find($value); |
| 86 | + |
40 | 87 | $aggregationBuilder
|
41 | 88 | ->match()
|
42 | 89 | ->field($property)
|
43 |
| - ->{(is_iterable($value)) ? 'in' : 'equals'}($value); |
| 90 | + ->{$method}($referencedDoc); |
44 | 91 | }
|
45 | 92 | }
|
0 commit comments