Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
10 / 13
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReflectionTypeNormalizer
76.92% covered (warning)
76.92%
10 / 13
50.00% covered (danger)
50.00%
2 / 4
5.31
0.00% covered (danger)
0.00%
0 / 1
 supportsDenormalization
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 denormalize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 supportsNormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\Lists\ItemHashmap;
5use Apie\Core\Lists\ItemList;
6use Apie\Core\ValueObjects\Utils;
7use Apie\Serializer\Context\ApieSerializerContext;
8use Apie\Serializer\Interfaces\DenormalizerInterface;
9use Apie\Serializer\Interfaces\NormalizerInterface;
10use Apie\TypeConverter\ReflectionTypeFactory;
11use Psr\Http\Message\UploadedFileInterface;
12use ReflectionIntersectionType;
13use ReflectionNamedType;
14use ReflectionType;
15use ReflectionUnionType;
16
17class ReflectionTypeNormalizer implements DenormalizerInterface, NormalizerInterface
18{
19    public function supportsDenormalization(
20        string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object,
21        string $desiredType,
22        ApieSerializerContext $apieSerializerContext
23    ): bool {
24        return in_array(
25            $desiredType,
26            [
27                ReflectionType::class,
28                ReflectionNamedType::class,
29                ReflectionUnionType::class,
30                ReflectionIntersectionType::class
31            ]
32        );
33    }
34    public function denormalize(
35        string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object,
36        string $desiredType,
37        ApieSerializerContext $apieSerializerContext
38    ): mixed {
39        return ReflectionTypeFactory::createReflectionType(Utils::toString($object));
40    }
41    public function supportsNormalization(
42        mixed $object,
43        ApieSerializerContext $apieSerializerContext
44    ): bool {
45        return $object instanceof ReflectionType;
46    }
47    public function normalize(
48        mixed $object,
49        ApieSerializerContext $apieSerializerContext
50    ): string|int|float|bool|null|ItemList|ItemHashmap {
51        assert($object instanceof ReflectionType);
52        return $object instanceof ReflectionNamedType ? $object->getName() : (string) $object;
53    }
54}