Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
9 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UnionDenormalizer | |
81.82% |
9 / 11 |
|
50.00% |
1 / 2 |
6.22 | |
0.00% |
0 / 1 |
| supportsDenormalization | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
5.93 | |||
| denormalize | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\Serializer\Normalizers; |
| 4 | |
| 5 | use Apie\Core\Lists\ItemHashmap; |
| 6 | use Apie\Core\Lists\ItemList; |
| 7 | use Apie\Serializer\Context\ApieSerializerContext; |
| 8 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 9 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 10 | use Psr\Http\Message\UploadedFileInterface; |
| 11 | use ReflectionIntersectionType; |
| 12 | use ReflectionUnionType; |
| 13 | |
| 14 | class 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 | } |