Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ItemListSchemaProvider | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| addDisplaySchemaFor | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| addCreationSchemaFor | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemList; |
| 5 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 6 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 7 | use cebe\openapi\spec\Components; |
| 8 | use ReflectionClass; |
| 9 | |
| 10 | /** |
| 11 | * Creates schemas for Item Lists. |
| 12 | * |
| 13 | * @implements SchemaProvider<ItemList> |
| 14 | */ |
| 15 | class ItemListSchemaProvider implements SchemaProvider |
| 16 | { |
| 17 | public function supports(ReflectionClass $class): bool |
| 18 | { |
| 19 | return $class->isSubclassOf(ItemList::class) || $class->name === ItemList::class; |
| 20 | } |
| 21 | |
| 22 | public function addDisplaySchemaFor( |
| 23 | ComponentsBuilder $componentsBuilder, |
| 24 | string $componentIdentifier, |
| 25 | ReflectionClass $class, |
| 26 | bool $nullable = false |
| 27 | ): Components { |
| 28 | $type = $class->getMethod('offsetGet')->getReturnType(); |
| 29 | $schema = $componentsBuilder->getSchemaForType($type, true, display: true, nullable: $nullable); |
| 30 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 31 | |
| 32 | return $componentsBuilder->getComponents(); |
| 33 | } |
| 34 | |
| 35 | public function addCreationSchemaFor( |
| 36 | ComponentsBuilder $componentsBuilder, |
| 37 | string $componentIdentifier, |
| 38 | ReflectionClass $class, |
| 39 | bool $nullable = false |
| 40 | ): Components { |
| 41 | $type = $class->getMethod('offsetGet')->getReturnType(); |
| 42 | $schema = $componentsBuilder->getSchemaForType($type, true, display: false, nullable: $nullable); |
| 43 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 44 | |
| 45 | return $componentsBuilder->getComponents(); |
| 46 | } |
| 47 | } |