Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
1.96% |
1 / 51 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DurationSchemaProvider | |
1.96% |
1 / 51 |
|
25.00% |
1 / 4 |
39.92 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSchema | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
2 | |||
| addDisplaySchemaFor | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| addCreationSchemaFor | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\SchemaGenerator\SchemaProviders; |
| 3 | |
| 4 | use Apie\SchemaGenerator\Builders\ComponentsBuilder; |
| 5 | use Apie\SchemaGenerator\Interfaces\SchemaProvider; |
| 6 | use cebe\openapi\spec\Components; |
| 7 | use cebe\openapi\spec\Schema; |
| 8 | use ReflectionClass; |
| 9 | use Time\Duration; |
| 10 | |
| 11 | /** |
| 12 | * @implements SchemaProvider<Duration> |
| 13 | */ |
| 14 | class DurationSchemaProvider implements SchemaProvider |
| 15 | { |
| 16 | public const MAX_SECONDS_DURATION = 9223372035; |
| 17 | |
| 18 | public function supports(ReflectionClass $class): bool |
| 19 | { |
| 20 | return $class->name === Duration::class; |
| 21 | } |
| 22 | |
| 23 | private function createSchema(): Schema |
| 24 | { |
| 25 | // json_encode Duration returns this structure: {"seconds":0,"nanoseconds":6000000,"negative":false} |
| 26 | return new Schema([ |
| 27 | 'type' => 'object', |
| 28 | 'description' => 'Represents a duration in seconds', |
| 29 | 'required' => ['seconds', 'nanoseconds', 'negative'], |
| 30 | 'properties' => [ |
| 31 | 'seconds' => new Schema([ |
| 32 | 'type' => 'integer', |
| 33 | 'description' => 'The number of seconds in the duration', |
| 34 | 'minimum' => 0, |
| 35 | 'maximum' => min(PHP_INT_MAX, self::MAX_SECONDS_DURATION), |
| 36 | ]), |
| 37 | 'nanoseconds' => new Schema([ |
| 38 | 'type' => 'integer', |
| 39 | 'description' => 'The number of nanoseconds in the duration', |
| 40 | 'minimum' => 0, |
| 41 | 'maximum' => 999999999 |
| 42 | ]), |
| 43 | 'negative' => new Schema([ |
| 44 | 'type' => 'boolean', |
| 45 | 'description' => 'Whether the duration is negative', |
| 46 | ]), |
| 47 | ], |
| 48 | 'example' => [ |
| 49 | 'seconds' => 0, |
| 50 | 'nanoseconds' => 6000000, |
| 51 | 'negative' => false, |
| 52 | ], |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | public function addDisplaySchemaFor( |
| 57 | ComponentsBuilder $componentsBuilder, |
| 58 | string $componentIdentifier, |
| 59 | ReflectionClass $class, |
| 60 | bool $nullable = false |
| 61 | ): Components { |
| 62 | $schema = $this->createSchema(); |
| 63 | if ($nullable) { |
| 64 | $schema->nullable = true; |
| 65 | } |
| 66 | $componentsBuilder->setSchema($componentIdentifier, $schema); |
| 67 | |
| 68 | return $componentsBuilder->getComponents(); |
| 69 | } |
| 70 | |
| 71 | public function addCreationSchemaFor( |
| 72 | ComponentsBuilder $componentsBuilder, |
| 73 | string $componentIdentifier, |
| 74 | ReflectionClass $class, |
| 75 | bool $nullable = false |
| 76 | ): Components { |
| 77 | $displaySchema = $this->createSchema(); |
| 78 | if ($nullable) { |
| 79 | $displaySchema->nullable = true; |
| 80 | } |
| 81 | $componentsBuilder->setSchema($componentIdentifier, new Schema([ |
| 82 | 'oneOf' => [ |
| 83 | $displaySchema, |
| 84 | new Schema([ |
| 85 | 'type' => 'number', |
| 86 | 'format' => 'integer', |
| 87 | 'description' => 'A duration in milliseconds', |
| 88 | 'example' => 3600000, |
| 89 | 'minimum' => 0, |
| 90 | 'maximum' => min(PHP_INT_MAX, self::MAX_SECONDS_DURATION * 1000 + 999), |
| 91 | ]), |
| 92 | ], |
| 93 | ])); |
| 94 | return $componentsBuilder->getComponents(); |
| 95 | } |
| 96 | } |