@@ -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 as an 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 )) {
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 => (bool ) $ defaultContext ,
55+ self ::RECURSION_DEPTH => $ depth ,
56+ );
57+ }
58+
59+ $ this ->defaultContext = array_merge ($ this ->defaultContext , $ defaultContext );
3860 }
3961
4062 /**
@@ -47,7 +69,7 @@ public function __construct(bool $associative = false, int $depth = 512)
4769 * The $context array is a simple key=>value array, with the following supported keys:
4870 *
4971 * json_decode_associative: boolean
50- * If true, returns the object as associative array.
72+ * If true, returns the object as an associative array.
5173 * If false, returns the object as nested stdClass
5274 * If not specified, this method will use the default set in JsonDecode::__construct
5375 *
@@ -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}
0 commit comments