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

Skip to content

Commit add8f3b

Browse files
committed
[Serializer] Refactor and uniformize the config by introducing a default context
1 parent 0f653d8 commit add8f3b

22 files changed

+856
-255
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 8 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
@@ -20,6 +21,13 @@ CHANGELOG
2021
either `NormalizerInterface` or `DenormalizerInterface`
2122
* deprecated creating a `Serializer` with encoders which do not implement
2223
either `EncoderInterface` or `DecoderInterface`
24+
* `AbstractNormalizer::$circularReferenceLimit`, `AbstractNormalizer::$circularReferenceHandler`,
25+
`AbstractNormalizer::$callbacks`, `AbstractNormalizer::$ignoredAttributes`,
26+
`AbstractNormalizer::$camelizedAttributes`, `AbstractNormalizer::setCircularReferenceLimit()`,
27+
`AbstractNormalizer::setCircularReferenceHandler()`, `AbstractNormalizer::setCallbacks()` and
28+
`AbstractNormalizer::setIgnoredAttributes()` are deprecated, use the default context instead.
29+
* `AbstractObjectNormalizer::$maxDepthHandler` and `AbstractObjectNormalizer::setMaxDepthHandler()`
30+
are deprecated, use the default context instead.
2331

2432
4.1.0
2533
-----

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

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,45 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3030
const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
3131
const AS_COLLECTION_KEY = 'as_collection';
3232

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

40-
public function __construct(string $delimiter = ',', string $enclosure = '"', string $escapeChar = '\\', string $keySeparator = '.', bool $escapeFormulas = false)
42+
/**
43+
* @param array $defaultContext
44+
* @param string $enclosure
45+
* @param string $escapeChar
46+
* @param string $keySeparator
47+
* @param bool $escapeFormulas
48+
*/
49+
public function __construct($defaultContext = array(), string $enclosure = '"', string $escapeChar = '\\', string $keySeparator = '.', bool $escapeFormulas = false)
4150
{
42-
$this->delimiter = $delimiter;
43-
$this->enclosure = $enclosure;
44-
$this->escapeChar = $escapeChar;
45-
$this->keySeparator = $keySeparator;
46-
$this->escapeFormulas = $escapeFormulas;
51+
if (!\is_array($defaultContext)) {
52+
@trigger_error(sprintf('The "delimiter" parameter is deprecated since Symfony 4.2, use the "%s" key of the context instead.', static::DELIMITER_KEY), E_USER_DEPRECATED);
53+
54+
$defaultContext = array(static::DELIMITER_KEY => $defaultContext);
55+
}
56+
57+
$args = array(
58+
array('enclosure', static::ENCLOSURE_KEY, '"'),
59+
array('escapeChar', static::ESCAPE_CHAR_KEY, '\\'),
60+
array('keySeparator', static::KEY_SEPARATOR_KEY, '.'),
61+
array('escapeFormulas', static::ESCAPE_FORMULAS_KEY, false),
62+
);
63+
foreach ($args as $arg) {
64+
$val = ${$arg[0]};
65+
if (${$arg[0]} !== $arg[2]) {
66+
$this->defaultContext[$arg[1]] = $val;
67+
@trigger_error(sprintf('The "%s" parameter is deprecated since Symfony 4.2, use the "%s" key of the context instead.', $arg[0], $arg[1]), E_USER_DEPRECATED);
68+
}
69+
}
70+
71+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
4772
}
4873

4974
/**
@@ -200,14 +225,14 @@ private function flatten(array $array, array &$result, string $keySeparator, str
200225
}
201226
}
202227

203-
private function getCsvOptions(array $context)
228+
private function getCsvOptions(array $context): array
204229
{
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;
230+
$delimiter = $context[static::DELIMITER_KEY] ?? $this->defaultContext[static::DELIMITER_KEY];
231+
$enclosure = $context[static::ENCLOSURE_KEY] ?? $this->defaultContext[static::ENCLOSURE_KEY];
232+
$escapeChar = $context[static::ESCAPE_CHAR_KEY] ?? $this->defaultContext[static::ESCAPE_CHAR_KEY];
233+
$keySeparator = $context[static::KEY_SEPARATOR_KEY] ?? $this->defaultContext[static::KEY_SEPARATOR_KEY];
234+
$headers = $context[static::HEADERS_KEY] ?? $this->defaultContext[static::HEADERS_KEY] ?? array();
235+
$escapeFormulas = $context[static::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[static::ESCAPE_FORMULAS_KEY];
211236

212237
if (!\is_array($headers)) {
213238
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: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,41 @@ 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+
/**
31+
* Specifies the recursion depth
32+
*/
33+
const RECURSION_DEPTH = 'json_decode_recursion_depth';
34+
35+
const OPTIONS = 'json_decode_options';
36+
37+
private $defaultContext = array(
38+
self::ASSOCIATIVE => false,
39+
self::RECURSION_DEPTH => 512,
40+
self::OPTIONS => 0,
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.', static::ASSOCIATIVE, static::RECURSION_DEPTH), E_USER_DEPRECATED);
52+
53+
$defaultContext = array(
54+
static::ASSOCIATIVE => $defaultContext,
55+
static::RECURSION_DEPTH => $depth,
56+
);
57+
}
58+
59+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
3860
}
3961

4062
/**
@@ -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[static::ASSOCIATIVE] ?? $this->defaultContext[static::ASSOCIATIVE] ;
92+
$recursionDepth = $context[static::RECURSION_DEPTH] ?? $this->defaultContext[static::RECURSION_DEPTH];
93+
$options = $context[static::OPTIONS] ?? $this->defaultContext[static::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[static::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[static::JSON_ENCODE_OPTIONS] ?? $this->defaultContext[static::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)