Skip to content

Commit 2479f1c

Browse files
committed
feat: replace GeneratorConfig with modular PropertyDataExtractor system
- Introduce PropertyDataExtractorInterface for validation constraint extraction - Add CompositePropertyDataExtractor with fluent API for extractor composition - Refactor AttributeConstraintExtractor and ValidationConstraintExtractor to implement new interface
1 parent 5a4cea6 commit 2479f1c

9 files changed

+247
-156
lines changed

src/Generator.php

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414
use Spiral\JsonSchemaGenerator\Schema\Definition;
1515
use Spiral\JsonSchemaGenerator\Schema\Property;
1616
use Spiral\JsonSchemaGenerator\Schema\PropertyType;
17-
use Spiral\JsonSchemaGenerator\Validation\ValidationConstraintExtractor;
18-
use Spiral\JsonSchemaGenerator\Validation\AttributeConstraintExtractor;
17+
use Spiral\JsonSchemaGenerator\Validation\CompositePropertyDataExtractor;
18+
use Spiral\JsonSchemaGenerator\Validation\PropertyDataExtractorInterface;
1919

2020
final class Generator implements GeneratorInterface
2121
{
2222
protected array $cache = [];
23-
private readonly ValidationConstraintExtractor $validationExtractor;
24-
private readonly AttributeConstraintExtractor $attributeExtractor;
23+
protected readonly PropertyDataExtractorInterface $propertyDataExtractor;
2524

2625
public function __construct(
2726
protected readonly ParserInterface $parser = new Parser(),
28-
?ValidationConstraintExtractor $validationExtractor = null,
29-
protected readonly GeneratorConfig $config = new GeneratorConfig(),
30-
?AttributeConstraintExtractor $attributeExtractor = null,
27+
?PropertyDataExtractorInterface $propertyDataExtractor = null,
3128
) {
32-
$this->validationExtractor = $validationExtractor ?? new ValidationConstraintExtractor();
33-
$this->attributeExtractor = $attributeExtractor ?? new AttributeConstraintExtractor();
29+
// If no extractor is provided, use the default composite with all extractors
30+
$this->propertyDataExtractor = $propertyDataExtractor ?? CompositePropertyDataExtractor::createDefault();
3431
}
3532

3633
/**
@@ -132,15 +129,8 @@ protected function generateProperty(PropertyInterface $property): ?Property
132129
$type = $property->getType();
133130
$propertyTypes = $this->extractPropertyTypes($type);
134131

135-
// Extract validation constraints from PHPDoc (if enabled)
136-
$validationRules = [];
137-
if ($this->config->enableValidationConstraints) {
138-
$validationRules = $this->extractValidationConstraints($property, $propertyTypes);
139-
$validationRules = \array_merge(
140-
$validationRules,
141-
$this->extractAttributeConstraints($property, $propertyTypes),
142-
);
143-
}
132+
// Extract validation constraints using the configurable extractor system
133+
$validationRules = $this->extractValidationConstraints($property, $propertyTypes);
144134

145135
return new Property(
146136
types: $propertyTypes,
@@ -172,32 +162,15 @@ enum: $collectionSimpleType->getEnumValues(),
172162
}
173163

174164
/**
175-
* Extract validation constraints from property PHPDoc
165+
* Extract validation constraints from property using the configured extractors.
176166
*/
177167
private function extractValidationConstraints(PropertyInterface $property, array $propertyTypes): array
178168
{
179169
$allValidationRules = [];
180170

181171
foreach ($propertyTypes as $propertyType) {
182172
if ($propertyType->type instanceof Schema\Type) {
183-
$validationRules = $this->validationExtractor->extractValidationRules($property, $propertyType->type);
184-
$allValidationRules = \array_merge($allValidationRules, $validationRules);
185-
}
186-
}
187-
188-
return $allValidationRules;
189-
}
190-
191-
/**
192-
* Extract validation constraints from property attributes
193-
*/
194-
private function extractAttributeConstraints(PropertyInterface $property, array $propertyTypes): array
195-
{
196-
$allValidationRules = [];
197-
198-
foreach ($propertyTypes as $propertyType) {
199-
if ($propertyType->type instanceof Schema\Type) {
200-
$validationRules = $this->attributeExtractor->extractValidationRules($property, $propertyType->type);
173+
$validationRules = $this->propertyDataExtractor->extractValidationRules($property, $propertyType->type);
201174
$allValidationRules = \array_merge($allValidationRules, $validationRules);
202175
}
203176
}

src/GeneratorConfig.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Validation/AttributeConstraintExtractor.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
use Spiral\JsonSchemaGenerator\Parser\PropertyInterface;
1414
use Spiral\JsonSchemaGenerator\Schema\Type;
1515

16-
/**
17-
* @internal
18-
*/
19-
final readonly class AttributeConstraintExtractor
16+
final readonly class AttributeConstraintExtractor implements PropertyDataExtractorInterface
2017
{
2118
public function extractValidationRules(PropertyInterface $property, Type $jsonSchemaType): array
2219
{
@@ -84,7 +81,6 @@ public function extractValidationRules(PropertyInterface $property, Type $jsonSc
8481
private function getLengthMinKey(Type $jsonSchemaType): string
8582
{
8683
return match ($jsonSchemaType) {
87-
Type::String => 'minLength',
8884
Type::Array => 'minItems',
8985
default => 'minLength', // fallback
9086
};
@@ -93,7 +89,6 @@ private function getLengthMinKey(Type $jsonSchemaType): string
9389
private function getLengthMaxKey(Type $jsonSchemaType): string
9490
{
9591
return match ($jsonSchemaType) {
96-
Type::String => 'maxLength',
9792
Type::Array => 'maxItems',
9893
default => 'maxLength', // fallback
9994
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\JsonSchemaGenerator\Validation;
6+
7+
use Spiral\JsonSchemaGenerator\Parser\PropertyInterface;
8+
use Spiral\JsonSchemaGenerator\Schema\Type;
9+
10+
final readonly class CompositePropertyDataExtractor implements PropertyDataExtractorInterface
11+
{
12+
/**
13+
* @param list<PropertyDataExtractorInterface> $extractors
14+
*/
15+
public function __construct(
16+
private array $extractors = [],
17+
) {}
18+
19+
/**
20+
* Create a default instance with commonly used extractors.
21+
*/
22+
public static function createDefault(): self
23+
{
24+
return new self([
25+
new ValidationConstraintExtractor(),
26+
new AttributeConstraintExtractor(),
27+
]);
28+
}
29+
30+
/**
31+
* Add an extractor to the composite.
32+
*/
33+
public function withExtractor(PropertyDataExtractorInterface $extractor): self
34+
{
35+
return new self([...$this->extractors, $extractor]);
36+
}
37+
38+
public function extractValidationRules(PropertyInterface $property, Type $jsonSchemaType): array
39+
{
40+
$allValidationRules = [];
41+
42+
foreach ($this->extractors as $extractor) {
43+
$validationRules = $extractor->extractValidationRules($property, $jsonSchemaType);
44+
$allValidationRules = \array_merge($allValidationRules, $validationRules);
45+
}
46+
47+
return $allValidationRules;
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\JsonSchemaGenerator\Validation;
6+
7+
use Spiral\JsonSchemaGenerator\Parser\PropertyInterface;
8+
use Spiral\JsonSchemaGenerator\Schema\Type;
9+
10+
interface PropertyDataExtractorInterface
11+
{
12+
/**
13+
* Extract validation rules from a property for a specific JSON schema type.
14+
*
15+
* @param PropertyInterface $property The property to extract rules from
16+
* @param Type $jsonSchemaType The JSON schema type to extract rules for
17+
* @return array<string, mixed> Array of validation rules
18+
*/
19+
public function extractValidationRules(PropertyInterface $property, Type $jsonSchemaType): array;
20+
}

src/Validation/ValidationConstraintExtractor.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use Spiral\JsonSchemaGenerator\Parser\PropertyInterface;
88
use Spiral\JsonSchemaGenerator\Schema\Type;
99

10-
/**
11-
* @internal
12-
*/
13-
final readonly class ValidationConstraintExtractor
10+
final readonly class ValidationConstraintExtractor implements PropertyDataExtractorInterface
1411
{
1512
public function __construct(
1613
private DocBlockParser $docBlockParser = new DocBlockParser(),

tests/Unit/GeneratorConfigTest.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)