1414use Spiral \JsonSchemaGenerator \Schema \Definition ;
1515use Spiral \JsonSchemaGenerator \Schema \Property ;
1616use Spiral \JsonSchemaGenerator \Schema \PropertyType ;
17+ use Spiral \JsonSchemaGenerator \Validation \ValidationConstraintExtractor ;
1718
18- class Generator implements GeneratorInterface
19+ final class Generator implements GeneratorInterface
1920{
2021 protected array $ cache = [];
22+ private readonly ValidationConstraintExtractor $ validationExtractor ;
2123
2224 public function __construct (
2325 protected readonly ParserInterface $ parser = new Parser (),
24- ) {}
26+ ?ValidationConstraintExtractor $ validationExtractor = null ,
27+ protected readonly GeneratorConfig $ config = new GeneratorConfig (),
28+ ) {
29+ $ this ->validationExtractor = $ validationExtractor ?? new ValidationConstraintExtractor ();
30+ }
2531
2632 /**
2733 * @param class-string|\ReflectionClass $class
@@ -120,14 +126,22 @@ protected function generateProperty(PropertyInterface $property): ?Property
120126 }
121127
122128 $ type = $ property ->getType ();
129+ $ propertyTypes = $ this ->extractPropertyTypes ($ type );
130+
131+ // Extract validation constraints from PHPDoc (if enabled)
132+ $ validationRules = [];
133+ if ($ this ->config ->enableValidationConstraints ) {
134+ $ validationRules = $ this ->extractValidationConstraints ($ property , $ propertyTypes );
135+ }
123136
124137 return new Property (
125- types: $ this -> extractPropertyTypes ( $ type ) ,
138+ types: $ propertyTypes ,
126139 title: $ title ,
127140 description: $ description ,
128141 required: $ default === null && !$ type ->allowsNull (),
129142 default: $ default ,
130143 format: $ format ,
144+ validationRules: $ validationRules ,
131145 );
132146 }
133147
@@ -139,10 +153,30 @@ private function extractPropertyTypes(Type $type): array
139153 return \array_map (static fn (SimpleType $ simpleType ) => new PropertyType (
140154 type: $ simpleType ->getName (),
141155 enum: $ simpleType ->getEnumValues (),
142- collectionTypes: $ simpleType ->isCollection () ? \array_map (static fn (SimpleType $ collectionSimpleType ) => new PropertyType (
143- type: $ collectionSimpleType ->getName (),
144- enum: $ collectionSimpleType ->getEnumValues (),
145- ), $ simpleType ->getCollectionType ()?->types ?? []) : null ,
156+ collectionTypes: $ simpleType ->isCollection () ? \array_map (
157+ static fn (SimpleType $ collectionSimpleType ) => new PropertyType (
158+ type: $ collectionSimpleType ->getName (),
159+ enum: $ collectionSimpleType ->getEnumValues (),
160+ ),
161+ $ simpleType ->getCollectionType ()?->types ?? [],
162+ ) : null ,
146163 ), $ type ->types );
147164 }
165+
166+ /**
167+ * Extract validation constraints from property PHPDoc
168+ */
169+ private function extractValidationConstraints (PropertyInterface $ property , array $ propertyTypes ): array
170+ {
171+ $ allValidationRules = [];
172+
173+ foreach ($ propertyTypes as $ propertyType ) {
174+ if ($ propertyType ->type instanceof Schema \Type) {
175+ $ validationRules = $ this ->validationExtractor ->extractValidationRules ($ property , $ propertyType ->type );
176+ $ allValidationRules = \array_merge ($ allValidationRules , $ validationRules );
177+ }
178+ }
179+
180+ return $ allValidationRules ;
181+ }
148182}
0 commit comments