|
| 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\Validator\Util; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Parameter; |
| 17 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 18 | +use Symfony\Component\TypeInfo\Type\CollectionType; |
| 19 | +use Symfony\Component\TypeInfo\Type\UnionType; |
| 20 | +use Symfony\Component\Validator\Constraint; |
| 21 | +use Symfony\Component\Validator\Constraints\All; |
| 22 | +use Symfony\Component\Validator\Constraints\AtLeastOneOf; |
| 23 | +use Symfony\Component\Validator\Constraints\Choice; |
| 24 | +use Symfony\Component\Validator\Constraints\Collection; |
| 25 | +use Symfony\Component\Validator\Constraints\Count; |
| 26 | +use Symfony\Component\Validator\Constraints\DivisibleBy; |
| 27 | +use Symfony\Component\Validator\Constraints\GreaterThan; |
| 28 | +use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; |
| 29 | +use Symfony\Component\Validator\Constraints\Length; |
| 30 | +use Symfony\Component\Validator\Constraints\LessThan; |
| 31 | +use Symfony\Component\Validator\Constraints\LessThanOrEqual; |
| 32 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 33 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 34 | +use Symfony\Component\Validator\Constraints\Range; |
| 35 | +use Symfony\Component\Validator\Constraints\Regex; |
| 36 | +use Symfony\Component\Validator\Constraints\Sequentially; |
| 37 | +use Symfony\Component\Validator\Constraints\Type; |
| 38 | +use Symfony\Component\Validator\Constraints\Unique; |
| 39 | + |
| 40 | +/** |
| 41 | + * Helper to get a set of validation constraints for a given Parameter. |
| 42 | + * |
| 43 | + * @experimental |
| 44 | + */ |
| 45 | +trait ParameterValidationConstraints |
| 46 | +{ |
| 47 | + /** |
| 48 | + * @param Parameter $parameter readonly |
| 49 | + * |
| 50 | + * @return list<Constraint> |
| 51 | + */ |
| 52 | + public static function getParameterValidationConstraints(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): array |
| 53 | + { |
| 54 | + $schema ??= $parameter->getSchema(); |
| 55 | + $required ??= $parameter->getRequired() ?? false; |
| 56 | + $openApi ??= $parameter->getOpenApi(); |
| 57 | + |
| 58 | + // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter, |
| 59 | + // only getAllowEmptyValue is used here anyways |
| 60 | + if (\is_array($openApi)) { |
| 61 | + $openApi = $openApi[0]; |
| 62 | + } elseif (false === $openApi) { |
| 63 | + $openApi = null; |
| 64 | + } |
| 65 | + |
| 66 | + $assertions = []; |
| 67 | + $allowEmptyValue = $openApi?->getAllowEmptyValue(); |
| 68 | + if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) { |
| 69 | + $assertions[] = new NotBlank(allowNull: !$required); |
| 70 | + } |
| 71 | + |
| 72 | + $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null; |
| 73 | + $exclusiveMinimum = isset($schema['exclusiveMinimum']); |
| 74 | + $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null; |
| 75 | + $exclusiveMaximum = isset($schema['exclusiveMaximum']); |
| 76 | + |
| 77 | + if ($minimum && $maximum) { |
| 78 | + if (!$exclusiveMinimum && !$exclusiveMaximum) { |
| 79 | + $assertions[] = new Range(min: $minimum, max: $maximum); |
| 80 | + } else { |
| 81 | + $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum); |
| 82 | + $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum); |
| 83 | + } |
| 84 | + } elseif ($minimum) { |
| 85 | + $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum); |
| 86 | + } elseif ($maximum) { |
| 87 | + $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum); |
| 88 | + } |
| 89 | + |
| 90 | + if (isset($schema['pattern'])) { |
| 91 | + $assertions[] = new Regex('#'.$schema['pattern'].'#'); |
| 92 | + } |
| 93 | + |
| 94 | + if (isset($schema['maxLength']) || isset($schema['minLength'])) { |
| 95 | + $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null); |
| 96 | + } |
| 97 | + |
| 98 | + if (isset($schema['multipleOf'])) { |
| 99 | + $assertions[] = new DivisibleBy(value: $schema['multipleOf']); |
| 100 | + } |
| 101 | + |
| 102 | + if (isset($schema['enum'])) { |
| 103 | + $assertions[] = new Choice(choices: $schema['enum']); |
| 104 | + } |
| 105 | + |
| 106 | + if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) { |
| 107 | + $fields = []; |
| 108 | + foreach ($properties as $propertyName) { |
| 109 | + $fields[$propertyName] = $assertions; |
| 110 | + } |
| 111 | + |
| 112 | + return [new Collection(fields: $fields, allowMissingFields: true)]; |
| 113 | + } |
| 114 | + |
| 115 | + $isCollectionType = fn ($t) => $t instanceof CollectionType; |
| 116 | + $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false; |
| 117 | + |
| 118 | + // type-info 7.2 |
| 119 | + if (!$isCollection && $parameter->getNativeType() instanceof UnionType) { |
| 120 | + foreach ($parameter->getNativeType()->getTypes() as $t) { |
| 121 | + if ($isCollection = $t->isSatisfiedBy($isCollectionType)) { |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if ($isCollection) { |
| 128 | + if (true === ($parameter->getCastToArray() ?? false)) { |
| 129 | + $assertions = $assertions ? [new All($assertions)] : []; |
| 130 | + } else { |
| 131 | + $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : []; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if ($required && false !== $allowEmptyValue) { |
| 136 | + $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey())); |
| 137 | + } |
| 138 | + |
| 139 | + if (isset($schema['minItems']) || isset($schema['maxItems'])) { |
| 140 | + $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null); |
| 141 | + } |
| 142 | + |
| 143 | + if ($schema['uniqueItems'] ?? false) { |
| 144 | + $assertions[] = new Unique(); |
| 145 | + } |
| 146 | + |
| 147 | + if (isset($schema['type']) && 'array' === $schema['type']) { |
| 148 | + $assertions[] = new Type(type: 'array'); |
| 149 | + } |
| 150 | + |
| 151 | + return $assertions; |
| 152 | + } |
| 153 | +} |
0 commit comments