|
18 | 18 | use ApiPlatform\Doctrine\Odm\State\Options; |
19 | 19 | use ApiPlatform\Metadata\CollectionOperationInterface; |
20 | 20 | use ApiPlatform\Metadata\DeleteOperationInterface; |
| 21 | +use ApiPlatform\Metadata\HttpOperation; |
| 22 | +use ApiPlatform\Metadata\Link; |
21 | 23 | use ApiPlatform\Metadata\Operation; |
22 | 24 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
23 | 25 | use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
24 | 26 | use ApiPlatform\State\Util\StateOptionsTrait; |
25 | 27 | use Doctrine\ODM\MongoDB\DocumentManager; |
| 28 | +use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
26 | 29 | use Doctrine\Persistence\ManagerRegistry; |
27 | 30 |
|
28 | 31 | final class DoctrineMongoDbOdmResourceCollectionMetadataFactory implements ResourceMetadataCollectionFactoryInterface |
@@ -92,9 +95,61 @@ private function addDefaults(Operation $operation): Operation |
92 | 95 | $operation = $operation->withProcessor($this->getProcessor($operation)); |
93 | 96 | } |
94 | 97 |
|
| 98 | + if ($operation instanceof HttpOperation) { |
| 99 | + $operation = $operation->withRequirements($this->getRequirements($operation)); |
| 100 | + } |
| 101 | + |
95 | 102 | return $operation; |
96 | 103 | } |
97 | 104 |
|
| 105 | + /** |
| 106 | + * @return array<string, string> |
| 107 | + */ |
| 108 | + private function getRequirements(HttpOperation $operation): array |
| 109 | + { |
| 110 | + $requirements = $operation->getRequirements() ?? []; |
| 111 | + $uriVariables = (array) ($operation->getUriVariables() ?? []); |
| 112 | + |
| 113 | + foreach ($uriVariables as $paramName => $uriVariable) { |
| 114 | + if (isset($requirements[$paramName])) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + if (!$uriVariable instanceof Link) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + $identifiers = $uriVariable->getIdentifiers(); |
| 123 | + if (1 !== \count($identifiers)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + $fieldName = $identifiers[0]; |
| 127 | + |
| 128 | + $fromClass = $uriVariable->getFromClass(); |
| 129 | + if (null === $fromClass) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + $classMetadata = $this->managerRegistry->getManagerForClass($fromClass)?->getClassMetadata($fromClass); |
| 133 | + |
| 134 | + $requirement = null; |
| 135 | + if ($classMetadata instanceof ClassMetadata && $classMetadata->hasField($fieldName)) { |
| 136 | + $fieldMapping = $classMetadata->getFieldMapping($fieldName); |
| 137 | + $requirement = match ($fieldMapping['type']) { |
| 138 | + 'uuid', 'guid' => '^[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$', |
| 139 | + 'ulid' => '^[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}$', |
| 140 | + 'smallint', 'integer', 'bigint' => '^-?[0-9]+$', |
| 141 | + default => null, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + if (null !== $requirement) { |
| 146 | + $requirements[$paramName] = $requirement; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return $requirements; |
| 151 | + } |
| 152 | + |
98 | 153 | private function getProvider(Operation $operation): string |
99 | 154 | { |
100 | 155 | if ($operation instanceof CollectionOperationInterface) { |
|
0 commit comments