|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\ManagerRegistryAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; |
| 18 | +use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; |
| 19 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 20 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 21 | +use ApiPlatform\Metadata\Operation; |
| 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; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Vincent Amstoutz <[email protected]> |
| 29 | + */ |
| 30 | +final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterface, ManagerRegistryAwareInterface |
| 31 | +{ |
| 32 | + use BackwardCompatibleFilterDescriptionTrait; |
| 33 | + use ManagerRegistryAwareTrait; |
| 34 | + use OpenApiFilterTrait; |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws MappingException |
| 38 | + * @throws LockException |
| 39 | + */ |
| 40 | + public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 41 | + { |
| 42 | + $parameter = $context['parameter']; |
| 43 | + $property = $parameter->getProperty(); |
| 44 | + $value = $parameter->getValue(); |
| 45 | + $operator = $context['operator'] ?? 'addAnd'; |
| 46 | + $match = $context['match'] = $context['match'] ?? |
| 47 | + $aggregationBuilder |
| 48 | + ->matchExpr(); |
| 49 | + |
| 50 | + $documentManager = $this->getManagerRegistry()->getManagerForClass($resourceClass); |
| 51 | + if (!$documentManager instanceof DocumentManager) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $classMetadata = $documentManager->getClassMetadata($resourceClass); |
| 56 | + |
| 57 | + if (!$classMetadata->hasReference($property)) { |
| 58 | + $match |
| 59 | + ->{$operator}($aggregationBuilder->matchExpr()->field($property)->{is_iterable($value) ? 'in' : 'equals'}($value)); |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $mapping = $classMetadata->getFieldMapping($property); |
| 65 | + $method = $classMetadata->isSingleValuedAssociation($property) ? 'references' : 'includesReferenceTo'; |
| 66 | + |
| 67 | + if (is_iterable($value)) { |
| 68 | + $or = $aggregationBuilder->matchExpr(); |
| 69 | + |
| 70 | + foreach ($value as $v) { |
| 71 | + $or->addOr($aggregationBuilder->matchExpr()->field($property)->{$method}($documentManager->getPartialReference($mapping['targetDocument'], $v))); |
| 72 | + } |
| 73 | + |
| 74 | + $match->{$operator}($or); |
| 75 | + |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $match |
| 80 | + ->{$operator}( |
| 81 | + $aggregationBuilder->matchExpr() |
| 82 | + ->field($property) |
| 83 | + ->{$method}($documentManager->getPartialReference($mapping['targetDocument'], $value)) |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments