Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| SchemaGenerator | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMethodSchema | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| createSchema | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| 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 | use ReflectionMethod; |
| 10 | |
| 11 | final class SchemaGenerator |
| 12 | { |
| 13 | public function __construct( |
| 14 | private readonly ComponentsBuilderFactory $componentsBuilderFactory |
| 15 | ) { |
| 16 | } |
| 17 | |
| 18 | public function createMethodSchema(ReflectionMethod $method): Schema |
| 19 | { |
| 20 | $builder = $this->componentsBuilderFactory->createComponentsBuilder(); |
| 21 | $methodSchema = $builder->getSchemaForMethod($method); |
| 22 | $schema = new Schema([ |
| 23 | 'type' => 'object', |
| 24 | 'properties' => $methodSchema->schemas, |
| 25 | 'required' => $methodSchema->required |
| 26 | ]); |
| 27 | $schema->resolveReferences(new ReferenceContext( |
| 28 | new OpenApi(['components' => $builder->getComponents()]), |
| 29 | 'file:///#/components' |
| 30 | )); |
| 31 | return $schema; |
| 32 | } |
| 33 | |
| 34 | public function createSchema(string $typehint, bool $display = false): Schema |
| 35 | { |
| 36 | $builder = $this->componentsBuilderFactory->createComponentsBuilder(); |
| 37 | $isArray = false; |
| 38 | if (str_ends_with($typehint, '[]')) { |
| 39 | $isArray = true; |
| 40 | $typehint = substr($typehint, 0, strlen($typehint) - 2); |
| 41 | } |
| 42 | $schema = $builder->getSchemaForType(ReflectionTypeFactory::createReflectionType($typehint), $isArray, $display); |
| 43 | if ($schema instanceof Reference) { |
| 44 | $schema = $builder->getSchemaForReference($schema); |
| 45 | } |
| 46 | $schema->resolveReferences(new ReferenceContext( |
| 47 | new OpenApi(['components' => $builder->getComponents()]), |
| 48 | 'file:///#/components' |
| 49 | )); |
| 50 | return $schema; |
| 51 | } |
| 52 | } |