12
12
namespace Symfony \Component \Serializer \Normalizer ;
13
13
14
14
use Symfony \Component \PropertyAccess \PropertyAccess ;
15
- use Symfony \Component \PropertyAccess \PropertyAccessor ;
15
+ use Symfony\Component \PropertyAccess \PropertyAccessorInterface ;
16
16
use Symfony \Component \Serializer \SerializerAwareInterface ;
17
- use Symfony \Component \Serializer \SerializerInterface ;
17
+ use Symfony \Component \Serializer \SerializerAwareTrait ;
18
18
19
19
final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
20
20
{
21
- const UNWRAP_PATH = ' unwrap_path ' ;
21
+ use SerializerAwareTrait ;
22
22
23
- /**
24
- * @var SerializerInterface|DenormalizerInterface
25
- */
26
- private $ serializer ;
23
+ const UNWRAP_PATH = 'unwrap_path ' ;
27
24
28
25
/**
29
26
* @var PropertyAccess
30
27
*/
31
- private $ propertyAccess ;
28
+ private $ propertyAccessor ;
32
29
33
- public function __construct (PropertyAccessor $ propertyAccess )
30
+ public function __construct (PropertyAccessorInterface $ propertyAccessor = null )
34
31
{
35
- $ this ->propertyAccess = $ propertyAccess ;
32
+ $ this ->propertyAccessor = $ propertyAccessor ?? PropertyAccess:: createPropertyAccessor () ;
36
33
}
37
34
38
35
/**
39
36
* {@inheritdoc}
40
37
*/
41
- public function denormalize ($ data , $ class , $ format = null , array $ context = [])
38
+ public function denormalize ($ data , $ class , string $ format = null , array $ context = [])
42
39
{
43
40
$ propertyPath = $ context [self ::UNWRAP_PATH ];
44
41
$ context ['unwrapped ' ] = true ;
45
42
46
43
if ($ propertyPath ) {
47
- if (!$ this ->propertyAccess ->isReadable ($ data , $ propertyPath )) {
44
+ if (!$ this ->propertyAccessor ->isReadable ($ data , $ propertyPath )) {
48
45
return null ;
49
46
}
50
47
51
- $ data = $ this ->propertyAccess ->getValue ($ data , $ propertyPath );
48
+ $ data = $ this ->propertyAccessor ->getValue ($ data , $ propertyPath );
52
49
}
53
50
54
51
return $ this ->serializer ->denormalize ($ data , $ class , $ format , $ context );
@@ -57,19 +54,11 @@ public function denormalize($data, $class, $format = null, array $context = [])
57
54
/**
58
55
* {@inheritdoc}
59
56
*/
60
- public function supportsDenormalization ($ data , $ type , $ format = null , array $ context = [])
57
+ public function supportsDenormalization ($ data , $ type , string $ format = null , array $ context = [])
61
58
{
62
59
return \array_key_exists (self ::UNWRAP_PATH , $ context ) && !isset ($ context ['unwrapped ' ]);
63
60
}
64
61
65
- /**
66
- * {@inheritdoc}
67
- */
68
- public function setSerializer (SerializerInterface $ serializer )
69
- {
70
- $ this ->serializer = $ serializer ;
71
- }
72
-
73
62
/**
74
63
* {@inheritdoc}
75
64
*/
0 commit comments