]> _ Git - cubist_util.git/commitdiff
#2843
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 28 Jun 2019 13:45:34 +0000 (15:45 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 28 Jun 2019 13:45:34 +0000 (15:45 +0200)
composer.json
src/Cubist/Util/ArrayUtil.php
src/Cubist/Util/Json.php

index 30224f210a5e2c9d1f7f4b7b1c131166a67bebd1..801a253a23e308d6712b1d6ae003321bdc9efdcf 100644 (file)
@@ -29,7 +29,6 @@
     "ext-json": "*",
     "ext-iconv": "*",
     "zendframework/zend-filter": "^2.9@dev",
-    "zendframework/zend-json": "^3.1@dev",
     "cubist/net": "dev-master"
   }
 }
index c27582a2ffa0bec992d16a78cf46ebdf827b59cc..78561232174533c8237dbb8489a7b75991610cd6 100644 (file)
@@ -113,7 +113,7 @@ class ArrayUtil {
                        return $input;
                }
                if (is_object($input)) {
-                       return Object::toArray($input);
+                       return ObjectUtil::toArray($input);
                }
                return array($input);
        }
index 2913b815fa9848487eac7fc755a929c61a4ff3e7..ac98a46c831cb18dfbe2f12598fab1b52fd8217a 100644 (file)
@@ -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