44How to Create your Custom Context Builder
55=========================================
66
7+ .. versionadded :: 6.1
8+
9+ Context builders were introduced in Symfony 6.1.
10+
711The :doc: `Serializer Component </components/serializer >` uses Normalizers
812and Encoders to transform any data to any data-structure (e.g. JSON).
9- That serialization process could be configured thanks to a
13+ That serialization process can be configured thanks to a
1014:ref: `serialization context <serializer-context >`, which can be built thanks to
1115:ref: `context builders <component-serializer-context-builders >`.
1216
13- Each built-in normalizer/encoder has its related context builder.
14- But, as an example, you may want to use custom context values
15- for your :doc: `custom normalizers </serializer/custom_normalizer >`
16- and create a custom context builder related to them.
17+ Each built-in normalizer/encoder has its related context builder. However, you
18+ may want to create a custom context builder for your
19+ :doc: `custom normalizers </serializer/custom_normalizer >`.
1720
18- Creating a new context builder
21+ Creating a new Context Builder
1922------------------------------
2023
2124Let's imagine that you want to handle date denormalization differently if they
22- are coming from a legacy system, by converting them to ``null `` if the serialized
25+ are coming from a legacy system, by converting dates to ``null `` if the serialized
2326value is ``0000-00-00 ``. To do that you'll first have to create your normalizer::
2427
2528 // src/Serializer/ZeroDateTimeDenormalizer.php
@@ -51,14 +54,13 @@ value is ``0000-00-00``. To do that you'll first have to create your normalizer:
5154 }
5255 }
5356
54- You'll therefore be able to cast zero-ish dates to ``null `` during denormalization::
57+ Now you can cast zero-ish dates to ``null `` during denormalization::
5558
5659 $legacyData = '{"updatedAt": "0000-00-00"}';
57-
5860 $serializer->deserialize($legacyData, MyModel::class, 'json', ['zero_datetime_to_null' => true]);
5961
60- Then, if you don't want other developers to have to remind the precise ``zero_date_to_null `` context key,
61- you can create a dedicated context builder::
62+ Now, to avoid having to remember about this specific ``zero_date_to_null ``
63+ context key, you can create a dedicated context builder::
6264
6365 // src/Serializer/LegacyContextBuilder
6466 namespace App\Serializer;
@@ -75,7 +77,7 @@ you can create a dedicated context builder::
7577 }
7678 }
7779
78- And finally use it to build the serialization context::
80+ And finally, use it to build the serialization context::
7981
8082 $legacyData = '{"updatedAt": "0000-00-00"}';
8183
0 commit comments