|
| 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\Core\Bridge\Doctrine\MongoDbOdm\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Api\IdentifiersExtractorInterface; |
| 17 | +use ApiPlatform\Core\Api\IriConverterInterface; |
| 18 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
| 19 | +use ApiPlatform\Doctrine\Common\Filter\SearchFilterInterface; |
| 20 | +use ApiPlatform\Doctrine\Common\Filter\SearchFilterTrait; |
| 21 | +use ApiPlatform\Doctrine\Odm\Filter\AbstractFilter; |
| 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 | + * Filter the collection by given properties. |
| 35 | + * |
| 36 | + * @experimental |
| 37 | + * |
| 38 | + * @author Kévin Dunglas <[email protected]> |
| 39 | + * @author Alan Poulain <[email protected]> |
| 40 | + */ |
| 41 | +final class SearchFilter extends AbstractFilter implements SearchFilterInterface |
| 42 | +{ |
| 43 | + use SearchFilterTrait; |
| 44 | + |
| 45 | + public const DOCTRINE_INTEGER_TYPE = [MongoDbType::INTEGER, MongoDbType::INT]; |
| 46 | + |
| 47 | + public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface $iriConverter, IdentifiersExtractorInterface $identifiersExtractor, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null) |
| 48 | + { |
| 49 | + parent::__construct($managerRegistry, $logger, $properties, $nameConverter); |
| 50 | + |
| 51 | + $this->iriConverter = $iriConverter; |
| 52 | + $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
| 53 | + $this->identifiersExtractor = $identifiersExtractor; |
| 54 | + } |
| 55 | + |
| 56 | + protected function getIriConverter(): IriConverterInterface |
| 57 | + { |
| 58 | + return $this->iriConverter; |
| 59 | + } |
| 60 | + |
| 61 | + protected function getPropertyAccessor(): PropertyAccessorInterface |
| 62 | + { |
| 63 | + return $this->propertyAccessor; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | + protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = []) |
| 70 | + { |
| 71 | + if ( |
| 72 | + null === $value || |
| 73 | + !$this->isPropertyEnabled($property, $resourceClass) || |
| 74 | + !$this->isPropertyMapped($property, $resourceClass, true) |
| 75 | + ) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $matchField = $field = $property; |
| 80 | + |
| 81 | + $values = $this->normalizeValues((array) $value, $property); |
| 82 | + if (null === $values) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $associations = []; |
| 87 | + if ($this->isPropertyNested($property, $resourceClass)) { |
| 88 | + [$matchField, $field, $associations] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass); |
| 89 | + } |
| 90 | + |
| 91 | + $caseSensitive = true; |
| 92 | + $strategy = $this->properties[$property] ?? self::STRATEGY_EXACT; |
| 93 | + |
| 94 | + // prefixing the strategy with i makes it case insensitive |
| 95 | + if (0 === strpos($strategy, 'i')) { |
| 96 | + $strategy = substr($strategy, 1); |
| 97 | + $caseSensitive = false; |
| 98 | + } |
| 99 | + |
| 100 | + /** @var MongoDBClassMetadata */ |
| 101 | + $metadata = $this->getNestedMetadata($resourceClass, $associations); |
| 102 | + |
| 103 | + if ($metadata->hasField($field) && !$metadata->hasAssociation($field)) { |
| 104 | + if ('id' === $field) { |
| 105 | + $values = array_map([$this, 'getIdFromValue'], $values); |
| 106 | + } |
| 107 | + |
| 108 | + if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) { |
| 109 | + $this->logger->notice('Invalid filter ignored', [ |
| 110 | + 'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)), |
| 111 | + ]); |
| 112 | + |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + $this->addEqualityMatchStrategy($strategy, $aggregationBuilder, $field, $matchField, $values, $caseSensitive, $metadata); |
| 117 | + |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + // metadata doesn't have the field, nor an association on the field |
| 122 | + if (!$metadata->hasAssociation($field)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $values = array_map([$this, 'getIdFromValue'], $values); |
| 127 | + $doctrineTypeField = $this->getDoctrineFieldType($property, $resourceClass); |
| 128 | + |
| 129 | + if (null !== $this->identifiersExtractor) { |
| 130 | + $associationResourceClass = $metadata->getAssociationTargetClass($field); |
| 131 | + $associationFieldIdentifier = $this->identifiersExtractor->getIdentifiersFromResourceClass($associationResourceClass)[0]; |
| 132 | + $doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass); |
| 133 | + } |
| 134 | + |
| 135 | + if (!$this->hasValidValues($values, $doctrineTypeField)) { |
| 136 | + $this->logger->notice('Invalid filter ignored', [ |
| 137 | + 'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $property)), |
| 138 | + ]); |
| 139 | + |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + $this->addEqualityMatchStrategy($strategy, $aggregationBuilder, $field, $matchField, $values, $caseSensitive, $metadata); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Add equality match stage according to the strategy. |
| 148 | + * |
| 149 | + * @param mixed $values |
| 150 | + */ |
| 151 | + private function addEqualityMatchStrategy(string $strategy, Builder $aggregationBuilder, string $field, string $matchField, $values, bool $caseSensitive, ClassMetadata $metadata): void |
| 152 | + { |
| 153 | + $inValues = []; |
| 154 | + foreach ($values as $inValue) { |
| 155 | + $inValues[] = $this->getEqualityMatchStrategyValue($strategy, $field, $inValue, $caseSensitive, $metadata); |
| 156 | + } |
| 157 | + |
| 158 | + $aggregationBuilder |
| 159 | + ->match() |
| 160 | + ->field($matchField) |
| 161 | + ->in($inValues); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get equality match value according to the strategy. |
| 166 | + * |
| 167 | + * @param mixed $value |
| 168 | + * |
| 169 | + * @throws InvalidArgumentException If strategy does not exist |
| 170 | + * |
| 171 | + * @return Regex|string |
| 172 | + */ |
| 173 | + private function getEqualityMatchStrategyValue(string $strategy, string $field, $value, bool $caseSensitive, ClassMetadata $metadata) |
| 174 | + { |
| 175 | + $type = $metadata->getTypeOfField($field); |
| 176 | + |
| 177 | + if (!MongoDbType::hasType($type)) { |
| 178 | + return $value; |
| 179 | + } |
| 180 | + if (MongoDbType::STRING !== $type) { |
| 181 | + return MongoDbType::getType($type)->convertToDatabaseValue($value); |
| 182 | + } |
| 183 | + |
| 184 | + $quotedValue = preg_quote($value); |
| 185 | + |
| 186 | + switch ($strategy) { |
| 187 | + case null: |
| 188 | + case self::STRATEGY_EXACT: |
| 189 | + return $caseSensitive ? $value : new Regex("^$quotedValue$", 'i'); |
| 190 | + case self::STRATEGY_PARTIAL: |
| 191 | + return new Regex($quotedValue, $caseSensitive ? '' : 'i'); |
| 192 | + case self::STRATEGY_START: |
| 193 | + return new Regex("^$quotedValue", $caseSensitive ? '' : 'i'); |
| 194 | + case self::STRATEGY_END: |
| 195 | + return new Regex("$quotedValue$", $caseSensitive ? '' : 'i'); |
| 196 | + case self::STRATEGY_WORD_START: |
| 197 | + return new Regex("(^$quotedValue.*|.*\s$quotedValue.*)", $caseSensitive ? '' : 'i'); |
| 198 | + default: |
| 199 | + throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy)); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * {@inheritdoc} |
| 205 | + */ |
| 206 | + protected function getType(string $doctrineType): string |
| 207 | + { |
| 208 | + switch ($doctrineType) { |
| 209 | + case MongoDbType::INT: |
| 210 | + case MongoDbType::INTEGER: |
| 211 | + return 'int'; |
| 212 | + case MongoDbType::BOOL: |
| 213 | + case MongoDbType::BOOLEAN: |
| 214 | + return 'bool'; |
| 215 | + case MongoDbType::DATE: |
| 216 | + return \DateTimeInterface::class; |
| 217 | + case MongoDbType::FLOAT: |
| 218 | + return 'float'; |
| 219 | + } |
| 220 | + |
| 221 | + return 'string'; |
| 222 | + } |
| 223 | +} |
0 commit comments