From: Vincent Vanwaelscappel Date: Fri, 28 Jun 2019 13:45:34 +0000 (+0200) Subject: #2843 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=3c86e5f02f93c23aef201ecb01c9dd078e25a3df;p=cubist_util.git #2843 --- diff --git a/composer.json b/composer.json index 30224f2..801a253 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "ext-json": "*", "ext-iconv": "*", "zendframework/zend-filter": "^2.9@dev", - "zendframework/zend-json": "^3.1@dev", "cubist/net": "dev-master" } } diff --git a/src/Cubist/Util/ArrayUtil.php b/src/Cubist/Util/ArrayUtil.php index c27582a..7856123 100644 --- a/src/Cubist/Util/ArrayUtil.php +++ b/src/Cubist/Util/ArrayUtil.php @@ -113,7 +113,7 @@ class ArrayUtil { return $input; } if (is_object($input)) { - return Object::toArray($input); + return ObjectUtil::toArray($input); } return array($input); } diff --git a/src/Cubist/Util/Json.php b/src/Cubist/Util/Json.php index 2913b81..ac98a46 100644 --- a/src/Cubist/Util/Json.php +++ b/src/Cubist/Util/Json.php @@ -3,45 +3,74 @@ namespace Cubist\Util; -class Json extends \Zend\Json\Json +class Json { - /** - * - * @param mixed $v - * @return boolean - */ - public static function isJson($v) - { - if (!is_string($v)) { - return false; - } - - $v = trim($v); - - $firstchar = mb_substr($v, 0, 1); - $lastchar = mb_substr($v, -1); - return (($firstchar == '[' && $lastchar == ']') || ($firstchar == '{' && $lastchar == '}')); - } - - public static function decode($encodedValue, $objectDecodeType = \Zend\Json\Json::TYPE_OBJECT) - { - if ((is_array($encodedValue) && $objectDecodeType == \Zend\Json\Json::TYPE_ARRAY) || - (is_object($encodedValue) && $objectDecodeType == \Zend\Json\Json::TYPE_OBJECT) - ) { - return $encodedValue; - } else if (is_array($encodedValue) || is_object($encodedValue)) { - $encodedValue = self::encode($encodedValue); - } - try { - return parent::decode($encodedValue, $objectDecodeType); - } catch (\Exception $e) { - return null; - } - } - - public static function encode($valueToEncode, $cycleCheck = false, array $options = []) - { - return parent::encode($valueToEncode, $cycleCheck, $options); - } + const TYPE_OBJECT = false; + const TYPE_ARRAY = true; + + /** + * + * @param mixed $v + * @return boolean + */ + public static function isJson($v) + { + if (!is_string($v)) { + return false; + } + + $v = trim($v); + + $firstchar = mb_substr($v, 0, 1); + $lastchar = mb_substr($v, -1); + return (($firstchar == '[' && $lastchar == ']') || ($firstchar == '{' && $lastchar == '}')); + } + + public static function decode($encodedValue, $objectDecodeType = self::TYPE_OBJECT) + { + if ((is_array($encodedValue) && $objectDecodeType == self::TYPE_ARRAY) || + (is_object($encodedValue) && $objectDecodeType == self::TYPE_OBJECT) + ) { + return $encodedValue; + } else if (is_array($encodedValue) || is_object($encodedValue)) { + $encodedValue = self::encode($encodedValue); + } + try { + return parent::decode($encodedValue, $objectDecodeType); + } catch (\Exception $e) { + return null; + } + } + + public static function encode($valueToEncode) + { + return json_encode($valueToEncode); + } + + public static function decodeRecursive($encodedValue, $objectDecodeType = self::TYPE_OBJECT) + { + $v = $encodedValue; + + if (is_string($encodedValue)) { + $v = json_decode($encodedValue); + if (!$v) { + return $encodedValue; + } + } + + $v = ArrayUtil::asArray($v); + + array_walk_recursive($v, function (&$v, $k) { + $v_decoded = json_decode($v, true); + if ($v_decoded) { + $v = $v_decoded; + } + }); + + if ($objectDecodeType == self::TYPE_OBJECT) { + return ObjectUtil::asObject($v); + } + return $v; + } } \ No newline at end of file