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

Skip to content

Commit 83463d2

Browse files
committed
[Serializer] Refactor and uniformize the config by introducing a default context
1 parent 5a0cad2 commit 83463d2

20 files changed

+851
-265
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* using the default context is the new recommended way to configure normalizers and encoders
78
* added a `skip_null_values` context option to not serialize properties with a `null` values
89
* `AbstractNormalizer::handleCircularReference` is now final and receives
910
two optional extra arguments: the format and the context
@@ -22,6 +23,15 @@ CHANGELOG
2223
either `EncoderInterface` or `DecoderInterface`
2324
* added the optional `$objectClassResolver` argument in `AbstractObjectNormalizer`
2425
and `ObjectNormalizer` constructor
26+
* `AbstractNormalizer::$circularReferenceLimit`, `AbstractNormalizer::$circularReferenceHandler`,
27+
`AbstractNormalizer::$callbacks`, `AbstractNormalizer::$ignoredAttributes`,
28+
`AbstractNormalizer::$camelizedAttributes`, `AbstractNormalizer::setCircularReferenceLimit()`,
29+
`AbstractNormalizer::setCircularReferenceHandler()`, `AbstractNormalizer::setCallbacks()` and
30+
`AbstractNormalizer::setIgnoredAttributes()` are deprecated, use the default context instead.
31+
* `AbstractObjectNormalizer::$maxDepthHandler` and `AbstractObjectNormalizer::setMaxDepthHandler()`
32+
are deprecated, use the default context instead.
33+
* passing configuration options directly to the constructor of `CsvEncoder`, `JsonDecode` and
34+
`XmlEncoder` is deprecated since Symfony 4.2, use the default context instead.
2535

