Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 914577e

Browse files
ogizanaginicolas-grekas
authored andcommitted
[Serializer] Unified normalizers/encoders config through default context solely
1 parent 3fec468 commit 914577e

16 files changed

+81
-792
lines changed

UPGRADE-5.0.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ Serializer
383383
----------
384384

385385
* The default value of the `CsvEncoder` "as_collection" option was changed to `true`.
386+
* Individual encoders & normalizers options as constructor arguments were removed.
387+
Use the default context instead.
388+
* The following method and properties:
389+
- `AbstractNormalizer::$circularReferenceLimit`
390+
- `AbstractNormalizer::$circularReferenceHandler`
391+
- `AbstractNormalizer::$callbacks`
392+
- `AbstractNormalizer::$ignoredAttributes`
393+
- `AbstractNormalizer::$camelizedAttributes`
394+
- `AbstractNormalizer::setCircularReferenceLimit()`
395+
- `AbstractNormalizer::setCircularReferenceHandler()`
396+
- `AbstractNormalizer::setCallbacks()`
397+
- `AbstractNormalizer::setIgnoredAttributes()`
398+
- `AbstractObjectNormalizer::$maxDepthHandler`
399+
- `AbstractObjectNormalizer::setMaxDepthHandler()`
400+
- `XmlEncoder::setRootNodeName()`
401+
- `XmlEncoder::getRootNodeName()`
402+
403+
were removed, use the default context instead.
386404
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.
387405

388406
Translation

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ CHANGELOG
77
* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
88
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
99
* changed the default value of the `CsvEncoder` "as_collection" option to `true`
10+
* removed `AbstractNormalizer::$circularReferenceLimit`, `AbstractNormalizer::$circularReferenceHandler`,
11+
`AbstractNormalizer::$callbacks`, `AbstractNormalizer::$ignoredAttributes`,
12+
`AbstractNormalizer::$camelizedAttributes`, `AbstractNormalizer::setCircularReferenceLimit()`,
13+
`AbstractNormalizer::setCircularReferenceHandler()`, `AbstractNormalizer::setCallbacks()` and
14+
`AbstractNormalizer::setIgnoredAttributes()`, use the default context instead.
15+
* removed `AbstractObjectNormalizer::$maxDepthHandler` and `AbstractObjectNormalizer::setMaxDepthHandler()`,
16+
use the default context instead.
17+
* removed `XmlEncoder::setRootNodeName()` & `XmlEncoder::getRootNodeName()`, use the default context instead.
18+
* removed individual encoders/normalizers options as constructor arguments.
1019

1120
4.3.0
1221
-----

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,8 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
4343
self::NO_HEADERS_KEY => false,
4444
];
4545

46-
/**
47-
* @param array $defaultContext
48-
*/
49-
public function __construct($defaultContext = [], string $enclosure = '"', string $escapeChar = '\\', string $keySeparator = '.', bool $escapeFormulas = false)
46+
public function __construct(array $defaultContext = [])
5047
{
51-
if (!\is_array($defaultContext)) {
52-
@trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED);
53-
54-
$defaultContext = [
55-
self::DELIMITER_KEY => (string) $defaultContext,
56-
self::ENCLOSURE_KEY => $enclosure,
57-
self::ESCAPE_CHAR_KEY => $escapeChar,
58-
self::KEY_SEPARATOR_KEY => $keySeparator,
59-
self::ESCAPE_FORMULAS_KEY => $escapeFormulas,
60-
];
61-
}
62-
6348
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
6449
}
6550

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,8 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
7171
private $format;
7272
private $context;
7373

74-
/**
75-
* @param array $defaultContext
76-
*/
77-
public function __construct($defaultContext = [], int $loadOptions = null, array $decoderIgnoredNodeTypes = [XML_PI_NODE, XML_COMMENT_NODE], array $encoderIgnoredNodeTypes = [])
74+
public function __construct(array $defaultContext = [])
7875
{
79-
if (!\is_array($defaultContext)) {
80-
@trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED);
81-
82-
$defaultContext = [
83-
self::DECODER_IGNORED_NODE_TYPES => $decoderIgnoredNodeTypes,
84-
self::ENCODER_IGNORED_NODE_TYPES => $encoderIgnoredNodeTypes,
85-
self::LOAD_OPTIONS => $loadOptions ?? LIBXML_NONET | LIBXML_NOBLANKS,
86-
self::ROOT_NODE_NAME => (string) $defaultContext,
87-
];
88-
}
89-
9076
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
9177
}
9278

