Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValueObjectNormalizer
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
10.03
0.00% covered (danger)
0.00%
0 / 1
 supportsNormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalize
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 supportsDenormalization
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 denormalize
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\Lists\ItemHashmap;
5use Apie\Core\Lists\ItemList;
6use Apie\Core\ValueObjects\CompositeValueObject;
7use Apie\Core\ValueObjects\CompositeWithOwnValidation;
8use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface;
9use Apie\Serializer\Context\ApieSerializerContext;
10use Apie\Serializer\Exceptions\ValidationException;
11use Apie\Serializer\Interfaces\DenormalizerInterface;
12use Apie\Serializer\Interfaces\NormalizerInterface;
13use Exception;
14use Psr\Http\Message\UploadedFileInterface;
15use ReflectionClass;
16
17class ValueObjectNormalizer implements NormalizerInterface, DenormalizerInterface
18{
19    public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool
20    {
21        return $object instanceof ValueObjectInterface;
22    }
23    public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string|int|float|bool|null|ItemList|ItemHashmap
24    {
25        $value = $object->toNative();
26        if (is_iterable($value)) {
27            return $apieSerializerContext->normalizeAgain($value);
28        }
29        return $value;
30    }
31    public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool
32    {
33        if (is_a($desiredType, ValueObjectInterface::class, true)) {
34            $class = new ReflectionClass($desiredType);
35
36            return in_array(CompositeWithOwnValidation::class, $class->getInterfaceNames())
37                || !in_array(CompositeValueObject::class, $class->getTraitNames());
38        }
39        return false;
40    }
41    public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): mixed
42    {
43        try {
44            return $desiredType::fromNative($object);
45        } catch (Exception $exception) {
46            if ($object === null || $object === '') {
47                $exception = new \LogicException('Value is required.', previous: $exception);
48            }
49            throw ValidationException::createFromArray(['' => $exception]);
50        }
51    }
52}