Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.50% |
10 / 16 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UriSchemaProvider | |
62.50% |
10 / 16 |
|
50.00% |
2 / 4 |
7.90 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSchema | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| addDisplaySchemaFor | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| addCreationSchemaFor | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 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 ReflectionClass; |
| 9 | use Uri\Rfc3986\Uri; |
| 10 | |
| 11 | /** |
| 12 | * @implements SchemaProvider<Uri> |
| 13 | */ |
| 14 | class UriSchemaProvider implements SchemaProvider |
| 15 | { |
| 16 | public function supports(ReflectionClass $class): bool |
| 17 | { |
| 18 | return $class->name === Uri::class; |
| 19 | } |
| 20 | |
| 21 | private function createSchema(): Schema |
| 22 | { |
| 23 | // json_encode Duration returns this structure: {"seconds":0,"nanoseconds":6000000,"negative":false} |
| 24 | return new Schema([ |
| 25 | 'type' => 'string', |
| 26 | 'format' => 'uri', |
| 27 | 'example' => 'https://www.example.com/path?query#hash', |
| 28 | ]); |
| 29 | } |
| 30 | |
| 31 | public function addDisplaySchemaFor( |
| 32 | ComponentsBuilder $componentsBuilder, |
| 33 | string $componentIdentifier, |
| 34 | ReflectionClass $class, |
| 35 | bool $nullable = false |
| 36 | ): Components { |
| 37 | $schema = $this->createSchema(); |
| 38 | if ($nullable) { |
| 39 | $schema->nullable = true; |
| 40 | } |
| 41 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 42 | |
| 43 | return $componentsBuilder->getComponents(); |
| 44 | } |
| 45 | |
| 46 | public function addCreationSchemaFor( |
| 47 | ComponentsBuilder $componentsBuilder, |
| 48 | string $componentIdentifier, |
| 49 | ReflectionClass $class, |
| 50 | bool $nullable = false |
| 51 | ): Components { |
| 52 | $schema = $this->createSchema(); |
| 53 | if ($nullable) { |
| 54 | $schema->nullable = true; |
| 55 | } |
| 56 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 57 | |
| 58 | return $componentsBuilder->getComponents(); |
| 59 | } |
| 60 | } |