Skip to content

Commit 4e01c46

Browse files
committed
array to object normalizer update
1 parent b2ffdba commit 4e01c46

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

src/Normalizers/ArrayToObjectNormalizer.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,53 @@
77
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
88
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
99
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10-
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
11-
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
12-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1310

14-
class ArrayToObjectNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
11+
class ArrayToObjectNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
1512
{
1613
use DenormalizerAwareTrait;
17-
use NormalizerAwareTrait;
1814

1915
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
2016
{
21-
return (object) $data;
17+
if (is_array($data) && $type === 'object') {
18+
return (object) $data;
19+
}
20+
21+
if (class_exists($type)) {
22+
$reflection = new \ReflectionClass($type);
23+
24+
foreach ($data as $key => $value) {
25+
if (is_array($value) && $this->shouldBeObject($reflection, $key)) {
26+
$data[$key] = (object) $value;
27+
}
28+
}
29+
30+
return $this->denormalizer->denormalize($data, $type, $format, $context + [__CLASS__ => true]);
31+
}
32+
33+
return $data;
2234
}
2335

2436
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
2537
{
26-
return is_array($data);
38+
if (isset($context[__CLASS__])) {
39+
return false;
40+
}
41+
42+
if ($type === 'object' && is_array($data)) {
43+
return true;
44+
}
45+
46+
if (class_exists($type) && is_array($data)) {
47+
$reflection = new \ReflectionClass($type);
48+
49+
foreach ($data as $key => $value) {
50+
if (is_array($value) && $this->shouldBeObject($reflection, $key)) {
51+
return true;
52+
}
53+
}
54+
}
55+
56+
return false;
2757
}
2858

2959
public function getSupportedTypes(?string $format): array
@@ -34,13 +64,20 @@ public function getSupportedTypes(?string $format): array
3464
];
3565
}
3666

37-
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
67+
private function shouldBeObject(\ReflectionClass $reflection, string $propertyName): bool
3868
{
39-
return (array) $object;
40-
}
69+
try {
70+
$property = $reflection->getProperty($propertyName);
71+
$type = $property->getType();
4172

42-
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
43-
{
44-
return true;
73+
if ($type instanceof \ReflectionNamedType) {
74+
return $type->getName() === 'object';
75+
}
76+
} catch (\ReflectionException $e) {
77+
// Property doesn't exist
78+
return false;
79+
}
80+
81+
return false;
4582
}
4683
}

0 commit comments

Comments
 (0)