Lines
66.66%
6 / 9
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| handledClass | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| keySuffix | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| encodeValue | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| decodeValue | 40.00% | 2 / 5 | 0.00% | 0 / 1 | 4.94 | ||
| 12 | final readonly class IsoInstantConverter implements ValueConverter | |
| 13 | { | |
| 14 | public function handledClass(): string | |
| 15 | { | |
| 16 | return Instant::class; | |
| 17 | } | |
| 18 | ||
| 19 | public function keySuffix(): string | |
| 20 | { | |
| 21 | return ''; | |
| 22 | } | |
| 23 | ||
| 24 | public function encodeValue(object $value): string | |
| 25 | { | |
| 26 | \assert($value instanceof Instant); | |
| 27 | ||
| 28 | return $value->toIso8601(); | |
| 29 | } | |
| 30 | ||
| 31 | public function decodeValue(mixed $value, string $path): Instant | |
| 32 | { | |
| 33 | if (!\is_string($value)) { | |
| 34 | throw JsonShapeException::wrongType($path, 'a string', get_debug_type($value)); | |
| 35 | } | |
| 36 | ||
| 37 | try { | |
| 38 | return Instant::fromIso($value); | |
| 39 | } catch (TimeException $e) { | |
| 40 | throw JsonShapeException::createForReason(JsonShapeError::InstantInvalid, $e, key: $path); | |
| 41 | } | |
| 42 | } | |
| 43 | } |