Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
15 / 21
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadedFileNormalizer
71.43% covered (warning)
71.43%
15 / 21
50.00% covered (danger)
50.00%
1 / 2
9.49
0.00% covered (danger)
0.00%
0 / 1
 supportsDenormalization
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 denormalize
60.00% covered (warning)
60.00%
9 / 15
0.00% covered (danger)
0.00%
0 / 1
6.60
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\ContextConstants;
5use Apie\Core\Enums\DoNotChangeUploadedFile;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\Core\Lists\ItemHashmap;
8use Apie\Core\Lists\ItemList;
9use Apie\Core\PropertyAccess;
10use Apie\Core\ValueObjects\JsonFileUpload;
11use Apie\Core\ValueObjects\Utils;
12use Apie\Serializer\Context\ApieSerializerContext;
13use Apie\Serializer\Exceptions\FileUploadException;
14use Apie\Serializer\Interfaces\DenormalizerInterface;
15use Apie\TypeConverter\ReflectionTypeFactory;
16use Psr\Http\Message\UploadedFileInterface;
17use ReflectionClass;
18
19class UploadedFileNormalizer implements DenormalizerInterface
20{
21    public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool
22    {
23        if (in_array($desiredType, [UploadedFileInterface::class, StoredFile::class])) {
24            return true;
25        }
26        if (!class_exists($desiredType)) {
27            return false;
28        }
29        $class = new ReflectionClass($desiredType);
30        return in_array(UploadedFileInterface::class, $class->getInterfaceNames());
31    }
32    public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): UploadedFileInterface
33    {
34        if ($object instanceof UploadedFileInterface) {
35            if ($object->getError() !== UPLOAD_ERR_OK) {
36                throw new FileUploadException($object);
37            }
38            return $object;
39        }
40        // we submit a special 'DoNotChange' string on edits to avoid having to submit a
41        // new file all the time.
42        if ($object === DoNotChangeUploadedFile::DoNotChange->value) {
43            $hierarchy = $apieSerializerContext->getContext()->getContext('hierarchy', false) ?? [];
44            $resource = $apieSerializerContext->getContext()->getContext(ContextConstants::RESOURCE);
45            return PropertyAccess::getPropertyValue($resource, $hierarchy, $apieSerializerContext->getContext());
46        }
47        $array = Utils::toArray($object);
48        /** @var JsonFileUpload $object */
49        $object = $apieSerializerContext->denormalizeFromTypehint(
50            $array,
51            ReflectionTypeFactory::createReflectionType(JsonFileUpload::class)
52        );
53        /** @var class-string<StoredFile> $className */
54        $className = $desiredType === UploadedFileInterface::class ? StoredFile::class : $desiredType;
55        return $object->toUploadedFile($className);
56    }
57}