Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UnionDenormalizer
81.82% covered (warning)
81.82%
9 / 11
50.00% covered (danger)
50.00%
1 / 2
6.22
0.00% covered (danger)
0.00%
0 / 1
 supportsDenormalization
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
5.93
 denormalize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Apie\Serializer\Normalizers;
4
5use Apie\Core\Lists\ItemHashmap;
6use Apie\Core\Lists\ItemList;
7use Apie\Serializer\Context\ApieSerializerContext;
8use Apie\Serializer\Interfaces\DenormalizerInterface;
9use Apie\TypeConverter\ReflectionTypeFactory;
10use Psr\Http\Message\UploadedFileInterface;
11use ReflectionIntersectionType;
12use ReflectionUnionType;
13
14class UnionDenormalizer implements DenormalizerInterface
15{
16
17    public function supportsDenormalization(float|bool|int|string|UploadedFileInterface|ItemHashmap|ItemList|null $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool
18    {
19        if (strpos($desiredType, '&') !== false || strpos($desiredType, '|') !== false) {
20            try {
21                $type = ReflectionTypeFactory::createReflectionType($desiredType);
22                return $type instanceof ReflectionUnionType || $type instanceof ReflectionIntersectionType;
23            } catch (\Throwable) {
24                return false;
25            }
26        }
27        return false;
28    }
29
30    public function denormalize(float|bool|int|string|UploadedFileInterface|ItemHashmap|ItemList|null $object, string $desiredType, ApieSerializerContext $apieSerializerContext): mixed
31    {
32        $type = ReflectionTypeFactory::createReflectionType($desiredType);
33        return $apieSerializerContext->denormalizeFromTypehint(
34            $object,
35            $type
36        );
37    }
38}