1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Serializer\Normalizer; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader; |
17
|
|
|
use Silverback\ApiComponentBundle\Model\Uploadable\Base64EncodedFile; |
18
|
|
|
use Silverback\ApiComponentBundle\Model\Uploadable\UploadedBase64EncodedFile; |
19
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
20
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
21
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
22
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
24
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Vincent Chalamon <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class UploadableNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface |
30
|
|
|
{ |
31
|
|
|
use DenormalizerAwareTrait; |
32
|
|
|
|
33
|
|
|
private const ALREADY_CALLED = 'UPLOADABLE_NORMALIZER_ALREADY_CALLED'; |
34
|
|
|
|
35
|
|
|
private UploadableAnnotationReader $uploadableHelper; |
36
|
|
|
|
37
|
|
|
public function __construct(UploadableAnnotationReader $uploadableHelper) |
38
|
|
|
{ |
39
|
|
|
$this->uploadableHelper = $uploadableHelper; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
46
|
|
|
{ |
47
|
|
|
$context[self::ALREADY_CALLED] = true; |
48
|
|
|
|
49
|
|
|
foreach ($data as $fieldName => $value) { |
50
|
|
|
try { |
51
|
|
|
$reflectionProperty = new \ReflectionProperty($type, $fieldName); |
52
|
|
|
} catch (\ReflectionException $exception) { |
53
|
|
|
// Property does not exist on class: just ignore it. |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Property is not an UploadableField: just ignore it. |
58
|
|
|
if (!$this->uploadableHelper->isFieldConfigured($reflectionProperty)) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Value is empty: set it to null. |
63
|
|
|
if (empty($value)) { |
64
|
|
|
$data[$fieldName] = null; |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$data[$fieldName] = new UploadedBase64EncodedFile(new Base64EncodedFile($value), 'uploaded_name'); |
70
|
|
|
} catch (FileException $exception) { |
71
|
|
|
throw new NotNormalizableValueException($exception->getMessage()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->denormalizer->denormalize($data, $type, $format, $context); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
82
|
|
|
{ |
83
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $this->uploadableHelper->isConfigured($type); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function hasCacheableSupportsMethod(): bool |
87
|
|
|
{ |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|