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