@@ -203,34 +189,6 @@ public function supportsDecoding($format)
203189
return self::FORMAT === $format;
204190
}
205191

206-
/**
207-
* Sets the root node name.
208-
*
209-
* @deprecated since Symfony 4.2
210-
*
211-
* @param string $name Root node name
212-
*/
213-
public function setRootNodeName($name)
214-
{
215-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the context instead.', __METHOD__), E_USER_DEPRECATED);
216-
217-
$this->defaultContext[self::ROOT_NODE_NAME] = $name;
218-
}
219-
220-
/**
221-
* Returns the root node name.
222-
*
223-
* @deprecated since Symfony 4.2
224-
*
225-
* @return string
226-
*/
227-
public function getRootNodeName()
228-
{
229-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the context instead.', __METHOD__), E_USER_DEPRECATED);
230-
231-
return $this->defaultContext[self::ROOT_NODE_NAME];
232-
}
233-
234192
final protected function appendXMLString(\DOMNode $node, string $val): bool
235193
{
236194
if ('' !== $val) {

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,11 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
118118

119119
protected $defaultContext = [
120120
self::ALLOW_EXTRA_ATTRIBUTES => true,
121+
self::CIRCULAR_REFERENCE_HANDLER => null,
121122
self::CIRCULAR_REFERENCE_LIMIT => 1,
122123
self::IGNORED_ATTRIBUTES => [],
123124
];
124125

125-
/**
126-
* @deprecated since Symfony 4.2
127-
*/
128-
protected $circularReferenceLimit = 1;
129-
130-
/**
131-
* @deprecated since Symfony 4.2
132-
*
133-
* @var callable|null
134-
*/
135-
protected $circularReferenceHandler;
136-
137126
/**
138127
* @var ClassMetadataFactoryInterface|null
139128
*/
@@ -144,21 +133,6 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
144133
*/
145134
protected $nameConverter;
146135

147-
/**
148-
* @deprecated since Symfony 4.2
149-
*/
150-
protected $callbacks = [];
151-
152-
/**
153-
* @deprecated since Symfony 4.2
154-
*/
155-
protected $ignoredAttributes = [];
156-
157-
/**
158-
* @deprecated since Symfony 4.2
159-
*/
160-
protected $camelizedAttributes = [];
161-
162136
/**
163137
* Sets the {@link ClassMetadataFactoryInterface} to use.
164138
*/
@@ -185,83 +159,6 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
185159
}
186160
}
187161

