|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Serializer\Normalizer; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
17
|
|
|
use Ramsey\Uuid\Uuid; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReader; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Factory\Uploadable\MediaObjectFactory; |
|
20
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\DataUriFile; |
|
21
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\UploadedDataUriFile; |
|
22
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
|
24
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
25
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
|
26
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
27
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
|
28
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
|
29
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
|
30
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
|
31
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
|
32
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @author Vincent Chalamon <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
final class UploadableNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, ContextAwareNormalizerInterface, NormalizerAwareInterface |
|
38
|
|
|
{ |
|
39
|
|
|
use ClassMetadataTrait; |
|
40
|
|
|
|
|
41
|
|
|
use DenormalizerAwareTrait; |
|
42
|
|
|
use NormalizerAwareTrait; |
|
43
|
|
|
|
|
44
|
|
|
private const ALREADY_CALLED = 'UPLOADABLE_NORMALIZER_ALREADY_CALLED'; |
|
45
|
|
|
|
|
46
|
|
|
private MediaObjectFactory $mediaObjectFactory; |
|
47
|
|
|
private UploadableAnnotationReader $annotationReader; |
|
48
|
|
|
|
|
49
|
7 |
|
public function __construct(MediaObjectFactory $mediaObjectFactory, UploadableAnnotationReader $annotationReader, ManagerRegistry $registry) |
|
50
|
|
|
{ |
|
51
|
7 |
|
$this->mediaObjectFactory = $mediaObjectFactory; |
|
52
|
7 |
|
$this->annotationReader = $annotationReader; |
|
53
|
7 |
|
$this->initRegistry($registry); |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $this->annotationReader->isConfigured($type); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($data as $fieldName => $value) { |
|
72
|
|
|
try { |
|
73
|
|
|
$reflectionProperty = new \ReflectionProperty($type, $fieldName); |
|
74
|
|
|
} catch (\ReflectionException $exception) { |
|
75
|
|
|
// Property does not exist on class: just ignore it. |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Property is not an UploadableField: just ignore it. |
|
80
|
|
|
if (!$this->annotationReader->isFieldConfigured($reflectionProperty)) { |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Value is empty: set it to null. |
|
85
|
|
|
if (empty($value)) { |
|
86
|
|
|
$data[$fieldName] = null; |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$file = new DataUriFile($value); |
|
92
|
|
|
$data[$fieldName] = new UploadedDataUriFile($file, Uuid::uuid4() . '.' . $file->getExtension()); |
|
93
|
|
|
} catch (FileException $exception) { |
|
94
|
|
|
throw new NotNormalizableValueException($exception->getMessage()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->denormalizer->denormalize($data, $type, $format, $context); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return !isset($context[self::ALREADY_CALLED]) && |
|
104
|
|
|
\is_object($data) && |
|
105
|
|
|
!$data instanceof \Traversable && |
|
106
|
|
|
$this->annotationReader->isConfigured($data); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function normalize($object, $format = null, array $context = []) |
|
110
|
|
|
{ |
|
111
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
112
|
|
|
|
|
113
|
|
|
$mediaObjects = $this->mediaObjectFactory->createMediaObjects($object); |
|
114
|
|
|
if ($mediaObjects) { |
|
115
|
|
|
$mediaObjects = $this->normalizer->normalize( |
|
116
|
|
|
$mediaObjects, |
|
117
|
|
|
$format, |
|
118
|
|
|
[ |
|
119
|
|
|
'jsonld_embed_context' => true, |
|
120
|
|
|
'skip_null_values' => $context['skip_null_values'] ?? false, |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
$context[MetadataNormalizer::METADATA_CONTEXT]['media_objects'] = $mediaObjects; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$fieldConfigurations = $this->annotationReader->getConfiguredProperties($object, true); |
|
127
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
128
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
129
|
|
|
foreach ($fieldConfigurations as $fileField => $fieldConfiguration) { |
|
130
|
|
|
$propertyAccessor->setValue($object, $fileField, null); |
|
131
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function hasCacheableSupportsMethod(): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|