Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
12 / 14 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DateValueObjectSchemaProvider | |
85.71% |
12 / 14 |
|
33.33% |
1 / 3 |
4.05 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addDisplaySchemaFor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addCreationSchemaFor | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\Core\RegexUtils; |
| 5 | use Apie\Core\ValueObjects\Interfaces\TimeRelatedValueObjectInterface; |
| 6 | use Apie\DateformatToRegex\DateFormatToRegex; |
| 7 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 8 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 9 | use cebe\openapi\spec\Components; |
| 10 | use cebe\openapi\spec\Schema; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | /** |
| 14 | * @implements SchemaProvider<TimeRelatedValueObjectInterface> |
| 15 | */ |
| 16 | class DateValueObjectSchemaProvider implements SchemaProvider |
| 17 | { |
| 18 | public function supports(ReflectionClass $class): bool |
| 19 | { |
| 20 | return $class->implementsInterface(TimeRelatedValueObjectInterface::class); |
| 21 | } |
| 22 | |
| 23 | public function addDisplaySchemaFor( |
| 24 | ComponentsBuilder $componentsBuilder, |
| 25 | string $componentIdentifier, |
| 26 | ReflectionClass $class, |
| 27 | bool $nullable = false |
| 28 | ): Components { |
| 29 | return $this->addCreationSchemaFor($componentsBuilder, $componentIdentifier, $class, $nullable); |
| 30 | } |
| 31 | |
| 32 | public function addCreationSchemaFor( |
| 33 | ComponentsBuilder $componentsBuilder, |
| 34 | string $componentIdentifier, |
| 35 | ReflectionClass $class, |
| 36 | bool $nullable = false |
| 37 | ): Components { |
| 38 | $className = $class->name; |
| 39 | $schema = new Schema([ |
| 40 | 'type' => 'string', |
| 41 | 'pattern' => RegexUtils::removeDelimiters( |
| 42 | DateFormatToRegex::formatToRegex($className::getDateFormat()) |
| 43 | ) |
| 44 | ]); |
| 45 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
| 46 | if ($nullable) { |
| 47 | $schema->nullable = true; |
| 48 | } |
| 49 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 50 | |
| 51 | return $componentsBuilder->getComponents(); |
| 52 | } |
| 53 | } |