Lines
88.88%
8 / 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 | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 3.07 | ||
| 11 | final readonly class DurationConverter implements ValueConverter | |
| 12 | { | |
| 13 | private const string EXPECTED = 'a non-negative whole number of microseconds'; | |
| 14 | ||
| 15 | public function handledClass(): string | |
| 16 | { | |
| 17 | return Duration::class; | |
| 18 | } | |
| 19 | ||
| 20 | public function keySuffix(): string | |
| 21 | { | |
| 22 | return '_us'; | |
| 23 | } | |
| 24 | ||
| 25 | public function encodeValue(object $value): int | |
| 26 | { | |
| 27 | \assert($value instanceof Duration); | |
| 28 | ||
| 29 | return $value->toMicroseconds(); | |
| 30 | } | |
| 31 | ||
| 32 | public function decodeValue(mixed $value, string $path): Duration | |
| 33 | { | |
| 34 | if (!\is_int($value)) { | |
| 35 | throw JsonShapeException::wrongType($path, self::EXPECTED, get_debug_type($value)); | |
| 36 | } | |
| 37 | ||
| 38 | try { | |
| 39 | return Duration::microseconds($value); | |
| 40 | } catch (TimeException) { | |
| 41 | throw JsonShapeException::wrongType($path, self::EXPECTED, (string) $value); | |
| 42 | } | |
| 43 | } | |
| 44 | } |