|
13 | 13 |
|
14 | 14 | namespace Sylius\ImportExport\Denormalizer; |
15 | 15 |
|
16 | | -use Doctrine\ORM\EntityManagerInterface; |
17 | 16 | use Sylius\ImportExport\Serializer\ExportAwareItemNormalizer; |
| 17 | +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; |
| 18 | +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
18 | 19 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
19 | | -use Webmozart\Assert\Assert; |
20 | 20 |
|
21 | 21 | final readonly class DefaultResourceDenormalizer implements ResourceDenormalizerInterface |
22 | 22 | { |
23 | 23 | public function __construct( |
24 | 24 | private DenormalizerInterface $denormalizer, |
25 | | - private RelationResolverInterface $relationResolver, |
26 | | - private EntityManagerInterface $entityManager, |
27 | 25 | ) { |
28 | 26 | } |
29 | 27 |
|
30 | 28 | public function denormalize(array $data, string $resourceClass): object |
31 | 29 | { |
32 | | - $metadata = $this->entityManager->getClassMetadata($resourceClass); |
33 | | - $processedData = []; |
34 | | - |
35 | | - foreach ($data as $field => $value) { |
36 | | - if (null === $value || (is_string($value) && '' === $value)) { |
37 | | - continue; |
38 | | - } |
39 | | - |
40 | | - if ($metadata->hasAssociation($field)) { |
41 | | - $associationMapping = $metadata->getAssociationMapping($field); |
42 | | - $targetEntity = $associationMapping['targetEntity']; |
43 | | - Assert::string($targetEntity); |
44 | | - |
45 | | - if ($metadata->isCollectionValuedAssociation($field)) { |
46 | | - if (is_array($value) && !empty($value)) { |
47 | | - $resolvedEntities = $this->relationResolver->resolveCollection($targetEntity, $value); |
48 | | - $filteredEntities = array_filter($resolvedEntities); // Remove null values |
49 | | - if (!empty($filteredEntities)) { |
50 | | - $processedData[$field] = $filteredEntities; |
51 | | - } |
52 | | - // Don't add the field at all if no entities found |
53 | | - } |
54 | | - } else { |
55 | | - if (is_array($value) && !empty($value)) { |
56 | | - $resolvedEntity = $this->relationResolver->resolveEntity($targetEntity, $value); |
57 | | - if (null !== $resolvedEntity) { |
58 | | - $processedData[$field] = $resolvedEntity; |
59 | | - } |
60 | | - // Don't add the field at all if entity not found |
61 | | - } |
62 | | - } |
63 | | - } elseif (is_array($value)) { |
64 | | - $processedData[$field] = $this->processNestedArray($value); |
65 | | - } else { |
66 | | - $processedData[$field] = $value; |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - // Ensure all entity relationships are properly managed before denormalization |
71 | | - foreach ($processedData as $field => $value) { |
72 | | - if ($metadata->hasAssociation($field)) { |
73 | | - if ($metadata->isCollectionValuedAssociation($field) && is_array($value)) { |
74 | | - foreach ($value as $entity) { |
75 | | - if (is_object($entity) && !$this->entityManager->contains($entity)) { |
76 | | - throw new \RuntimeException(sprintf( |
77 | | - 'Entity %s in collection %s is not managed by EntityManager', |
78 | | - get_class($entity), |
79 | | - $field |
80 | | - )); |
81 | | - } |
82 | | - } |
83 | | - } elseif (is_object($value) && !$this->entityManager->contains($value)) { |
84 | | - throw new \RuntimeException(sprintf( |
85 | | - 'Entity %s in field %s is not managed by EntityManager', |
86 | | - get_class($value), |
87 | | - $field |
88 | | - )); |
89 | | - } |
90 | | - } |
91 | | - } |
92 | | - |
93 | 30 | return $this->denormalizer->denormalize( |
94 | | - $processedData, |
| 31 | + $data, |
95 | 32 | $resourceClass, |
96 | 33 | null, |
97 | | - [ExportAwareItemNormalizer::EXPORT_CONTEXT_KEY => true] |
| 34 | + [ |
| 35 | + ExportAwareItemNormalizer::EXPORT_CONTEXT_KEY => true, |
| 36 | + DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s', |
| 37 | + AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true, |
| 38 | + ], |
98 | 39 | ); |
99 | 40 | } |
100 | 41 |
|
101 | | - private function processNestedArray(array $data): array |
102 | | - { |
103 | | - return $data; |
104 | | - } |
105 | | - |
106 | 42 | public function supports(string $resourceClass): bool |
107 | 43 | { |
108 | 44 | return true; |
|
0 commit comments