12
12
namespace Symfony \Component \Serializer \NameConverter ;
13
13
14
14
use Symfony \Component \Serializer \Mapping \Factory \ClassMetadataFactoryInterface ;
15
+ use Symfony \Component \Serializer \Normalizer \AbstractNormalizer ;
15
16
16
17
/**
17
18
* @author Fabien Bourigault <[email protected] >
@@ -25,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
25
26
*/
26
27
private $ fallbackNameConverter ;
27
28
28
- private static $ normalizeCache = [];
29
+ private $ normalizeCache = [];
29
30
30
- private static $ denormalizeCache = [];
31
+ private $ denormalizeCache = [];
31
32
32
- private static $ attributesMetadataCache = [];
33
+ private $ attributesMetadataCache = [];
33
34
34
35
public function __construct (ClassMetadataFactoryInterface $ metadataFactory , NameConverterInterface $ fallbackNameConverter = null )
35
36
{
@@ -46,11 +47,11 @@ public function normalize($propertyName, string $class = null, string $format =
46
47
return $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
47
48
}
48
49
49
- if (!isset (self :: $ normalizeCache [$ class ][$ propertyName ])) {
50
- self :: $ normalizeCache [$ class ][$ propertyName ] = $ this ->getCacheValueForNormalization ($ propertyName , $ class );
50
+ if (!isset ($ this -> normalizeCache [$ class ][$ propertyName ])) {
51
+ $ this -> normalizeCache [$ class ][$ propertyName ] = $ this ->getCacheValueForNormalization ($ propertyName , $ class );
51
52
}
52
53
53
- return self :: $ normalizeCache [$ class ][$ propertyName ] ?? $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
54
+ return $ this -> normalizeCache [$ class ][$ propertyName ] ?? $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
54
55
}
55
56
56
57
/**
@@ -62,11 +63,11 @@ public function denormalize($propertyName, string $class = null, string $format
62
63
return $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
63
64
}
64
65
65
- if (!isset (self :: $ denormalizeCache [$ class ][$ propertyName ])) {
66
- self :: $ denormalizeCache [$ class ][$ propertyName ] = $ this ->getCacheValueForDenormalization ($ propertyName , $ class );
66
+ if (!isset ($ this -> denormalizeCache [$ class ][$ propertyName ])) {
67
+ $ this -> denormalizeCache [$ class ][$ propertyName ] = $ this ->getCacheValueForDenormalization ($ propertyName , $ class, $ context );
67
68
}
68
69
69
- return self :: $ denormalizeCache [$ class ][$ propertyName ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
70
+ return $ this -> denormalizeCache [$ class ][$ propertyName ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
70
71
}
71
72
72
73
private function getCacheValueForNormalization ($ propertyName , string $ class )
@@ -88,21 +89,21 @@ private function normalizeFallback($propertyName, string $class = null, string $
88
89
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->normalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
89
90
}
90
91
91
- private function getCacheValueForDenormalization ($ propertyName , string $ class )
92
+ private function getCacheValueForDenormalization ($ propertyName , string $ class, $ context )
92
93
{
93
- if (!isset (self :: $ attributesMetadataCache [$ class ])) {
94
- self :: $ attributesMetadataCache [$ class ] = $ this ->getCacheValueForAttributesMetadata ($ class );
94
+ if (!isset ($ this -> attributesMetadataCache [$ class ])) {
95
+ $ this -> attributesMetadataCache [$ class ] = $ this ->getCacheValueForAttributesMetadata ($ class, $ context );
95
96
}
96
97
97
- return self :: $ attributesMetadataCache [$ class ][$ propertyName ] ?? null ;
98
+ return $ this -> attributesMetadataCache [$ class ][$ propertyName ] ?? null ;
98
99
}
99
100
100
101
private function denormalizeFallback ($ propertyName , string $ class = null , string $ format = null , array $ context = [])
101
102
{
102
103
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->denormalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
103
104
}
104
105
105
- private function getCacheValueForAttributesMetadata (string $ class ): array
106
+ private function getCacheValueForAttributesMetadata (string $ class, $ context ): array
106
107
{
107
108
if (!$ this ->metadataFactory ->hasMetadataFor ($ class )) {
108
109
return [];
@@ -116,6 +117,14 @@ private function getCacheValueForAttributesMetadata(string $class): array
116
117
continue ;
117
118
}
118
119
120
+ $ groups = $ metadata ->getGroups ();
121
+ if (!$ groups && ($ context [AbstractNormalizer::GROUPS ] ?? [])) {
122
+ continue ;
123
+ }
124
+ if ($ groups && !array_intersect ($ groups , $ context [AbstractNormalizer::GROUPS ] ?? [])) {
125
+ continue ;
126
+ }
127
+
119
128
$ cache [$ metadata ->getSerializedName ()] = $ name ;
120
129
}
121
130
0 commit comments