Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ItemHashmapSchemaProvider | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
addDisplaySchemaFor | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
addCreationSchemaFor | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\SchemaGenerator\SchemaProviders; |
3 | |
4 | use Apie\Core\Lists\ItemHashmap; |
5 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
6 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
7 | use cebe\openapi\spec\Components; |
8 | use cebe\openapi\spec\Schema; |
9 | use ReflectionClass; |
10 | |
11 | /** |
12 | * @implements SchemaProvider<ItemHashmap> |
13 | */ |
14 | class ItemHashmapSchemaProvider implements SchemaProvider |
15 | { |
16 | public function supports(ReflectionClass $class): bool |
17 | { |
18 | return $class->isSubclassOf(ItemHashmap::class) || $class->name === ItemHashmap::class; |
19 | } |
20 | |
21 | public function addDisplaySchemaFor( |
22 | ComponentsBuilder $componentsBuilder, |
23 | string $componentIdentifier, |
24 | ReflectionClass $class, |
25 | bool $nullable = false |
26 | ): Components { |
27 | $type = $class->getMethod('offsetGet')->getReturnType(); |
28 | $schema = $componentsBuilder->getSchemaForType($type, display: true, nullable: $nullable); |
29 | $schema = new Schema([ |
30 | 'type' => 'object', |
31 | 'additionalProperties' => $schema, |
32 | ]); |
33 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
34 | if ($nullable) { |
35 | $schema->nullable = true; |
36 | } |
37 | |
38 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
39 | |
40 | return $componentsBuilder->getComponents(); |
41 | } |
42 | |
43 | public function addCreationSchemaFor( |
44 | ComponentsBuilder $componentsBuilder, |
45 | string $componentIdentifier, |
46 | ReflectionClass $class, |
47 | bool $nullable = false |
48 | ): Components { |
49 | $type = $class->getMethod('offsetGet')->getReturnType(); |
50 | $schema = $componentsBuilder->getSchemaForType($type, display: false, nullable: $nullable); |
51 | $schema = new Schema([ |
52 | 'type' => 'object', |
53 | 'additionalProperties' => $schema, |
54 | ]); |
55 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
56 | if ($nullable) { |
57 | $schema->nullable = true; |
58 | } |
59 | |
60 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
61 | |
62 | return $componentsBuilder->getComponents(); |
63 | } |
64 | } |