From bfdcbb883f57305d12aa68d00a472e7dfd3bc6ab Mon Sep 17 00:00:00 2001 From: Steevan BARBOYON Date: Tue, 26 Mar 2019 10:23:52 +0100 Subject: [PATCH] Add parameters to validate decoded json or not --- src/Decoder/AbstractJsonDecoder.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Decoder/AbstractJsonDecoder.php b/src/Decoder/AbstractJsonDecoder.php index 71e64dd..bf455b3 100644 --- a/src/Decoder/AbstractJsonDecoder.php +++ b/src/Decoder/AbstractJsonDecoder.php @@ -32,8 +32,11 @@ abstract class AbstractJsonDecoder /** @var bool */ private $objectAsArray = false; + /** @var bool */ + private $validateDecodedData = true; + /** @return mixed */ - public function decode(string $json) + public function decode(string $json, bool $validateDecodedData = null) { $options = 0; if ($this->isBigIntAsString()) { @@ -53,7 +56,13 @@ public function decode(string $json) throw new JsonDecodeNullException($json); } - if (is_array($return)) { + if ( + is_array($return) + && ( + (is_bool($validateDecodedData) && $validateDecodedData) + || (is_bool($validateDecodedData) === false && $this->isValidateDecodedData()) + ) + ) { $optionResolver = new OptionsResolver(); $this->configureDecodedJson($optionResolver); $optionResolver->resolve($return); @@ -126,4 +135,16 @@ protected function isObjectAsArray(): bool { return $this->objectAsArray; } + + protected function setValidateDecodedData(bool $validate): self + { + $this->validateDecodedData = $validate; + + return $this; + } + + protected function isValidateDecodedData(): bool + { + return $this->validateDecodedData; + } }