Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
30 / 30 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| PredefinedObjectSchemaProvider | |
100.00% |
30 / 30 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| supports | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| createSchema | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| addDisplaySchemaFor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| addCreationSchemaFor | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 5 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 6 | use BcMath\Number; |
| 7 | use cebe\openapi\spec\Components; |
| 8 | use cebe\openapi\spec\Schema; |
| 9 | use Closure; |
| 10 | use DateInterval; |
| 11 | use DOMAttr; |
| 12 | use DOMElement; |
| 13 | use FFI\CData; |
| 14 | use FFI\CType; |
| 15 | use GMP; |
| 16 | use ReflectionClass; |
| 17 | use SimpleXMLElement; |
| 18 | use StreamBucket; |
| 19 | use Uri\Rfc3986\Uri; |
| 20 | |
| 21 | /** |
| 22 | * @implements SchemaProvider<Uri|GMP|Number> |
| 23 | */ |
| 24 | class PredefinedObjectSchemaProvider implements SchemaProvider |
| 25 | { |
| 26 | public function supports(ReflectionClass $class): bool |
| 27 | { |
| 28 | return in_array( |
| 29 | $class->name, |
| 30 | [ |
| 31 | Uri::class, |
| 32 | Number::class, |
| 33 | GMP::class, |
| 34 | StreamBucket::class, |
| 35 | CType::class, |
| 36 | CData::class, |
| 37 | SimpleXMLElement::class, |
| 38 | DOMAttr::class, |
| 39 | DOMElement::class, |
| 40 | DateInterval::class, |
| 41 | Closure::class, |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param ReflectionClass<covariant object> $class |
| 48 | */ |
| 49 | private function createSchema(ReflectionClass $class): Schema |
| 50 | { |
| 51 | $fixturePath = __DIR__ . '/../../fixtures/' . str_replace('\\', '_', $class->name) . '.json'; |
| 52 | $data = json_decode(file_get_contents($fixturePath), true, flags: JSON_THROW_ON_ERROR); |
| 53 | assert(is_array($data)); |
| 54 | return new Schema($data); |
| 55 | } |
| 56 | |
| 57 | public function addDisplaySchemaFor( |
| 58 | ComponentsBuilder $componentsBuilder, |
| 59 | string $componentIdentifier, |
| 60 | ReflectionClass $class, |
| 61 | bool $nullable = false |
| 62 | ): Components { |
| 63 | $schema = $this->createSchema($class); |
| 64 | if ($nullable) { |
| 65 | $schema->nullable = true; |
| 66 | } |
| 67 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 68 | |
| 69 | return $componentsBuilder->getComponents(); |
| 70 | } |
| 71 | |
| 72 | public function addCreationSchemaFor( |
| 73 | ComponentsBuilder $componentsBuilder, |
| 74 | string $componentIdentifier, |
| 75 | ReflectionClass $class, |
| 76 | bool $nullable = false |
| 77 | ): Components { |
| 78 | $schema = $this->createSchema($class); |
| 79 | if ($nullable) { |
| 80 | $schema->nullable = true; |
| 81 | } |
| 82 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 83 | |
| 84 | return $componentsBuilder->getComponents(); |
| 85 | } |
| 86 | } |