Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97741e2

Browse files
committed
extract skip null value test into a trait
1 parent 43c32d8 commit 97741e2

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
5050
*/
5151
public const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
5252

53-
const SKIP_NULL_VALUES = 'skip_null_values';
53+
/**
54+
* Flag to control whether fields with the value `null` should be output
55+
* when normalizing or omitted.
56+
*/
57+
public const SKIP_NULL_VALUES = 'skip_null_values';
5458

5559
public const MAX_DEPTH_HANDLER = 'max_depth_handler';
5660

@@ -128,38 +132,38 @@ public function normalize($object, $format = null, array $context = [])
128132
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
129133
$maxDepthHandler = $context[self::MAX_DEPTH_HANDLER] ?? $this->defaultContext[self::MAX_DEPTH_HANDLER] ?? $this->maxDepthHandler;
130134

131-
foreach ($attributes as $attribute) {
135+
foreach ($attributes as $attributeName) {
132136
$maxDepthReached = false;
133-
if (null !== $attributesMetadata && ($maxDepthReached = $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) && !$maxDepthHandler) {
137+
if (null !== $attributesMetadata && ($maxDepthReached = $this->isMaxDepthReached($attributesMetadata, $class, $attributeName, $context)) && !$maxDepthHandler) {
134138
continue;
135139
}
136140

137-
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
141+
$attributeValue = $this->getAttributeValue($object, $attributeName, $format, $context);
138142
if ($maxDepthReached) {
139-
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $context);
143+
$attributeValue = $maxDepthHandler($attributeValue, $object, $attributeName, $format, $context);
140144
}
141145

142146
/**
143147
* @var callable|null
144148
*/
145-
$callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? $this->callbacks[$attribute] ?? null;
149+
$callback = $context[self::CALLBACKS][$attributeName] ?? $this->defaultContext[self::CALLBACKS][$attributeName] ?? $this->callbacks[$attributeName] ?? null;
146150
if ($callback) {
147-
$attributeValue = $callback($attributeValue, $object, $attribute, $format, $context);
151+
$attributeValue = $callback($attributeValue, $object, $attributeName, $format, $context);
148152
}
149153

150154
if (null !== $attributeValue && !is_scalar($attributeValue)) {
151-
$stack[$attribute] = $attributeValue;
155+
$stack[$attributeName] = $attributeValue;
152156
}
153157

154-
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $context);
158+
$data = $this->updateData($data, $attributeName, $attributeValue, $class, $format, $context);
155159
}
156160

157-
foreach ($stack as $attribute => $attributeValue) {
161+
foreach ($stack as $attributeName => $attributeValue) {
158162
if (!$this->serializer instanceof NormalizerInterface) {
159-
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
163+
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attributeName));
160164
}
161165

162-
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)), $class, $format, $context);
166+
$data = $this->updateData($data, $attributeName, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attributeName)), $class, $format, $context);
163167
}
164168

165169
return $data;

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,6 @@ public function testExtraAttributesException()
164164
]);
165165
}
166166

167-
public function testSkipNullValues()
168-
{
169-
$dummy = new Dummy();
170-
$dummy->bar = 'present';
171-
172-
$normalizer = new ObjectNormalizer();
173-
$result = $normalizer->normalize($dummy, null, [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
174-
$this->assertSame(['bar' => 'present'], $result);
175-
}
176-
177167
public function testDeepObjectToPopulate()
178168
{
179169
$child = new DeepObjectPopulateChildDummy();

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Symfony\Component\Serializer\Tests\Normalizer\Features\MaxDepthTestTrait;
4444
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy;
4545
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
46+
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipNullValuesTestTrait;
4647
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
4748

4849
/**
@@ -59,6 +60,7 @@ class ObjectNormalizerTest extends TestCase
5960
use ConstructorArgumentsTestTrait;
6061
use TypeEnforcementTestTrait;
6162
use CallbacksTestTrait;
63+
use SkipNullValuesTestTrait;
6264

6365
/**
6466
* @var ObjectNormalizer
@@ -260,7 +262,7 @@ protected function getNormalizerForGroups(): ObjectNormalizer
260262
return $normalizer;
261263
}
262264

263-
protected function getDenormalizerForGroups(): DenormalizerInterface
265+
protected function getDenormalizerForGroups(): ObjectNormalizer
264266
{
265267
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
266268
return new ObjectNormalizer($classMetadataFactory);
@@ -271,7 +273,7 @@ protected function getDenormalizerForObjectToPopulate(): DenormalizerInterface
271273
return new ObjectNormalizer();
272274
}
273275

274-
protected function getNormalizerForMaxDepth(): NormalizerInterface
276+
protected function getNormalizerForMaxDepth(): ObjectNormalizer
275277
{
276278
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
277279
$normalizer = new ObjectNormalizer($classMetadataFactory);
@@ -281,7 +283,7 @@ protected function getNormalizerForMaxDepth(): NormalizerInterface
281283
return $normalizer;
282284
}
283285

284-
protected function getDenormalizerForConstructArguments(): DenormalizerInterface
286+
protected function getDenormalizerForConstructArguments(): ObjectNormalizer
285287
{
286288
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
287289
$denormalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
@@ -291,7 +293,7 @@ protected function getDenormalizerForConstructArguments(): DenormalizerInterface
291293
return $denormalizer;
292294
}
293295

294-
protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface
296+
protected function getDenormalizerForTypeEnforcement(): ObjectNormalizer
295297
{
296298
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
297299
$normalizer = new ObjectNormalizer(null, null, null, $extractor);
@@ -300,6 +302,11 @@ protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface
300302
return $normalizer;
301303
}
302304

305+
protected function getNormalizerForSkipNullValues(): ObjectNormalizer
306+
{
307+
return new ObjectNormalizer();
308+
}
309+
303310
public function testGroupsNormalizeWithNameConverter()
304311
{
305312
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
 (0)