Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
9 / 11 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ValueObjectSchemaProvider | |
81.82% |
9 / 11 |
|
75.00% |
3 / 4 |
6.22 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| addDisplaySchemaFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addCreationSchemaFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSchema | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
2.06 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\Core\RegexUtils; |
| 5 | use Apie\Core\ValueObjects\CompositeValueObject; |
| 6 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 7 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 8 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 9 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 10 | use cebe\openapi\spec\Components; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | /** |
| 14 | * Gets schema data from the toNative() return type hint. |
| 15 | * @implements SchemaProvider<ValueObjectInterface> |
| 16 | */ |
| 17 | class ValueObjectSchemaProvider implements SchemaProvider |
| 18 | { |
| 19 | public function supports(ReflectionClass $class): bool |
| 20 | { |
| 21 | return $class->implementsInterface(ValueObjectInterface::class) && !in_array(CompositeValueObject::class, $class->getTraitNames()); |
| 22 | } |
| 23 | |
| 24 | public function addDisplaySchemaFor( |
| 25 | ComponentsBuilder $componentsBuilder, |
| 26 | string $componentIdentifier, |
| 27 | ReflectionClass $class, |
| 28 | bool $nullable = false |
| 29 | ): Components { |
| 30 | return $this->getSchema($componentsBuilder, $componentIdentifier, $class, true, $nullable); |
| 31 | } |
| 32 | |
| 33 | public function addCreationSchemaFor( |
| 34 | ComponentsBuilder $componentsBuilder, |
| 35 | string $componentIdentifier, |
| 36 | ReflectionClass $class, |
| 37 | bool $nullable = false |
| 38 | ): Components { |
| 39 | return $this->getSchema($componentsBuilder, $componentIdentifier, $class, false, $nullable); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param ReflectionClass<ValueObjectInterface> $class |
| 44 | */ |
| 45 | private function getSchema( |
| 46 | ComponentsBuilder $componentsBuilder, |
| 47 | string $componentIdentifier, |
| 48 | ReflectionClass $class, |
| 49 | bool $display, |
| 50 | bool $nullable, |
| 51 | ): Components { |
| 52 | $type = $class->getMethod('toNative')->getReturnType(); |
| 53 | $schema = $componentsBuilder->getSchemaForType($type, false, $display, $nullable); |
| 54 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
| 55 | |
| 56 | if ($class->implementsInterface(HasRegexValueObjectInterface::class)) { |
| 57 | $className = $class->name; |
| 58 | $schema->pattern = RegexUtils::removeDelimiters($className::getRegularExpression()); |
| 59 | } |
| 60 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 61 | |
| 62 | return $componentsBuilder->getComponents(); |
| 63 | } |
| 64 | } |