Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
20 / 28 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ResourceNormalizer | |
71.43% |
20 / 28 |
|
50.00% |
2 / 4 |
18.57 | |
0.00% |
0 / 1 |
| supportsNormalization | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| normalize | |
66.67% |
14 / 21 |
|
0.00% |
0 / 1 |
10.37 | |||
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| denormalize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\Actions\ActionResponse; |
| 5 | use Apie\Core\ContextConstants; |
| 6 | use Apie\Core\Datalayers\Lists\PaginatedResult; |
| 7 | use Apie\Core\Entities\EntityInterface; |
| 8 | use Apie\Core\FileStorage\StoredFile; |
| 9 | use Apie\Core\Lists\ItemHashmap; |
| 10 | use Apie\Core\Lists\ItemList; |
| 11 | use Apie\Core\PropertyAccess; |
| 12 | use Apie\Serializer\Context\ApieSerializerContext; |
| 13 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 14 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 15 | use Psr\Http\Message\UploadedFileInterface; |
| 16 | use ReflectionClass; |
| 17 | use UnitEnum; |
| 18 | |
| 19 | class ResourceNormalizer implements NormalizerInterface, DenormalizerInterface |
| 20 | { |
| 21 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 22 | { |
| 23 | if (!$apieSerializerContext->getContext()->hasContext(ActionResponse::class)) { |
| 24 | return false; |
| 25 | } |
| 26 | return is_resource($object) |
| 27 | || $object instanceof UploadedFileInterface |
| 28 | || get_debug_type($object) === 'resource (closed)'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param resource|UploadedFileInterface $object |
| 33 | */ |
| 34 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): ?string |
| 35 | { |
| 36 | $apieContext = $apieSerializerContext->getContext(); |
| 37 | $actionResponse = $apieContext->getContext(ActionResponse::class); |
| 38 | assert($actionResponse instanceof ActionResponse); |
| 39 | assert(isset($actionResponse->result)); |
| 40 | $hierarchy = $apieContext->getContext('hierarchy', false) ?? []; |
| 41 | $result = $actionResponse->result; |
| 42 | |
| 43 | if ($result instanceof PaginatedResult) { |
| 44 | $index = array_shift($hierarchy); |
| 45 | $result = PropertyAccess::getPropertyValue($result->list, [$index], $apieContext, false); |
| 46 | } |
| 47 | if ($result instanceof EntityInterface) { |
| 48 | $boundedContextId = $apieContext->getContext(ContextConstants::BOUNDED_CONTEXT_ID); |
| 49 | $resourceClass = new ReflectionClass($apieContext->getContext(ContextConstants::RESOURCE_NAME)); |
| 50 | $displayAsString = ($apieContext->hasContext(ContextConstants::GET_ALL_OBJECTS) && $apieContext->hasContext(ContextConstants::CMS)) |
| 51 | || $apieContext->hasContext(ContextConstants::SHOW_PROFILE); |
| 52 | if ($displayAsString) { |
| 53 | if ($object instanceof StoredFile) { |
| 54 | return $object->getClientFilename() ?? $object->getServerPath(); |
| 55 | } |
| 56 | if (is_resource($object)) { |
| 57 | return 'stream'; |
| 58 | } |
| 59 | } |
| 60 | return '/' . $boundedContextId . '/' . $resourceClass->getShortName() . '/' . $result->getId()->toNative() . '/download/' . implode('/', $hierarchy); |
| 61 | } |
| 62 | |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool |
| 67 | { |
| 68 | return $desiredType === 'resource'; |
| 69 | } |
| 70 | |
| 71 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): UnitEnum |
| 72 | { |
| 73 | throw new \LogicException('not implemented yet'); |
| 74 | } |
| 75 | } |