@@ -37,14 +37,19 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
37
37
const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement ' ;
38
38
const SKIP_NULL_VALUES = 'skip_null_values ' ;
39
39
40
- private $ propertyTypeExtractor ;
41
- private $ typesCache = array ();
42
- private $ attributesCache = array ();
43
-
44
40
/**
45
41
* @var callable|null
46
42
*/
47
- private $ maxDepthHandler ;
43
+ protected $ maxDepthHandler ;
44
+
45
+ /**
46
+ * @var bool
47
+ */
48
+ protected $ skipNullValues = false ;
49
+
50
+ private $ propertyTypeExtractor ;
51
+ private $ typesCache = array ();
52
+ private $ attributesCache = array ();
48
53
49
54
/**
50
55
* @var ClassDiscriminatorResolverInterface|null
@@ -221,6 +226,14 @@ public function setMaxDepthHandler(?callable $handler): void
221
226
$ this ->maxDepthHandler = $ handler ;
222
227
}
223
228
229
+ /**
230
+ * Configures the normalizer to skip null values.
231
+ */
232
+ public function skipNullValues ()
233
+ {
234
+ $ this ->skipNullValues = true ;
235
+ }
236
+
224
237
/**
225
238
* {@inheritdoc}
226
239
*/
@@ -403,7 +416,7 @@ private function getTypes(string $currentClass, string $attribute)
403
416
*/
404
417
private function updateData (array $ data , string $ attribute , $ attributeValue , string $ class , ?string $ format , array $ context ): array
405
418
{
406
- if (null === $ attributeValue && ($ context [self ::SKIP_NULL_VALUES ] ?? false )) {
419
+ if (null === $ attributeValue && ($ this -> skipNullValues || ( $ context [self ::SKIP_NULL_VALUES ] ?? false ) )) {
407
420
return $ data ;
408
421
}
409
422
0 commit comments