From f8c32e94c8656c17a7360d88d6d486bc8ce23b2d Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 10 Aug 2022 17:09:47 +0200 Subject: [PATCH] [Serializer] forward the context from the JsonEncoder to JsonEncode and JsonDecode --- Encoder/JsonEncoder.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Encoder/JsonEncoder.php b/Encoder/JsonEncoder.php index 27f749b63..d460331a3 100644 --- a/Encoder/JsonEncoder.php +++ b/Encoder/JsonEncoder.php @@ -23,10 +23,15 @@ class JsonEncoder implements EncoderInterface, DecoderInterface protected $encodingImpl; protected $decodingImpl; + private $defaultContext = [ + JsonDecode::ASSOCIATIVE => true, + ]; + public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null, array $defaultContext = []) { - $this->encodingImpl = $encodingImpl ?? new JsonEncode($defaultContext); - $this->decodingImpl = $decodingImpl ?? new JsonDecode(array_merge([JsonDecode::ASSOCIATIVE => true], $defaultContext)); + $this->defaultContext = array_merge($this->defaultContext, $defaultContext); + $this->encodingImpl = $encodingImpl ?? new JsonEncode($this->defaultContext); + $this->decodingImpl = $decodingImpl ?? new JsonDecode($this->defaultContext); } /** @@ -34,6 +39,8 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin */ public function encode($data, string $format, array $context = []) { + $context = array_merge($this->defaultContext, $context); + return $this->encodingImpl->encode($data, self::FORMAT, $context); } @@ -42,6 +49,8 @@ public function encode($data, string $format, array $context = []) */ public function decode(string $data, string $format, array $context = []) { + $context = array_merge($this->defaultContext, $context); + return $this->decodingImpl->decode($data, self::FORMAT, $context); }