23
23
24
24
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
25
25
use ApiPlatform \Core \Serializer \AbstractItemNormalizer ;
26
+ use ApiPlatform \Core \Serializer \ItemNormalizer as BaseItemNormalizer ;
26
27
27
28
/**
28
29
* GraphQL normalizer.
29
30
*
30
31
* @author Kévin Dunglas <[email protected] >
31
32
*/
32
- final class ItemNormalizer extends AbstractItemNormalizer
33
+ final class ItemNormalizer extends BaseItemNormalizer
33
34
{
34
35
const FORMAT = 'graphql ' ;
35
36
const ITEM_KEY = '#item ' ;
36
37
38
+ /**
39
+ * {@inheritdoc}
40
+ */
41
+ public function supportsNormalization ($ data , $ format = null )
42
+ {
43
+ return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
44
+ }
45
+
37
46
/**
38
47
* {@inheritdoc}
39
48
*/
40
49
public function normalize ($ object , $ format = null , array $ context = [])
41
50
{
42
- $ data = parent ::normalize ($ object , $ format , $ context );
51
+ $ data = AbstractItemNormalizer ::normalize ($ object , $ format , $ context );
43
52
$ data [self ::ITEM_KEY ] = serialize ($ object ); // calling serialize prevent weird normalization process done by Webonyx's GraphQL PHP
44
53
45
54
return $ data ;
@@ -57,16 +66,35 @@ protected function normalizeCollectionOfRelations(PropertyMetadata $propertyMeta
57
66
/**
58
67
* {@inheritdoc}
59
68
*/
60
- public function supportsNormalization ($ data , $ format = null )
69
+ public function supportsDenormalization ($ data, $ type , $ format = null )
61
70
{
62
- return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
71
+ return self ::FORMAT === $ format && parent ::supportsDenormalization ($ data, $ type , $ format );
63
72
}
64
73
65
74
/**
66
75
* {@inheritdoc}
67
76
*/
68
- public function supportsDenormalization ( $ data , $ type , $ format = null )
77
+ protected function getAllowedAttributes ( $ classOrObject , array $ context , $ attributesAsString = false )
69
78
{
70
- return self ::FORMAT === $ format && parent ::supportsDenormalization ($ data , $ type , $ format );
79
+ $ allowedAttributes = parent ::getAllowedAttributes ($ classOrObject , $ context , $ attributesAsString );
80
+
81
+ if (($ context ['api_denormalize ' ] ?? false ) && false !== ($ indexId = array_search ('id ' , $ allowedAttributes , true ))) {
82
+ $ allowedAttributes [] = '_id ' ;
83
+ array_splice ($ allowedAttributes , $ indexId , 1 );
84
+ }
85
+
86
+ return $ allowedAttributes ;
87
+ }
88
+
89
+ /**
90
+ * {@inheritdoc}
91
+ */
92
+ protected function setAttributeValue ($ object , $ attribute , $ value , $ format = null , array $ context = [])
93
+ {
94
+ if ('_id ' === $ attribute ) {
95
+ $ attribute = 'id ' ;
96
+ }
97
+
98
+ parent ::setAttributeValue ($ object , $ attribute , $ value , $ format , $ context );
71
99
}
72
100
}
0 commit comments