diff --git a/src/Utils.php b/src/Utils.php index 05a2f43..d623b58 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -315,15 +315,31 @@ class Utils $json = (array) $json; $typeName = []; - foreach ($json as $key => $param) { - if (is_array($param) && $depth > 1) { - foreach ($param as $subKey => $subParam) { - $json[$key][$subKey] = self::jsonToArray($subParam, $depth-1); + if ($depth > 1) { + foreach ($json as $key => $param) { + if (is_array($param)) { + foreach ($param as $subKey => $subParam) { + $json[$key][$subKey] = self::jsonToArray($subParam, $depth-1); + } + } elseif ($param instanceof stdClass) { + $json[$key] = self::jsonToArray($param, $depth-1); } } } return $json; - } elseif (!is_array($json)) { + } elseif (is_array($json)) { + if ($depth > 1) { + foreach ($json as $key => $param) { + if (is_array($param)) { + foreach ($param as $subKey => $subParam) { + $json[$key][$subKey] = self::jsonToArray($subParam, $depth-1); + } + } elseif ($param instanceof stdClass) { + $json[$key] = self::jsonToArray($param, $depth-1); + } + } + } + } else { throw new InvalidArgumentException('jsonToArray json must be array or stdClass.'); } return $json; diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 1587cfb..529884d 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -44,7 +44,10 @@ class UtilsTest extends TestCase ], "payable": false, "stateMutability": "nonpayable", - "type": "function" + "type": "function", + "test": { + "name": "testObject" + } }'; /** @@ -270,13 +273,17 @@ class UtilsTest extends TestCase public function testJsonToArray() { $json = json_decode($this->testJsonMethodString); - $jsonArray = Utils::jsonToArray($json); + $jsonArrayDepth1 = Utils::jsonToArray($json); - $this->assertEquals($jsonArray, (array) $json); + $this->assertEquals($jsonArrayDepth1, (array) $json); $jsonAssoc = json_decode($this->testJsonMethodString, true); - $jsonArray = Utils::jsonToArray($json, 2); + $jsonArrayDepth2 = Utils::jsonToArray($json, 2); - $this->assertEquals($jsonArray, $jsonAssoc); + $this->assertEquals($jsonArrayDepth2, $jsonAssoc); + + $jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2); + + $this->assertEquals($jsonArrayDepth2, $jsonAssoc); } } \ No newline at end of file