Lines
66.66%
6 / 9
Functions and Methods
75.00%
3 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| IsoInstantConverter | 66.66% | 6 / 9 | 75.00% | 3 / 4 | 7.33 | 0.00% | 0 / 1 | |||
| 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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Json; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\TimeException; | |
| 8 | use Stewart\Contracts\Time\Instant; | |
| 9 | use Stewart\Support\Exception\JsonShapeError; | |
| 10 | use Stewart\Support\Exception\JsonShapeException; | |
| 11 | ||
| 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 | } |