Skip to content

Commit 0645727

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Notifier] Update FreeMobile DSN [Serializer] Allow to provide (de)normalization context in mapping
2 parents 9366767 + c5bad2c commit 0645727

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

components/serializer.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ normalized data, instead of the denormalizer re-creating them. Note that
229229
arrays of objects. Those will still be replaced when present in the normalized
230230
data.
231231

232+
Context
233+
-------
234+
235+
Many Serializer features can be configured :doc:`using a context </serializer#serializer-context>`.
236+
232237
.. _component-serializer-attributes-groups:
233238

234239
Attributes Groups

notifier.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Clickatell ``symfony/clickatell-notifier`` ``clickatell://ACCESS_TO
6969
ContactEveryone ``symfony/contact-everyone-notifier`` ``contact-everyone://TOKEN@default?&diffusionname=DIFFUSION_NAME&category=CATEGORY``
7070
Esendex ``symfony/esendex-notifier`` ``esendex://USER_NAME:PASSWORD@default?accountreference=ACCOUNT_REFERENCE&from=FROM``
7171
FakeSms ``symfony/fake-sms-notifier`` ``fakesms+email://MAILER_SERVICE_ID?to=TO&from=FROM`` or ``fakesms+logger://default``
72-
FreeMobile ``symfony/free-mobile-notifier`` ``freemobile://LOGIN:PASSWORD@default?phone=PHONE``
72+
FreeMobile ``symfony/free-mobile-notifier`` ``freemobile://LOGIN:API_KEY@default?phone=PHONE``
7373
GatewayApi ``symfony/gateway-api-notifier`` ``gatewayapi://TOKEN@default?from=FROM``
7474
Infobip ``symfony/infobip-notifier`` ``infobip://AUTH_TOKEN@HOST?from=FROM``
7575
Iqsms ``symfony/iqsms-notifier`` ``iqsms://LOGIN:PASSWORD@default?from=FROM``

serializer.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,109 @@ configuration:
162162

163163
The option to configure YAML indentation was introduced in Symfony 6.2.
164164

165+
You can also specify the context on a per-property basis::
166+
167+
.. configuration-block::
168+
169+
.. code-block:: php-annotations
170+
171+
namespace App\Model;
172+
173+
use Symfony\Component\Serializer\Annotation\Context;
174+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
175+
176+
class Person
177+
{
178+
/**
179+
* @Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' })
180+
*/
181+
public $createdAt;
182+
183+
// ...
184+
}
185+
186+
.. code-block:: php-attributes
187+
188+
namespace App\Model;
189+
190+
use Symfony\Component\Serializer\Annotation\Context;
191+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
192+
193+
class Person
194+
{
195+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
196+
public $createdAt;
197+
198+
// ...
199+
}
200+
201+
.. code-block:: yaml
202+
203+
App\Model\Person:
204+
attributes:
205+
createdAt:
206+
context:
207+
datetime_format: 'Y-m-d'
208+
209+
.. code-block:: xml
210+
211+
<?xml version="1.0" encoding="UTF-8" ?>
212+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
213+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
214+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
215+
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
216+
>
217+
<class name="App\Model\Person">
218+
<attribute name="createdAt">
219+
<context>
220+
<entry name="datetime_format">Y-m-d</entry>
221+
</context>
222+
</attribute>
223+
</class>
224+
</serializer>
225+
226+
Use the options to specify context specific to normalization or denormalization::
227+
228+
namespace App\Model;
229+
230+
use Symfony\Component\Serializer\Annotation\Context;
231+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
232+
233+
class Person
234+
{
235+
#[Context(
236+
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'],
237+
denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339],
238+
)]
239+
public $createdAt;
240+
241+
// ...
242+
}
243+
244+
You can also restrict the usage of a context to some groups::
245+
246+
namespace App\Model;
247+
248+
use Symfony\Component\Serializer\Annotation\Context;
249+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
250+
251+
class Person
252+
{
253+
#[Serializer\Groups(['extended'])]
254+
#[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
255+
#[Serializer\Context(
256+
context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
257+
groups: ['extended'],
258+
)]
259+
public $createdAt;
260+
261+
// ...
262+
}
263+
264+
The attribute/annotation can be repeated as much as needed on a single property.
265+
Context without group is always applied first. Then context for the matching
266+
groups are merged in the provided order.
267+
165268
.. _serializer-using-context-builders:
166269

167270
Using Context Builders

0 commit comments

Comments
 (0)