From 68a049fadd21fda5f92b3d29fe62364e750ed18c Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 24 Apr 2019 09:46:33 +0300 Subject: [PATCH] Using clean JSON instead of YAML #123 #117 --- flextype/parsers/JsonParser.php | 118 ++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 flextype/parsers/JsonParser.php diff --git a/flextype/parsers/JsonParser.php b/flextype/parsers/JsonParser.php new file mode 100644 index 00000000..9026fdcd --- /dev/null +++ b/flextype/parsers/JsonParser.php @@ -0,0 +1,118 @@ + + * @link http://romanenko.digital + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +class JsonParser { + + /** + * Encode options + * + * Bitmask consisting of encode options + * https://www.php.net/manual/en/function.json-encode.php + * + * @var int + */ + public static $encode_options = 0; + + /** + * Encode Depth + * + * Set the maximum depth. + * + * @var int + */ + public static $encode_depth = 512; + + /** + * Decode assoc + * + * Set the maximum depth. + * + * @var int + */ + public static $decode_assoc = true; + + /** + * Decode Depth + * + * Set the maximum depth. + * + * @var int + */ + public static $decode_depth = 512; + + /** + * Decode options + * + * Bitmask consisting of decode options + * https://www.php.net/manual/en/function.json-decode.php + * + * @var int + */ + public static $decode_options = 0; + + /** + * Returns the JSON representation of a value + * + * $result = JsonParser::encode($json_content); + * + * @param mixed $input A string containing JSON + * @param int $encode_depth User specified recursion depth. + * @param bool $encode_options Bitmask consisting of encode options. + * @return mixed The JSON converted to a PHP value + * + */ + public static function encode($input, int $encode_options = 0, int $encode_depth = 512) : string + { + $encoded = @json_encode( + $input, + $encode_options ? $encode_options : JsonParser::$encode_options, + $encode_depth ? $encode_depth : JsonParser::$encode_depth + ); + + if ($encoded === false) { + throw new \RuntimeException('Encoding JSON failed'); + } + + return $encoded; + } + + /** + * Takes a JSON encoded string and converts it into a PHP variable. + * + * $array = JsonParser::decode($json_file_content); + * + * @param string $input A string containing JSON + * @param int $decode_assoc When TRUE, returned objects will be converted into associative arrays. + * @param int $decode_depth User specified recursion depth. + * @param bool $decode_options Bitmask consisting of decode options. + * @return mixed The JSON converted to a PHP value + * + * @throws ParseException If the JSON is not valid + */ + public static function decode(string $input, bool $decode_assoc = true, int $decode_depth = 512, int $decode_options = 0) + { + $decoded = @json_decode( + $input, + $decode_assoc ? $decode_assoc : JsonParser::$decode_assoc, + $decode_depth ? $decode_depth : JsonParser::$decode_depth, + $decode_options ? $decode_options : JsonParser::$decode_options + ); + + if ($decoded === false) { + throw new \RuntimeException('Decoding JSON failed'); + } + + return $decoded; + } +}