Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IdentifierNormalizer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 supportsDenormalization
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
 denormalize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\ContextConstants;
6use Apie\Core\Datalayers\ApieDatalayer;
7use Apie\Core\Datalayers\ApieDatalayerWithSupport;
8use Apie\Core\Entities\EntityInterface;
9use Apie\Core\Identifiers\IdentifierInterface;
10use Apie\Core\Lists\ItemHashmap;
11use Apie\Core\Lists\ItemList;
12use Apie\Serializer\Context\ApieSerializerContext;
13use Apie\Serializer\Interfaces\DenormalizerInterface;
14use Psr\Http\Message\UploadedFileInterface;
15use ReflectionClass;
16
17class IdentifierNormalizer implements DenormalizerInterface
18{
19    public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool
20    {
21        $apieContext = $apieSerializerContext->getContext();
22        $hierarchy = $apieContext->getContext('hierarchy', false) ?? [];
23        $fieldName = array_pop($hierarchy);
24        if (class_exists($desiredType) && $fieldName !== 'id' && $apieContext->hasContext(ApieDatalayer::class) && $apieContext->hasContext(ContextConstants::BOUNDED_CONTEXT_ID)) {
25            $refl = new ReflectionClass($desiredType);
26            $datalayer = $apieContext->getContext(ApieDatalayer::class);
27            $boundedContextId = new BoundedContextId($apieContext->getContext(ContextConstants::BOUNDED_CONTEXT_ID));
28            return $refl->implementsInterface(IdentifierInterface::class)
29                && (!$datalayer instanceof ApieDatalayerWithSupport || $datalayer->isSupported($refl, $boundedContextId));
30        }
31        return false;
32    }
33
34    /**
35     * @template T of IdentifierInterface<EntityInterface>
36     * @param class-string<T> $desiredType
37     * @return T
38     */
39    public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): IdentifierInterface
40    {
41        $identifier = $desiredType::fromNative($object);
42        $datalayer = $apieSerializerContext->getContext()->getContext(ApieDatalayer::class);
43        $boundedContextId = $apieSerializerContext->getContext()->getContext(ContextConstants::BOUNDED_CONTEXT_ID);
44        $datalayer->find($identifier, BoundedContextId::fromNative($boundedContextId));
45        return $identifier;
46    }
47}