Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| DateTimeNormalizer | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| denormalize | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemHashmap; |
| 5 | use Apie\Core\Lists\ItemList; |
| 6 | use Apie\Core\ValueObjects\Utils; |
| 7 | use Apie\Serializer\Context\ApieSerializerContext; |
| 8 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 9 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 10 | use DateTimeImmutable; |
| 11 | use DateTimeInterface; |
| 12 | use Psr\Http\Message\UploadedFileInterface; |
| 13 | |
| 14 | final class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface |
| 15 | { |
| 16 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 17 | { |
| 18 | return $object instanceof DateTimeInterface; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param DateTimeInterface $object |
| 23 | */ |
| 24 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string |
| 25 | { |
| 26 | return $object->format(DateTimeInterface::ATOM); |
| 27 | } |
| 28 | |
| 29 | public function supportsDenormalization( |
| 30 | string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, |
| 31 | string $desiredType, |
| 32 | ApieSerializerContext $apieSerializerContext |
| 33 | ): bool { |
| 34 | return is_a($desiredType, DateTimeInterface::class, true); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param class-string<DateTimeInterface> $desiredType |
| 39 | */ |
| 40 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): DateTimeInterface |
| 41 | { |
| 42 | $object = Utils::toString($object); |
| 43 | if ($desiredType === DateTimeInterface::class) { |
| 44 | $desiredType = DateTimeImmutable::class; |
| 45 | } |
| 46 | return new $desiredType($object); |
| 47 | } |
| 48 | } |