14
14
use Symfony \Component \Serializer \Exception \BadMethodCallException ;
15
15
use Symfony \Component \Serializer \Exception \InvalidArgumentException ;
16
16
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
17
+ use Symfony \Component \Serializer \Serializer ;
17
18
use Symfony \Component \Serializer \SerializerAwareInterface ;
18
19
use Symfony \Component \Serializer \SerializerInterface ;
19
20
24
25
*
25
26
* @final
26
27
*/
27
- class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
28
+ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
28
29
{
29
- /**
30
- * @var SerializerInterface|DenormalizerInterface
31
- */
32
- private $ serializer ;
30
+ use DenormalizerAwareTrait;
33
31
34
32
/**
35
33
* {@inheritdoc}
36
34
*
37
35
* @throws NotNormalizableValueException
38
- *
39
- * @return array
40
36
*/
41
- public function denormalize ($ data , string $ type , string $ format = null , array $ context = [])
37
+ public function denormalize ($ data , string $ type , string $ format = null , array $ context = []): array
42
38
{
43
- if (null === $ this ->serializer ) {
44
- throw new BadMethodCallException ('Please set a serializer before calling denormalize()! ' );
39
+ if (null === $ this ->denormalizer ) {
40
+ throw new BadMethodCallException ('Please set a denormalizer before calling denormalize()! ' );
45
41
}
46
42
if (!\is_array ($ data )) {
47
43
throw new InvalidArgumentException ('Data expected to be an array, ' .get_debug_type ($ data ).' given. ' );
@@ -50,7 +46,6 @@ public function denormalize($data, string $type, string $format = null, array $c
50
46
throw new InvalidArgumentException ('Unsupported class: ' .$ type );
51
47
}
52
48
53
- $ serializer = $ this ->serializer ;
54
49
$ type = substr ($ type , 0 , -2 );
55
50
56
51
$ builtinType = isset ($ context ['key_type ' ]) ? $ context ['key_type ' ]->getBuiltinType () : null ;
@@ -59,7 +54,7 @@ public function denormalize($data, string $type, string $format = null, array $c
59
54
throw new NotNormalizableValueException (sprintf ('The type of the key "%s" must be "%s" ("%s" given). ' , $ key , $ builtinType , get_debug_type ($ key )));
60
55
}
61
56
62
- $ data [$ key ] = $ serializer ->denormalize ($ value , $ type , $ format , $ context );
57
+ $ data [$ key ] = $ this -> denormalizer ->denormalize ($ value , $ type , $ format , $ context );
63
58
}
64
59
65
60
return $ data ;
@@ -70,31 +65,37 @@ public function denormalize($data, string $type, string $format = null, array $c
70
65
*/
71
66
public function supportsDenormalization ($ data , string $ type , string $ format = null , array $ context = []): bool
72
67
{
73
- if (null === $ this ->serializer ) {
74
- throw new BadMethodCallException (sprintf ('The serializer needs to be set to allow "%s()" to be used. ' , __METHOD__ ));
68
+ if (null === $ this ->denormalizer ) {
69
+ throw new BadMethodCallException (sprintf ('The nested denormalizer needs to be set to allow "%s()" to be used. ' , __METHOD__ ));
75
70
}
76
71
77
72
return '[]' === substr ($ type , -2 )
78
- && $ this ->serializer ->supportsDenormalization ($ data , substr ($ type , 0 , -2 ), $ format , $ context );
73
+ && $ this ->denormalizer ->supportsDenormalization ($ data , substr ($ type , 0 , -2 ), $ format , $ context );
79
74
}
80
75
81
76
/**
82
77
* {@inheritdoc}
78
+ *
79
+ * @deprecated call setDenormalizer() instead
83
80
*/
84
81
public function setSerializer (SerializerInterface $ serializer )
85
82
{
86
83
if (!$ serializer instanceof DenormalizerInterface) {
87
84
throw new InvalidArgumentException ('Expected a serializer that also implements DenormalizerInterface. ' );
88
85
}
89
86
90
- $ this ->serializer = $ serializer ;
87
+ if (Serializer::class !== debug_backtrace ()[1 ]['class ' ] ?? null ) {
88
+ trigger_deprecation ('symfony/serializer ' , '5.3 ' , 'Calling "%s" is deprecated. Please call setDenormalizer() instead. ' );
89
+ }
90
+
91
+ $ this ->setDenormalizer ($ serializer );
91
92
}
92
93
93
94
/**
94
95
* {@inheritdoc}
95
96
*/
96
97
public function hasCacheableSupportsMethod (): bool
97
98
{
98
- return $ this ->serializer instanceof CacheableSupportsMethodInterface && $ this ->serializer ->hasCacheableSupportsMethod ();
99
+ return $ this ->denormalizer instanceof CacheableSupportsMethodInterface && $ this ->denormalizer ->hasCacheableSupportsMethod ();
99
100
}
100
101
}
0 commit comments