|
| 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\Laravel\Metadata; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Parameter; |
| 17 | +use ApiPlatform\Metadata\Parameters; |
| 18 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 19 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 20 | +use Illuminate\Validation\Rule; |
| 21 | +use Psr\Container\ContainerInterface; |
| 22 | + |
| 23 | +final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, |
| 27 | + private readonly ?ContainerInterface $filterLocator = null, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function create(string $resourceClass): ResourceMetadataCollection |
| 32 | + { |
| 33 | + $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass); |
| 34 | + |
| 35 | + foreach ($resourceMetadataCollection as $i => $resource) { |
| 36 | + $operations = $resource->getOperations(); |
| 37 | + |
| 38 | + foreach ($operations as $operationName => $operation) { |
| 39 | + $parameters = $operation->getParameters() ?? new Parameters(); |
| 40 | + foreach ($parameters as $key => $parameter) { |
| 41 | + $parameters->add($key, $this->addSchemaValidation($parameter)); |
| 42 | + } |
| 43 | + |
| 44 | + if (\count($parameters) > 0) { |
| 45 | + $operations->add($operationName, $operation->withParameters($parameters)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort()); |
| 50 | + |
| 51 | + if (!$graphQlOperations = $resource->getGraphQlOperations()) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + foreach ($graphQlOperations as $operationName => $operation) { |
| 56 | + $parameters = $operation->getParameters() ?? new Parameters(); |
| 57 | + foreach ($operation->getParameters() ?? [] as $key => $parameter) { |
| 58 | + $parameters->add($key, $this->addSchemaValidation($parameter)); |
| 59 | + } |
| 60 | + |
| 61 | + if (\count($parameters) > 0) { |
| 62 | + $graphQlOperations[$operationName] = $operation->withParameters($parameters); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations); |
| 67 | + } |
| 68 | + |
| 69 | + return $resourceMetadataCollection; |
| 70 | + } |
| 71 | + |
| 72 | + private function addSchemaValidation(Parameter $parameter): Parameter |
| 73 | + { |
| 74 | + $schema = $parameter->getSchema(); |
| 75 | + $required = $parameter->getRequired(); |
| 76 | + $openApi = $parameter->getOpenApi(); |
| 77 | + |
| 78 | + // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter, |
| 79 | + // only getAllowEmptyValue is used here anyways |
| 80 | + if (\is_array($openApi)) { |
| 81 | + $openApi = $openApi[0]; |
| 82 | + } |
| 83 | + $assertions = []; |
| 84 | + $allowEmptyValue = $openApi?->getAllowEmptyValue(); |
| 85 | + if ($required || (false === $required && false === $allowEmptyValue)) { |
| 86 | + $assertions[] = 'required'; |
| 87 | + } |
| 88 | + |
| 89 | + if (true === $allowEmptyValue) { |
| 90 | + $assertions[] = 'nullable'; |
| 91 | + } |
| 92 | + |
| 93 | + if (isset($schema['exclusiveMinimum'])) { |
| 94 | + $assertions[] = 'gt:'.$schema['exclusiveMinimum']; |
| 95 | + } |
| 96 | + |
| 97 | + if (isset($schema['exclusiveMaximum'])) { |
| 98 | + $assertions[] = 'lt:'.$schema['exclusiveMaximum']; |
| 99 | + } |
| 100 | + |
| 101 | + if (isset($schema['minimum'])) { |
| 102 | + $assertions[] = 'gte:'.$schema['minimum']; |
| 103 | + } |
| 104 | + |
| 105 | + if (isset($schema['maximum'])) { |
| 106 | + $assertions[] = 'lte:'.$schema['maximum']; |
| 107 | + } |
| 108 | + |
| 109 | + if (isset($schema['pattern'])) { |
| 110 | + $assertions[] = 'regex:'.$schema['pattern']; |
| 111 | + } |
| 112 | + |
| 113 | + $minLength = isset($schema['minLength']); |
| 114 | + $maxLength = isset($schema['maxLength']); |
| 115 | + |
| 116 | + if ($minLength && $maxLength) { |
| 117 | + $assertions[] = \sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']); |
| 118 | + } elseif ($minLength) { |
| 119 | + $assertions[] = 'min:'.$schema['minLength']; |
| 120 | + } elseif ($maxLength) { |
| 121 | + $assertions[] = 'max:'.$schema['maxLength']; |
| 122 | + } |
| 123 | + |
| 124 | + $minItems = isset($schema['minItems']); |
| 125 | + $maxItems = isset($schema['maxItems']); |
| 126 | + |
| 127 | + if ($minItems && $maxItems) { |
| 128 | + $assertions[] = \sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']); |
| 129 | + } elseif ($minItems) { |
| 130 | + $assertions[] = 'min:'.$schema['minItems']; |
| 131 | + } elseif ($maxItems) { |
| 132 | + $assertions[] = 'max:'.$schema['maxItems']; |
| 133 | + } |
| 134 | + |
| 135 | + if (isset($schema['multipleOf'])) { |
| 136 | + $assertions[] = 'multiple_of:'.$schema['multipleOf']; |
| 137 | + } |
| 138 | + |
| 139 | + // if (isset($schema['enum'])) { |
| 140 | + // $assertions[] = [Rule::enum($schema['enum'])]; |
| 141 | + // } |
| 142 | + |
| 143 | + if (isset($schema['type']) && 'array' === $schema['type']) { |
| 144 | + $assertions[] = 'array'; |
| 145 | + } |
| 146 | + |
| 147 | + if (!$assertions) { |
| 148 | + return $parameter; |
| 149 | + } |
| 150 | + |
| 151 | + if (1 === \count($assertions)) { |
| 152 | + return $parameter->withConstraints($assertions[0]); |
| 153 | + } |
| 154 | + |
| 155 | + return $parameter->withConstraints($assertions); |
| 156 | + } |
| 157 | +} |
0 commit comments