Lines 85.71% 6 / 7
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DateTimeImmutableConverter 85.71% 6 / 7 75.00% 3 / 4 5.07 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 66.66% 2 / 3 0.00% 0 / 1 2.15
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Ipc\Wire;
6
7use DateTimeImmutable;
8use Stewart\Contracts\Exposure\CalendarStateFormat;
9use Stewart\Runtime\Json\ValueConverter;
10use Stewart\Support\Exception\JsonShapeException;
11
12final readonly class DateTimeImmutableConverter implements ValueConverter
13{
14    public function handledClass(): string
15    {
16        return DateTimeImmutable::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 DateTimeImmutable);
27
28        return CalendarStateFormat::formatDateTime($value);
29    }
30
31    public function decodeValue(mixed $value, string $path): DateTimeImmutable
32    {
33        if (!\is_string($value)) {
34            throw JsonShapeException::wrongType($path, 'an ISO 8601 date and time', get_debug_type($value));
35        }
36
37        return CalendarStateFormat::parseDateTime($value) ?? throw JsonShapeException::unexpectedValue($path, 'an ISO 8601 date and time with an offset', $value);
38    }
39}