Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.86% |
13 / 14 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
SchemaGenerator | |
92.86% |
13 / 14 |
|
50.00% |
1 / 2 |
4.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createSchema | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 |
1 | <?php |
2 | namespace Apie\SchemaGenerator; |
3 | |
4 | use Apie\TypeConverter\ReflectionTypeFactory; |
5 | use cebe\openapi\ReferenceContext; |
6 | use cebe\openapi\spec\OpenApi; |
7 | use cebe\openapi\spec\Reference; |
8 | use cebe\openapi\spec\Schema; |
9 | |
10 | final class SchemaGenerator |
11 | { |
12 | public function __construct( |
13 | private readonly ComponentsBuilderFactory $componentsBuilderFactory |
14 | ) { |
15 | } |
16 | |
17 | public function createSchema(string $typehint): Schema |
18 | { |
19 | $builder = $this->componentsBuilderFactory->createComponentsBuilder(); |
20 | $isArray = false; |
21 | if (str_ends_with($typehint, '[]')) { |
22 | $isArray = true; |
23 | $typehint = substr($typehint, 0, strlen($typehint) - 2); |
24 | } |
25 | $schema = $builder->getSchemaForType(ReflectionTypeFactory::createReflectionType($typehint), $isArray); |
26 | if ($schema instanceof Reference) { |
27 | $schema = $builder->getSchemaForReference($schema); |
28 | } |
29 | $schema->resolveReferences(new ReferenceContext( |
30 | new OpenApi(['components' => $builder->getComponents()]), |
31 | 'file:///#/components' |
32 | )); |
33 | return $schema; |
34 | } |
35 | } |