188-
/**
189-
* Sets circular reference limit.
190-
*
191-
* @deprecated since Symfony 4.2
192-
*
193-
* @param int $circularReferenceLimit Limit of iterations for the same object
194-
*
195-
* @return self
196-
*/
197-
public function setCircularReferenceLimit($circularReferenceLimit)
198-
{
199-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "circular_reference_limit" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
200-
201-
$this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT] = $this->circularReferenceLimit = $circularReferenceLimit;
202-
203-
return $this;
204-
}
205-
206-
/**
207-
* Sets circular reference handler.
208-
*
209-
* @deprecated since Symfony 4.2
210-
*
211-
* @param callable $circularReferenceHandler
212-
*
213-
* @return self
214-
*/
215-
public function setCircularReferenceHandler(callable $circularReferenceHandler)
216-
{
217-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "circular_reference_handler" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
218-
219-
$this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER] = $this->circularReferenceHandler = $circularReferenceHandler;
220-
221-
return $this;
222-
}
223-
224-
/**
225-
* Sets normalization callbacks.
226-
*
227-
* @deprecated since Symfony 4.2
228-
*
229-
* @param callable[] $callbacks Help normalize the result
230-
*
231-
* @return self
232-
*
233-
* @throws InvalidArgumentException if a non-callable callback is set
234-
*/
235-
public function setCallbacks(array $callbacks)
236-
{
237-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "callbacks" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
238-
239-
foreach ($callbacks as $attribute => $callback) {
240-
if (!\is_callable($callback)) {
241-
throw new InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
242-
}
243-
}
244-
$this->defaultContext[self::CALLBACKS] = $this->callbacks = $callbacks;
245-
246-
return $this;
247-
}
248-
249-
/**
250-
* Sets ignored attributes for normalization and denormalization.
251-
*
252-
* @deprecated since Symfony 4.2
253-
*
254-
* @return self
255-
*/
256-
public function setIgnoredAttributes(array $ignoredAttributes)
257-
{
258-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "ignored_attributes" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
259-
260-
$this->defaultContext[self::IGNORED_ATTRIBUTES] = $this->ignoredAttributes = $ignoredAttributes;
261-
262-
return $this;
263-
}
264-
265162
/**
266163
* {@inheritdoc}
267164
*/
@@ -284,7 +181,7 @@ protected function isCircularReference($object, &$context)
284181
{
285182
$objectHash = spl_object_hash($object);
286183

287-
$circularReferenceLimit = $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->circularReferenceLimit;
184+
$circularReferenceLimit = $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT];
288185
if (isset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash])) {
289186
if ($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] >= $circularReferenceLimit) {
290187
unset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash]);
@@ -324,12 +221,12 @@ protected function handleCircularReference($object/*, string $format = null, arr
324221
$format = \func_num_args() > 1 ? func_get_arg(1) : null;
325222
$context = \func_num_args() > 2 ? func_get_arg(2) : [];
326223

327-
$circularReferenceHandler = $context[self::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER] ?? $this->circularReferenceHandler;
224+
$circularReferenceHandler = $context[self::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER];
328225
if ($circularReferenceHandler) {
329226
return $circularReferenceHandler($object, $format, $context);
330227
}
331228

332-
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit));
229+
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT]));
333230
}
334231

335232
/**
@@ -387,7 +284,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
387284
*/
388285
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
389286
{
390-
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES] ?? $this->ignoredAttributes;
287+
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
391288
if (\in_array($attribute, $ignoredAttributes)) {
392289
return false;
393290
}

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
9292
private $typesCache = [];
9393
private $attributesCache = [];
9494

95-
/**
96-
* @deprecated since Symfony 4.2
97-
*
98-
* @var callable|null
99-
*/
100-
private $maxDepthHandler;
10195
private $objectClassResolver;
10296

10397
/**
@@ -168,8 +162,7 @@ public function normalize($object, $format = null, array $context = [])
168162
throw new InvalidArgumentException(sprintf('The "%s" given in the context is not callable.', self::MAX_DEPTH_HANDLER));
169163
}
170164
} else {
171-
// already validated in constructor resp by type declaration of setMaxDepthHandler
172-
$maxDepthHandler = $this->defaultContext[self::MAX_DEPTH_HANDLER] ?? $this->maxDepthHandler;
165+
$maxDepthHandler = null;
173166
}
174167

175168
foreach ($attributes as $attribute) {
@@ -186,7 +179,7 @@ public function normalize($object, $format = null, array $context = [])
186179
/**
187180
* @var callable|null
188181
*/
189-
$callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? $this->callbacks[$attribute] ?? null;
182+
$callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? null;
190183
if ($callback) {
191184
$attributeValue = $callback($attributeValue, $object, $attribute, $format, $context);
192185
}
@@ -295,18 +288,6 @@ abstract protected function extractAttributes($object, $format = null, array $co
295288
*/
296289
abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = []);
297290

298-
/**
299-
* Sets a handler function that will be called when the max depth is reached.
300-
*
301-
* @deprecated since Symfony 4.2
302-
*/
303-
public function setMaxDepthHandler(?callable $handler): void
304-
{
305-
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "max_depth_handler" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
306-
307-
$this->maxDepthHandler = $handler;
308-
}
309-
310291
/**
311292
* {@inheritdoc}
312293
*/
@@ -595,8 +576,7 @@ private function getCacheKey(?string $format, array $context)
595576
try {
596577
return md5($format.serialize([
597578
'context' => $context,
598-
'ignored' => $this->ignoredAttributes,
599-
'camelized' => $this->camelizedAttributes,
579+
'ignored' => $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES],
600580
]));
601581
} catch (\Exception $exception) {
602582
// The context cannot be serialized, skip the cache

src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,8 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
2828
self::FORMAT_KEY => 'P%yY%mM%dDT%hH%iM%sS',
2929
];
3030

31-
/**
32-
* @param array $defaultContext
33-
*/
34-
public function __construct($defaultContext = [])
31+
public function __construct(array $defaultContext = [])
3532
{
36-
if (!\is_array($defaultContext)) {
37-
@trigger_error(sprintf('The "format" parameter is deprecated since Symfony 4.2, use the "%s" key of the context instead.', self::FORMAT_KEY), E_USER_DEPRECATED);
38-
39-
$defaultContext = [self::FORMAT_KEY => (string) $defaultContext];
40-
}
41-
4233
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
4334
}
4435

0 commit comments

Comments
 (0)