Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| IdentifierNormalizer | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| supportsDenormalization | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
| denormalize | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\ContextConstants; |
| 6 | use Apie\Core\Datalayers\ApieDatalayer; |
| 7 | use Apie\Core\Datalayers\ApieDatalayerWithSupport; |
| 8 | use Apie\Core\Entities\EntityInterface; |
| 9 | use Apie\Core\Identifiers\IdentifierInterface; |
| 10 | use Apie\Core\Lists\ItemHashmap; |
| 11 | use Apie\Core\Lists\ItemList; |
| 12 | use Apie\Serializer\Context\ApieSerializerContext; |
| 13 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 14 | use Psr\Http\Message\UploadedFileInterface; |
| 15 | use ReflectionClass; |
| 16 | |
| 17 | class 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 | } |