|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\JsonSchemaGenerator\Validation; |
| 6 | + |
| 7 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\Enum; |
| 8 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\Items; |
| 9 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\Length; |
| 10 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\MultipleOf; |
| 11 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\Pattern; |
| 12 | +use Spiral\JsonSchemaGenerator\Attribute\Constraint\Range; |
| 13 | +use Spiral\JsonSchemaGenerator\Parser\PropertyInterface; |
| 14 | +use Spiral\JsonSchemaGenerator\Schema\Type; |
| 15 | + |
| 16 | +/** |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +final readonly class AttributeConstraintExtractor |
| 20 | +{ |
| 21 | + public function extractValidationRules(PropertyInterface $property, Type $jsonSchemaType): array |
| 22 | + { |
| 23 | + $validationRules = []; |
| 24 | + |
| 25 | + // Extract Pattern constraint |
| 26 | + $pattern = $property->findAttribute(Pattern::class); |
| 27 | + if ($pattern instanceof Pattern) { |
| 28 | + $validationRules['pattern'] = $pattern->pattern; |
| 29 | + } |
| 30 | + |
| 31 | + // Extract Length constraint |
| 32 | + $length = $property->findAttribute(Length::class); |
| 33 | + if ($length instanceof Length) { |
| 34 | + if ($length->min !== null) { |
| 35 | + $validationRules[$this->getLengthMinKey($jsonSchemaType)] = $length->min; |
| 36 | + } |
| 37 | + if ($length->max !== null) { |
| 38 | + $validationRules[$this->getLengthMaxKey($jsonSchemaType)] = $length->max; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Extract Range constraint |
| 43 | + $range = $property->findAttribute(Range::class); |
| 44 | + if ($range instanceof Range) { |
| 45 | + if ($range->min !== null) { |
| 46 | + $key = $range->exclusiveMin === true ? 'exclusiveMinimum' : 'minimum'; |
| 47 | + $validationRules[$key] = $range->min; |
| 48 | + } |
| 49 | + if ($range->max !== null) { |
| 50 | + $key = $range->exclusiveMax === true ? 'exclusiveMaximum' : 'maximum'; |
| 51 | + $validationRules[$key] = $range->max; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Extract MultipleOf constraint |
| 56 | + $multipleOf = $property->findAttribute(MultipleOf::class); |
| 57 | + if ($multipleOf instanceof MultipleOf && $this->isNumericType($jsonSchemaType)) { |
| 58 | + $validationRules['multipleOf'] = $multipleOf->value; |
| 59 | + } |
| 60 | + |
| 61 | + // Extract Items constraint |
| 62 | + $items = $property->findAttribute(Items::class); |
| 63 | + if ($items instanceof Items && $jsonSchemaType === Type::Array) { |
| 64 | + if ($items->min !== null) { |
| 65 | + $validationRules['minItems'] = $items->min; |
| 66 | + } |
| 67 | + if ($items->max !== null) { |
| 68 | + $validationRules['maxItems'] = $items->max; |
| 69 | + } |
| 70 | + if ($items->unique === true) { |
| 71 | + $validationRules['uniqueItems'] = true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Extract Enum constraint |
| 76 | + $enum = $property->findAttribute(Enum::class); |
| 77 | + if ($enum instanceof Enum) { |
| 78 | + $validationRules['enum'] = $enum->values; |
| 79 | + } |
| 80 | + |
| 81 | + return $validationRules; |
| 82 | + } |
| 83 | + |
| 84 | + private function getLengthMinKey(Type $jsonSchemaType): string |
| 85 | + { |
| 86 | + return match ($jsonSchemaType) { |
| 87 | + Type::String => 'minLength', |
| 88 | + Type::Array => 'minItems', |
| 89 | + default => 'minLength', // fallback |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private function getLengthMaxKey(Type $jsonSchemaType): string |
| 94 | + { |
| 95 | + return match ($jsonSchemaType) { |
| 96 | + Type::String => 'maxLength', |
| 97 | + Type::Array => 'maxItems', |
| 98 | + default => 'maxLength', // fallback |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + private function isNumericType(Type $jsonSchemaType): bool |
| 103 | + { |
| 104 | + return $jsonSchemaType === Type::Integer || $jsonSchemaType === Type::Number; |
| 105 | + } |
| 106 | +} |
0 commit comments