Skip to content

Commit c2f93ef

Browse files
committed
minor #12582 Use normalizer context constants everywhere (l-vo)
This PR was merged into the 4.3 branch. Discussion ---------- Use normalizer context constants everywhere Commits ------- 70f84af Use normalizer context constants everywhere
2 parents a890dcc + 70f84af commit c2f93ef

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

components/serializer.rst

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ needs three parameters:
168168

169169
By default, additional attributes that are not mapped to the denormalized object
170170
will be ignored by the Serializer component. If you prefer to throw an exception
171-
when this happens, set the ``allow_extra_attributes`` context option to
171+
when this happens, set the ``AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES`` context option to
172172
``false`` and provide an object that implements ``ClassMetadataFactoryInterface``
173173
when constructing the normalizer::
174174

@@ -188,7 +188,7 @@ when constructing the normalizer::
188188
// this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException
189189
// because "city" is not an attribute of the Person class
190190
$person = $serializer->deserialize($data, 'App\Model\Person', 'xml', [
191-
'allow_extra_attributes' => false,
191+
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
192192
]);
193193

194194
Deserializing in an Existing Object
@@ -209,12 +209,12 @@ The serializer can also be used to update an existing object::
209209
</person>
210210
EOF;
211211

212-
$serializer->deserialize($data, Person::class, 'xml', ['object_to_populate' => $person]);
212+
$serializer->deserialize($data, Person::class, 'xml', [AbstractNormalizer::OBJECT_TO_POPULATE => $person]);
213213
// $person = App\Model\Person(name: 'foo', age: '69', sportsperson: true)
214214

215215
This is a common need when working with an ORM.
216216

217-
The ``OBJECT_TO_POPULATE`` is only used for the top level object. If that object
217+
The ``AbstractNormalizer::OBJECT_TO_POPULATE`` is only used for the top level object. If that object
218218
is the root of a tree structure, all child elements that exist in the
219219
normalized data will be re-created with new instances.
220220

@@ -377,6 +377,7 @@ Selecting Specific Attributes
377377

378378
It is also possible to serialize only a set of specific attributes::
379379

380+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
380381
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
381382
use Symfony\Component\Serializer\Serializer;
382383

@@ -404,7 +405,7 @@ It is also possible to serialize only a set of specific attributes::
404405

405406
$serializer = new Serializer([new ObjectNormalizer()]);
406407

407-
$data = $serializer->normalize($user, null, ['attributes' => ['familyName', 'company' => ['name']]]);
408+
$data = $serializer->normalize($user, null, [AbstractNormalizer::ATTRIBUTES => ['familyName', 'company' => ['name']]]);
408409
// $data = ['familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']];
409410

410411
Only attributes that are not ignored (see below) are available.
@@ -416,11 +417,12 @@ Ignoring Attributes
416417
-------------------
417418

418419
As an option, there's a way to ignore attributes from the origin object.
419-
To remove those attributes provide an array via the ``ignored_attributes``
420+
To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES``
420421
key in the ``context`` parameter of the desired serializer method::
421422

422423
use Acme\Person;
423424
use Symfony\Component\Serializer\Encoder\JsonEncoder;
425+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
424426
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
425427
use Symfony\Component\Serializer\Serializer;
426428

@@ -432,12 +434,12 @@ key in the ``context`` parameter of the desired serializer method::
432434
$encoder = new JsonEncoder();
433435

434436
$serializer = new Serializer([$normalizer], [$encoder]);
435-
$serializer->serialize($person, 'json', ['ignored_attributes' => ['age']]); // Output: {"name":"foo"}
437+
$serializer->serialize($person, 'json', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['age']]); // Output: {"name":"foo"}
436438

437439
.. deprecated:: 4.2
438440

439441
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
440-
method that was used as an alternative to the ``ignored_attributes`` option
442+
method that was used as an alternative to the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` option
441443
was deprecated in Symfony 4.2.
442444

443445
.. _component-serializer-converting-property-names-when-serializing-and-deserializing:
@@ -873,7 +875,7 @@ Skipping ``null`` Values
873875
------------------------
874876

875877
By default, the Serializer will preserve properties containing a ``null`` value.
876-
You can change this behavior by setting the ``skip_null_values`` context option
878+
You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NULL_VALUES`` context option
877879
to ``true``::
878880

879881
$dummy = new class {
@@ -882,7 +884,7 @@ to ``true``::
882884
};
883885

884886
$normalizer = new ObjectNormalizer();
885-
$result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]);
887+
$result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
886888
// ['bar' => 'notNull']
887889

888890
.. _component-serializer-handling-circular-references:
@@ -983,7 +985,7 @@ having unique identifiers::
983985
.. deprecated:: 4.2
984986

985987
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler`
986-
method is deprecated since Symfony 4.2. Use the ``circular_reference_handler``
988+
method is deprecated since Symfony 4.2. Use the ``AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER``
987989
key of the context instead.
988990

989991
Handling Serialization Depth
@@ -1063,11 +1065,11 @@ in a Symfony application. When using the standalone component, refer to
10631065
:ref:`the groups documentation <component-serializer-attributes-groups>` to
10641066
learn how to do that.
10651067

1066-
The check is only done if the ``enable_max_depth`` key of the serializer context
1068+
The check is only done if the ``AbstractObjectNormalizer::ENABLE_MAX_DEPTH`` key of the serializer context
10671069
is set to ``true``. In the following example, the third level is not serialized
10681070
because it is deeper than the configured maximum depth of 2::
10691071

1070-
$result = $serializer->normalize($level1, null, ['enable_max_depth' => true]);
1072+
$result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
10711073
/*
10721074
$result = [
10731075
'foo' => 'level1',
@@ -1088,6 +1090,7 @@ having unique identifiers::
10881090
use Symfony\Component\Serializer\Annotation\MaxDepth;
10891091
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
10901092
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1093+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
10911094
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
10921095
use Symfony\Component\Serializer\Serializer;
10931096

@@ -1126,7 +1129,7 @@ having unique identifiers::
11261129

11271130
$serializer = new Serializer([$normalizer]);
11281131

1129-
$result = $serializer->normalize($level1, null, [ObjectNormalizer::ENABLE_MAX_DEPTH => true]);
1132+
$result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
11301133
/*
11311134
$result = [
11321135
'id' => 1,
@@ -1262,6 +1265,7 @@ If the class constructor defines arguments, as usually happens with
12621265
arguments are missing. In those cases, use the ``default_constructor_arguments``
12631266
context option::
12641267

1268+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
12651269
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
12661270
use Symfony\Component\Serializer\Serializer;
12671271

@@ -1283,7 +1287,7 @@ context option::
12831287
$data = $serializer->denormalize(
12841288
['foo' => 'Hello'],
12851289
'MyObj',
1286-
['default_constructor_arguments' => [
1290+
[AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [
12871291
'MyObj' => ['foo' => '', 'bar' => ''],
12881292
]]
12891293
);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy