1111
1212namespace Symfony \Component \Serializer \Dumper ;
1313
14- use Symfony \Component \Serializer \NormalizerInterface ;
15- use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
1614use Symfony \Component \Serializer \Mapping \Factory \ClassMetadataFactoryInterface ;
15+ use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
16+ use Symfony \Component \Serializer \NormalizerInterface ;
1717
1818/**
1919 * @author Guilhem Niot <guilhem.niot@gmail.com>
@@ -61,7 +61,7 @@ class {$context['class']} implements NormalizerInterface, NormalizerAwareInterfa
6161
6262 use CircularReferenceTrait, NormalizerAwareTrait;
6363
64- public function __construct(array \$defaultContext)
64+ public function __construct(array \$defaultContext = array() )
6565 {
6666 \$this->defaultContext = array_merge( \$this->defaultContext, \$defaultContext);
6767 }
@@ -91,7 +91,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
9191 $ code = <<<EOL
9292
9393 if ( \$this->isCircularReference( \$object, \$context)) {
94- return \$this->handleCircularReference( \$object);
94+ return \$this->handleCircularReference( \$object, \$ format, \$ context );
9595 }
9696
9797 \$groups = isset( \$context[ObjectNormalizer::GROUPS]) && is_array( \$context[ObjectNormalizer::GROUPS]) ? \$context[ObjectNormalizer::GROUPS] : null;
@@ -113,10 +113,9 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
113113 }
114114
115115 if ($ maxDepthCode ) {
116- $ maxDepthKey = ObjectNormalizer::ENABLE_MAX_DEPTH ;
117116 $ code .= <<<EOL
118117
119- if ( \$context[ $ maxDepthKey ] ?? \$this->defaultContext[ $ maxDepthKey ]) { {$ maxDepthCode }
118+ if ( \$context[ObjectNormalizer::ENABLE_MAX_DEPTH ] ?? \$this->defaultContext[ObjectNormalizer::ENABLE_MAX_DEPTH ]) { {$ maxDepthCode }
120119 }
121120
122121EOL ;
@@ -125,7 +124,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
125124 foreach ($ attributesMetadata as $ attributeMetadata ) {
126125 $ code .= <<<EOL
127126
128- \$attributes = \$context['attributes' ] ?? $ this ->defaultContext ['attributes' ] ?? null
127+ \$attributes = \$context[ObjectNormalizer::ATTRIBUTES ] ?? \ $this->defaultContext[ObjectNormalizer::ATTRIBUTES ] ?? null;
129128 if ((null === \$groups
130129EOL ;
131130
@@ -134,7 +133,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
134133 }
135134 $ code .= ') ' ;
136135
137- $ code .= " && (isset( \$attributes[' {$ attributeMetadata ->name }']) || (is_array( \$attributes) && in_array(' {$ attributeMetadata ->name }', \$attributes, true))) " ;
136+ $ code .= " && (null === \$ attributes || isset( \$attributes[' {$ attributeMetadata ->name }']) || (is_array( \$attributes) && in_array(' {$ attributeMetadata ->name }', \$attributes, true))) " ;
138137
139138 if (null !== $ maxDepth = $ attributeMetadata ->getMaxDepth ()) {
140139 $ key = sprintf (ObjectNormalizer::DEPTH_KEY_PATTERN , $ reflectionClass ->name , $ attributeMetadata ->name );
@@ -151,10 +150,10 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
151150 \$output[' {$ attributeMetadata ->name }'] = \$value;
152151 } else {
153152 \$subContext = \$context;
154- if (isset( \$context[' attributes'] [' {$ attributeMetadata ->name }'])) {
155- \$subContext['attributes' ] = \$context[' attributes'] [' {$ attributeMetadata ->name }'];
153+ if (isset( \$attributes[' {$ attributeMetadata ->name }'])) {
154+ \$subContext[ObjectNormalizer::ATTRIBUTES ] = \$attributes[' {$ attributeMetadata ->name }'];
156155 } else {
157- unset( \$subContext['attributes' ]);
156+ unset( \$subContext[ObjectNormalizer::ATTRIBUTES ]);
158157 }
159158
160159 \$output[' {$ attributeMetadata ->name }'] = \$this->normalizer->normalize( \$value, \$format, \$subContext);
0 commit comments