7
7
use Symfony \Component \Serializer \Normalizer \DenormalizerAwareInterface ;
8
8
use Symfony \Component \Serializer \Normalizer \DenormalizerAwareTrait ;
9
9
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 ;
13
10
14
- class ArrayToObjectNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
11
+ class ArrayToObjectNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
15
12
{
16
13
use DenormalizerAwareTrait;
17
- use NormalizerAwareTrait;
18
14
19
15
public function denormalize (mixed $ data , string $ type , ?string $ format = null , array $ context = []): mixed
20
16
{
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 ;
22
34
}
23
35
24
36
public function supportsDenormalization (mixed $ data , string $ type , ?string $ format = null , array $ context = []): bool
25
37
{
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 ;
27
57
}
28
58
29
59
public function getSupportedTypes (?string $ format ): array
@@ -34,13 +64,20 @@ public function getSupportedTypes(?string $format): array
34
64
];
35
65
}
36
66
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
38
68
{
39
- return (array ) $ object ;
40
- }
69
+ try {
70
+ $ property = $ reflection ->getProperty ($ propertyName );
71
+ $ type = $ property ->getType ();
41
72
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 ;
45
82
}
46
83
}
0 commit comments