Lines
100.00%
7 / 7
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| EpochInstantConverter | 100.00% | 7 / 7 | 100.00% | 4 / 4 | 5 | 100.00% | 1 / 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 | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Json; | |
| 6 | ||
| 7 | use Stewart\Contracts\Time\Instant; | |
| 8 | use Stewart\Support\Exception\JsonShapeException; | |
| 9 | ||
| 10 | final readonly class EpochInstantConverter implements ValueConverter | |
| 11 | { | |
| 12 | public function handledClass(): string | |
| 13 | { | |
| 14 | return Instant::class; | |
| 15 | } | |
| 16 | ||
| 17 | public function keySuffix(): string | |
| 18 | { | |
| 19 | return '_us'; | |
| 20 | } | |
| 21 | ||
| 22 | public function encodeValue(object $value): int | |
| 23 | { | |
| 24 | \assert($value instanceof Instant); | |
| 25 | ||
| 26 | return $value->toEpochMicroseconds(); | |
| 27 | } | |
| 28 | ||
| 29 | public function decodeValue(mixed $value, string $path): Instant | |
| 30 | { | |
| 31 | return \is_int($value) | |
| 32 | ? Instant::fromEpochMicroseconds($value) | |
| 33 | : throw JsonShapeException::wrongType($path, 'a whole number of microseconds since the epoch', get_debug_type($value)); | |
| 34 | } | |
| 35 | } |