Patch passing array to jsonToArroy not working.

This commit is contained in:
sc0Vu 2017-12-26 14:44:29 +08:00
parent 7b6e35108f
commit 63a748e4dc
2 changed files with 33 additions and 10 deletions

View File

@ -315,15 +315,31 @@ class Utils
$json = (array) $json; $json = (array) $json;
$typeName = []; $typeName = [];
if ($depth > 1) {
foreach ($json as $key => $param) { foreach ($json as $key => $param) {
if (is_array($param) && $depth > 1) { if (is_array($param)) {
foreach ($param as $subKey => $subParam) { foreach ($param as $subKey => $subParam) {
$json[$key][$subKey] = self::jsonToArray($subParam, $depth-1); $json[$key][$subKey] = self::jsonToArray($subParam, $depth-1);
} }
} elseif ($param instanceof stdClass) {
$json[$key] = self::jsonToArray($param, $depth-1);
}
} }
} }
return $json; 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.'); throw new InvalidArgumentException('jsonToArray json must be array or stdClass.');
} }
return $json; return $json;

View File

@ -44,7 +44,10 @@ class UtilsTest extends TestCase
], ],
"payable": false, "payable": false,
"stateMutability": "nonpayable", "stateMutability": "nonpayable",
"type": "function" "type": "function",
"test": {
"name": "testObject"
}
}'; }';
/** /**
@ -270,13 +273,17 @@ class UtilsTest extends TestCase
public function testJsonToArray() public function testJsonToArray()
{ {
$json = json_decode($this->testJsonMethodString); $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); $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);
} }
} }