Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
15 / 21 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UploadedFileNormalizer | |
71.43% |
15 / 21 |
|
50.00% |
1 / 2 |
9.49 | |
0.00% |
0 / 1 |
| supportsDenormalization | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| denormalize | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
6.60 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\ContextConstants; |
| 5 | use Apie\Core\Enums\DoNotChangeUploadedFile; |
| 6 | use Apie\Core\FileStorage\StoredFile; |
| 7 | use Apie\Core\Lists\ItemHashmap; |
| 8 | use Apie\Core\Lists\ItemList; |
| 9 | use Apie\Core\PropertyAccess; |
| 10 | use Apie\Core\ValueObjects\JsonFileUpload; |
| 11 | use Apie\Core\ValueObjects\Utils; |
| 12 | use Apie\Serializer\Context\ApieSerializerContext; |
| 13 | use Apie\Serializer\Exceptions\FileUploadException; |
| 14 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 15 | use Apie\TypeConverter\ReflectionTypeFactory; |
| 16 | use Psr\Http\Message\UploadedFileInterface; |
| 17 | use ReflectionClass; |
| 18 | |
| 19 | class 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 | } |