8000 Tweaks and minor rewords · symfony/symfony-docs@eea96ba · GitHub
[go: up one dir, main page]

Skip to content

Commit eea96ba

Browse files
committed
Tweaks and minor rewords
1 parent e968429 commit eea96ba

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

components/serializer.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,9 +1207,8 @@ Option Description Defaul
12071207
Context Builders
12081208
----------------
12091209

1210-
Context builders are objects that help creating the :ref:`serialization context <serializer-context>`.
1211-
1212-
You can easily use context builders by instantiating them::
1210+
Instead of passing plain PHP arrays to the :ref:`serialization context <serializer-context>`,
1211+
you can use "context builders" to define the context using a fluent interface::
12131212

12141213
use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
12151214
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
@@ -1228,14 +1227,18 @@ You can easily use context builders by instantiating them::
12281227

12291228
$serializer->serialize($something, 'csv', $contextBuilder->toArray());
12301229

1230+
.. versionadded:: 6.1
1231+
1232+
Context builders were introduced in Symfony 6.1.
1233+
12311234
.. note::
12321235

12331236
The Serializer component provides a context builder
12341237
for each :ref:`normalizer <component-serializer-normalizers>`
12351238
and :ref:`encoder <component-serializer-encoders>`.
12361239

1237-
You can also create custom context builders to deal with your
1238-
context values. Read more at :doc:`/serializer/custom_context_builders`.
1240+
You can also :doc:`create custom context builders </serializer/custom_context_builders>`
1241+
to deal with your context values.
12391242

12401243
Skipping ``null`` Values
12411244
------------------------

serializer.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,22 @@ configuration:
156156
Using Context Builders
157157
----------------------
158158

159-
To define a proper (de)serialization context, you can leverage context builders.
160-
Those are objects that help you to create that context by providing
161-
auto-completion, validation, and documentation::
159+
.. versionadded:: 6.1
160+
161+
Context builders were introduced in Symfony 6.1.
162+
163+
To define the (de)serialization context, you can use "context builders", which
164+
are objects that help you to create that context by providing autocompletion,
165+
validation, and documentation::
162166

163167
use Symfony\Component\Serializer\Context\Normalizer\DateTimeNormalizerContextBuilder;
164168

165169
$contextBuilder = (new DateTimeNormalizerContextBuilder())->withFormat('Y-m-d H:i:s');
166-
167170
$serializer->serialize($something, 'json', $contextBuilder->toArray());
168171

169172
Each normalizer/encoder has its related :ref:`context builder <component-serializer-context-builders>`.
170-
To create a full (de)serialization context, you will be able to chain them using the
171-
``withContext`` method. As the ``withContext`` method takes an array as an argument, it is
172-
also possible to pass custom values to that context::
173+
To create a more complex (de)serialization context, you can chain them using the
174+
``withContext()`` method::
173175

174176
use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
175177
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
@@ -188,8 +190,8 @@ also possible to pass custom values to that context::
188190

189191
$serializer->serialize($something, 'csv', $contextBuilder->toArray());
190192

191-
If you want auto-completion, validation, and documentation for your custom context values,
192-
you can :doc:`create your context builders </serializer/custom_context_builders>`.
193+
You can also :doc:`create your context builders </serializer/custom_context_builders>`
194+
to have autocompletion, validation, and documentation for your custom context values.
193195

194196
.. _serializer-using-serialization-groups-annotations:
195197

serializer/custom_context_builders.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44
How to Create your Custom Context Builder
55
=========================================
66

7+
.. versionadded:: 6.1
8+
9+
Context builders were introduced in Symfony 6.1.
10+
711
The :doc:`Serializer Component </components/serializer>` uses Normalizers
812
and 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

2124
Let'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
2326
value 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

Comments
 (0)
0