Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| UploadedFileSchemaProvider | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| addDisplaySchemaFor | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| addCreationSchemaFor | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\JsonFileUpload; |
| 5 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 6 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 7 | use cebe\openapi\spec\Components; |
| 8 | use cebe\openapi\spec\Reference; |
| 9 | use cebe\openapi\spec\Schema; |
| 10 | use Psr\Http\Message\UploadedFileInterface; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | /** |
| 14 | * @implements SchemaProvider<UploadedFileInterface> |
| 15 | */ |
| 16 | class UploadedFileSchemaProvider implements SchemaProvider |
| 17 | { |
| 18 | public function supports(ReflectionClass $class): bool |
| 19 | { |
| 20 | return $class->name === UploadedFileInterface::class || in_array(UploadedFileInterface::class, $class->getInterfaceNames()); |
| 21 | } |
| 22 | |
| 23 | public function addDisplaySchemaFor( |
| 24 | ComponentsBuilder $componentsBuilder, |
| 25 | string $componentIdentifier, |
| 26 | ReflectionClass $class, |
| 27 | bool $nullable = false |
| 28 | ): Components { |
| 29 | $schema = new Schema([ |
| 30 | 'type' => 'string', |
| 31 | 'format' => 'path', |
| 32 | ]); |
| 33 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
| 34 | if ($nullable) { |
| 35 | $schema->nullable = true; |
| 36 | } |
| 37 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 38 | |
| 39 | return $componentsBuilder->getComponents(); |
| 40 | } |
| 41 | |
| 42 | public function addCreationSchemaFor( |
| 43 | ComponentsBuilder $componentsBuilder, |
| 44 | string $componentIdentifier, |
| 45 | ReflectionClass $class, |
| 46 | bool $nullable = false |
| 47 | ): Components { |
| 48 | if ($componentsBuilder->getContentType() && str_starts_with($componentsBuilder->getContentType(), 'multipart/')) { |
| 49 | $schema = new Schema([ |
| 50 | 'type' => 'string', |
| 51 | 'format' => 'binary', |
| 52 | 'x-upload' => '*/*', |
| 53 | 'nullable' => $nullable, |
| 54 | ]); |
| 55 | ComponentsBuilder::addDescriptionOfObject($schema, $class); |
| 56 | } else { |
| 57 | $schema = $componentsBuilder->addCreationSchemaFor(JsonFileUpload::class, nullable: $nullable); |
| 58 | if ($schema instanceof Reference) { |
| 59 | $schema = $componentsBuilder->getSchemaForReference($schema); |
| 60 | } |
| 61 | } |
| 62 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 63 | |
| 64 | return $componentsBuilder->getComponents(); |
| 65 | } |
| 66 | } |