2636
4.1.0
2737
-----

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

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,43 @@
2222
class CsvEncoder implements EncoderInterface, DecoderInterface
2323
{
2424
const FORMAT = 'csv';
25+
26+
const AS_COLLECTION_KEY = 'as_collection';
2527
const DELIMITER_KEY = 'csv_delimiter';
2628
const ENCLOSURE_KEY = 'csv_enclosure';
2729
const ESCAPE_CHAR_KEY = 'csv_escape_char';
28-
const KEY_SEPARATOR_KEY = 'csv_key_separator';
29-
const HEADERS_KEY = 'csv_headers';
3030
const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
31-
const AS_COLLECTION_KEY = 'as_collection';
31+
const HEADERS_KEY = 'csv_headers';
32+
const KEY_SEPARATOR_KEY = 'csv_key_separator';
3233

33-
private $delimiter;
34-
private $enclosure;
35-
private $escapeChar;
36-
private $keySeparator;
37-
private $escapeFormulas;
3834
private $formulasStartCharacters = array('=', '-', '+', '@');
35+
private $defaultContext = array(
36+
self::DELIMITER_KEY => ',',
37+
self::ENCLOSURE_KEY => '"',
38+
self::ESCAPE_CHAR_KEY => '\\',
39+
self::ESCAPE_FORMULAS_KEY => false,
40+
self::HEADERS_KEY => array(),
41+
self::KEY_SEPARATOR_KEY => '.',
42+
);
3943

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

4964
/**
@@ -200,14 +215,14 @@ private function flatten(array $array, array &$result, string $keySeparator, str
200215
}
201216
}
202217

203-
private function getCsvOptions(array $context)
218+
private function getCsvOptions(array $context): array
204219
{
205-
$delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter;
206-
$enclosure = isset($context[self::ENCLOSURE_KEY]) ? $context[self::ENCLOSURE_KEY] : $this->enclosure;
207-
$escapeChar = isset($context[self::ESCAPE_CHAR_KEY]) ? $context[self::ESCAPE_CHAR_KEY] : $this->escapeChar;
208-
$keySeparator = isset($context[self::KEY_SEPARATOR_KEY]) ? $context[self::KEY_SEPARATOR_KEY] : $this->keySeparator;
209-
$headers = isset($context[self::HEADERS_KEY]) ? $context[self::HEADERS_KEY] : array();
210-
$escapeFormulas = isset($context[self::ESCAPE_FORMULAS_KEY]) ? $context[self::ESCAPE_FORMULAS_KEY] : $this->escapeFormulas;
220+
$delimiter = $context[self::DELIMITER_KEY] ?? $this->defaultContext[self::DELIMITER_KEY];
221+
$enclosure = $context[self::ENCLOSURE_KEY] ?? $this->defaultContext[self::ENCLOSURE_KEY];
222+
$escapeChar = $context[self::ESCAPE_CHAR_KEY] ?? $this->defaultContext[self::ESCAPE_CHAR_KEY];
223+
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
224+
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
225+
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
211226

212227
if (!\is_array($headers)) {
213228
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,49 @@ class JsonDecode implements DecoderInterface
2222
{
2323
protected $serializer;
2424

25-
private $associative;
26-
private $recursionDepth;
25+
/**
26+
* True to return the result associative array, false for a nested stdClass hierarchy.
27+
*/
28+
const ASSOCIATIVE = 'json_decode_associative';
29+
30+
const OPTIONS = 'json_decode_options';
31+
32+
/**
33+
* Specifies the recursion depth.
34+
*/
35+
const RECURSION_DEPTH = 'json_decode_recursion_depth';
36+
37+
private $defaultContext = array(
38+
self::ASSOCIATIVE => false,
39+
self::OPTIONS => 0,
40+
self::RECURSION_DEPTH => 512,
41+
);
2742

2843
/**
2944
* Constructs a new JsonDecode instance.
3045
*
31-
* @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
32-
* @param int $depth Specifies the recursion depth
46+
* @param array $defaultContext
3347
*/
34-
public function __construct(bool $associative = false, int $depth = 512)
48+
public function __construct($defaultContext = array(), int $depth = 512)
3549
{
36-
$this->associative = $associative;
37-
$this->recursionDepth = $depth;
50+
if (!\is_array($defaultContext) || 512 !== $depth) {
51+
@trigger_error(sprintf('Using constructor parameters that are not a default context is deprecated since Symfony 4.2, use the "%s" and "%s" keys of the context instead.', self::ASSOCIATIVE, self::RECURSION_DEPTH), E_USER_DEPRECATED);
52+
53+
$defaultContext = array(
54+
self::ASSOCIATIVE => $defaultContext,
55+
self::RECURSION_DEPTH => $depth,
56+
);
57+
}
58+
59+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
3860
}
3961

4062
/**
4163
* Decodes data.
4264
*
4365
* @param string $data The encoded JSON string to decode
4466
* @param string $format Must be set to JsonEncoder::FORMAT
45-
* @param array $context An optional set of options for the JSON decoder; see below
67+
* @param array $context an optional set of options for the JSON decoder; see below
4668
*
4769
* The $context array is a simple key=>value array, with the following supported keys:
4870
*
@@ -56,7 +78,7 @@ public function __construct(bool $associative = false, int $depth = 512)
5678
* If not specified, this method will use the default set in JsonDecode::__construct
5779
*
5880
* json_decode_options: integer
59-
* Specifies additional options as per documentation for json_decode.
81+
* Specifies additional options as per documentation for json_decode
6082
*
6183
* @return mixed
6284
*
@@ -66,11 +88,9 @@ public function __construct(bool $associative = false, int $depth = 512)
6688
*/
6789
public function decode($data, $format, array $context = array())
6890
{
69-
$context = $this->resolveContext($context);
70-
71-
$associative = $context['json_decode_associative'];
72-
$recursionDepth = $context['json_decode_recursion_depth'];
73-
$options = $context['json_decode_options'];
91+
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
92+
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
93+
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
7494

7595
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
7696

@@ -88,20 +108,4 @@ public function supportsDecoding($format)
88108
{
89109
return JsonEncoder::FORMAT === $format;
90110
}
91-
92-
/**
93-
* Merges the default options of the Json Decoder with the passed context.
94-
*
95-
* @return array
96-
*/
97-
private function resolveContext(array $context)
98-
{
99-
$defaultOptions = array(
100-
'json_decode_associative' => $this->associative,
101-
'json_decode_recursion_depth' => $this->recursionDepth,
102-
'json_decode_options' => 0,
103-
);
104-
105-
return array_merge($defaultOptions, $context);
106-
}
107111
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@
2020
*/
2121
class JsonEncode implements EncoderInterface
2222
{
23-
private $options;
23+
const JSON_ENCODE_OPTIONS = 'json_encode_options';
2424

25-
public function __construct(int $bitmask = 0)
25+
private $defaultContext = array(
26+
'json_encode_options' => 0,
27+
);
28+
29+
/**
30+
* @param array $defaultContext
31+
*/
32+
public function __construct($defaultContext = array())
2633
{
27-
$this->options = $bitmask;
34+
if (\is_int($defaultContext)) {
35+
@trigger_error(sprintf('Passing an integer as first parameter of the "%s()" method is deprecated since Symfony 4.2, use the "json_encode_options" key of the context instead.', __METHOD__), E_USER_DEPRECATED);
36+
37+
$this->defaultContext[self::JSON_ENCODE_OPTIONS] = $defaultContext;
38+
} else {
39+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
40+
}
2841
}
2942

3043
/**
@@ -34,11 +47,10 @@ public function __construct(int $bitmask = 0)
3447
*/
3548
public function encode($data, $format, array $context = array())
3649
{
37-
$context = $this->resolveContext($context);
38-
39-
$encodedJson = json_encode($data, $context['json_encode_options']);
50+
$jsonEncodeOptions = $context[self::JSON_ENCODE_OPTIONS] ?? $this->defaultContext[self::JSON_ENCODE_OPTIONS];
51+
$encodedJson = json_encode($data, $jsonEncodeOptions);
4052

41-
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
53+
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($jsonEncodeOptions & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
4254
throw new NotEncodableValueException(json_last_error_msg());
4355
}
4456

@@ -52,14 +64,4 @@ public function supportsEncoding($format)
5264
{
5365
return JsonEncoder::FORMAT === $format;
5466
}
55-
56-
/**
57-
* Merge default json encode options with context.
58-
*
59-
* @return array
60-
*/
61-
private function resolveContext(array $context = array())
62-
{
63-
return array_merge(array('json_encode_options' => $this->options), $context);
64-
}
6567
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
2626
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
2727
{
2828
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
29-
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
29+
$this->decodingImpl = $decodingImpl ?: new JsonDecode(array(JsonDecode::ASSOCIATIVE => true));
3030
}
3131

3232
/**

0 commit comments

Comments
 (0)