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\AttributeReader\UploadableAttributeReader; |
19
|
|
|
use Silverback\ApiComponentsBundle\Factory\Uploadable\MediaObjectFactory; |
20
|
|
|
use Silverback\ApiComponentsBundle\Helper\Uploadable\UploadableFileManager; |
21
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\DataUriFile; |
22
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\UploadedDataUriFile; |
23
|
|
|
use Silverback\ApiComponentsBundle\Serializer\ResourceMetadata\ResourceMetadataProvider; |
24
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
25
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
26
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
27
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
28
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
29
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
30
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
31
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
32
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
33
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
34
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
35
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @author Vincent Chalamon <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
final class UploadableNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface |
41
|
|
|
{ |
42
|
|
|
use ClassMetadataTrait; |
43
|
|
|
|
44
|
|
|
use DenormalizerAwareTrait; |
45
|
|
|
use NormalizerAwareTrait; |
46
|
|
|
|
47
|
|
|
private const ALREADY_CALLED = 'UPLOADABLE_NORMALIZER_ALREADY_CALLED'; |
48
|
|
|
|
49
|
|
|
private PropertyAccessor $propertyAccessor; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
private MediaObjectFactory $mediaObjectFactory, |
53
|
|
|
private UploadableAttributeReader $annotationReader, |
54
|
|
|
private UploadableFileManager $uploadableFileManager, |
55
|
|
|
ManagerRegistry $registry, |
56
|
|
|
private ResourceMetadataProvider $resourceMetadataProvider |
57
|
|
|
) { |
58
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
59
|
|
|
$this->initRegistry($registry); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
66
|
|
|
{ |
67
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $this->annotationReader->isConfigured($type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function denormalize($data, $type, $format = null, array $context = []): mixed |
74
|
|
|
{ |
75
|
|
|
$context[self::ALREADY_CALLED] = true; |
76
|
|
|
|
77
|
|
|
foreach ($data as $fieldName => $value) { |
78
|
|
|
try { |
79
|
|
|
$reflectionProperty = new \ReflectionProperty($type, $fieldName); |
80
|
|
|
} catch (\ReflectionException $exception) { |
81
|
|
|
// Property does not exist on class: just ignore it. |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Property is not an UploadableField: just ignore it. |
86
|
|
|
if (!$this->annotationReader->isFieldConfigured($reflectionProperty)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Value is empty: set it to null. Might be blank string |
91
|
|
|
if (empty($value)) { |
92
|
|
|
$fieldConfig = $this->annotationReader->getPropertyConfiguration($reflectionProperty); |
93
|
|
|
$this->uploadableFileManager->addDeletedField($fieldConfig->property); |
94
|
|
|
$data[$fieldName] = null; |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$file = new DataUriFile($value); |
100
|
|
|
$data[$fieldName] = new UploadedDataUriFile($file, Uuid::uuid4() . '.' . $file->getExtension()); |
101
|
|
|
} catch (FileException $exception) { |
102
|
|
|
throw new NotNormalizableValueException($exception->getMessage()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->denormalizer->denormalize($data, $type, $format, $context); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
110
|
|
|
{ |
111
|
|
|
if (!\is_object($data) || $data instanceof \Traversable) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
116
|
|
|
$context[self::ALREADY_CALLED] = []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
$id = $this->propertyAccessor->getValue($data, 'id'); |
121
|
|
|
} catch (NoSuchPropertyException $e) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return !\in_array($id, $context[self::ALREADY_CALLED], true) |
126
|
|
|
&& $this->annotationReader->isConfigured($data); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
130
|
|
|
{ |
131
|
|
|
$context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id'); |
132
|
|
|
|
133
|
|
|
$mediaObjects = $this->mediaObjectFactory->createMediaObjects($object); |
134
|
|
|
if ($mediaObjects) { |
135
|
|
|
$mediaObjects = $this->normalizer->normalize( |
136
|
|
|
$mediaObjects, |
137
|
|
|
$format, |
138
|
|
|
[ |
139
|
|
|
'jsonld_embed_context' => true, |
140
|
|
|
'skip_null_values' => $context['skip_null_values'] ?? false, |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object); |
145
|
|
|
$resourceMetadata->setMediaObjects($mediaObjects); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$fieldConfigurations = $this->annotationReader->getConfiguredProperties($object, true); |
149
|
|
|
$classMetadata = $this->getClassMetadata($object); |
150
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
151
|
|
|
foreach ($fieldConfigurations as $fileField => $fieldConfiguration) { |
152
|
|
|
$propertyAccessor->setValue($object, $fileField, null); |
153
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
return ['object' => false]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.