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