@@ -23,17 +23,24 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
23
23
protected $ encodingImpl ;
24
24
protected $ decodingImpl ;
25
25
26
+ private $ defaultContext = [
27
+ JsonDecode::ASSOCIATIVE => true ,
28
+ ];
29
+
26
30
public function __construct (JsonEncode $ encodingImpl = null , JsonDecode $ decodingImpl = null , array $ defaultContext = [])
27
31
{
28
- $ this ->encodingImpl = $ encodingImpl ?? new JsonEncode ($ defaultContext );
29
- $ this ->decodingImpl = $ decodingImpl ?? new JsonDecode (array_merge ([JsonDecode::ASSOCIATIVE => true ], $ defaultContext ));
32
+ $ this ->defaultContext = array_merge ($ this ->defaultContext , $ defaultContext );
33
+ $ this ->encodingImpl = $ encodingImpl ?? new JsonEncode ($ this ->defaultContext );
34
+ $ this ->decodingImpl = $ decodingImpl ?? new JsonDecode ($ this ->defaultContext );
30
35
}
31
36
32
37
/**
33
38
* {@inheritdoc}
34
39
*/
35
40
public function encode ($ data , string $ format , array $ context = [])
36
41
{
42
+ $ context = array_merge ($ this ->defaultContext , $ context );
43
+
37
44
return $ this ->encodingImpl ->encode ($ data , self ::FORMAT , $ context );
38
45
}
39
46
@@ -42,6 +49,8 @@ public function encode($data, string $format, array $context = [])
42
49
*/
43
50
public function decode (string $ data , string $ format , array $ context = [])
44
51
{
52
+ $ context = array_merge ($ this ->defaultContext , $ context );
53
+
45
54
return $ this ->decodingImpl ->decode ($ data , self ::FORMAT , $ context );
46
55
}
47
56
0 commit comments