Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ValueObjectNormalizer | |
100.00% |
13 / 13 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| supportsDenormalization | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| denormalize | |
100.00% |
3 / 3 |
|
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\CompositeValueObject; |
| 7 | use Apie\Core\ValueObjects\CompositeWithOwnValidation; |
| 8 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 9 | use Apie\Serializer\Context\ApieSerializerContext; |
| 10 | use Apie\Serializer\Exceptions\ValidationException; |
| 11 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 12 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 13 | use Exception; |
| 14 | use Psr\Http\Message\UploadedFileInterface; |
| 15 | use ReflectionClass; |
| 16 | |
| 17 | class 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 | return $class->implementsInterface(CompositeWithOwnValidation::class) |
| 36 | ||!in_array(CompositeValueObject::class, $class->getTraitNames()); |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): mixed |
| 41 | { |
| 42 | try { |
| 43 | return $desiredType::fromNative($object); |
| 44 | } catch (Exception $exception) { |
| 45 | throw ValidationException::createFromArray(['' => $exception]); |
| 46 | } |
| 47 | } |
| 48 | } |