Remove depth for jsonToArray function
This commit is contained in:
sc0Vu 2019-01-13 14:07:31 +08:00
parent c5fe156d97
commit 83188b171e
No known key found for this signature in database
GPG Key ID: A53323E7065B1FA7
3 changed files with 126 additions and 69 deletions

View File

@ -115,9 +115,18 @@ class Contract
} else if ($provider instanceof Provider) { } else if ($provider instanceof Provider) {
$this->provider = $provider; $this->provider = $provider;
} }
$abi = Utils::jsonToArray($abi, 5);
foreach ($abi as $item) { $abiArray = [];
if (is_string($abi)) {
$abiArray = json_decode($abi, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('abi decode error: ' . json_last_error_msg());
}
} else {
$abiArray = Utils::jsonToArray($abi);
}
foreach ($abiArray as $item) {
if (isset($item['type'])) { if (isset($item['type'])) {
if ($item['type'] === 'function') { if ($item['type'] === 'function') {
$this->functions[$item['name']] = $item; $this->functions[$item['name']] = $item;
@ -128,7 +137,7 @@ class Contract
} }
} }
} }
$this->abi = $abi; $this->abi = $abiArray;
$this->eth = new Eth($this->provider); $this->eth = new Eth($this->provider);
$this->ethabi = new Ethabi([ $this->ethabi = new Ethabi([
'address' => new Address, 'address' => new Address,
@ -359,9 +368,18 @@ class Contract
if (StringValidator::validate($abi) === false) { if (StringValidator::validate($abi) === false) {
throw new InvalidArgumentException('Please make sure abi is valid.'); throw new InvalidArgumentException('Please make sure abi is valid.');
} }
$abi = Utils::jsonToArray($abi, 5); $abiArray = [];
if (is_string($abi)) {
$abiArray = json_decode($abi, true);
foreach ($abi as $item) { if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('abi decode error: ' . json_last_error_msg());
}
} else {
$abiArray = Utils::jsonToArray($abi);
}
foreach ($abiArray as $item) {
if (isset($item['type'])) { if (isset($item['type'])) {
if ($item['type'] === 'function') { if ($item['type'] === 'function') {
$this->functions[$item['name']] = $item; $this->functions[$item['name']] = $item;
@ -372,7 +390,7 @@ class Contract
} }
} }
} }
$this->abi = $abi; $this->abi = $abiArray;
return $this; return $this;
} }

View File

@ -422,53 +422,35 @@ class Utils
/** /**
* jsonToArray * jsonToArray
* *
* @param stdClass|array|string $json * @param stdClass|array $json
* @param int $depth
* @return array * @return array
*/ */
public static function jsonToArray($json, $depth=1) public static function jsonToArray($json)
{ {
if (!is_int($depth) || $depth <= 0) {
throw new InvalidArgumentException('jsonToArray depth must be int and depth must bigger than 0.');
}
if ($json instanceof stdClass) { if ($json instanceof stdClass) {
$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)) { 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);
} }
} elseif ($param instanceof stdClass) { } elseif ($param instanceof stdClass) {
$json[$key] = self::jsonToArray($param, $depth-1); $json[$key] = self::jsonToArray($param);
} }
} }
}
return $json;
} elseif (is_array($json)) { } elseif (is_array($json)) {
if ($depth > 1) {
foreach ($json as $key => $param) { foreach ($json as $key => $param) {
if (is_array($param)) { 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);
} }
} elseif ($param instanceof stdClass) { } elseif ($param instanceof stdClass) {
$json[$key] = self::jsonToArray($param, $depth-1); $json[$key] = self::jsonToArray($param);
} }
} }
} }
} elseif (is_string($json)) {
$json = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
}
return $json;
} else {
throw new InvalidArgumentException('The json param to jsonToArray must be array or stdClass or string.');
}
return $json; return $json;
} }

View File

@ -7,6 +7,7 @@ use stdClass;
use Test\TestCase; use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber; use phpseclib\Math\BigInteger as BigNumber;
use Web3\Utils; use Web3\Utils;
use Web3\Contract;
class UtilsTest extends TestCase class UtilsTest extends TestCase
{ {
@ -52,6 +53,85 @@ class UtilsTest extends TestCase
} }
}'; }';
/**
* testIssue112Json
* see: https://github.com/sc0Vu/web3.php/issues/112
*
* @var string
*/
protected $testIssue112Json = '[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "tokenOwner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "tokens",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]';
/** /**
* setUp * setUp
* *
@ -418,39 +498,16 @@ class UtilsTest extends TestCase
*/ */
public function testJsonToArray() public function testJsonToArray()
{ {
$json = json_decode($this->testJsonMethodString); $decodedJson = json_decode($this->testJsonMethodString);
$jsonArrayDepth1 = Utils::jsonToArray($json); $jsonArray = Utils::jsonToArray($decodedJson);
$this->assertEquals($jsonArrayDepth1, (array) $json);
$jsonAssoc = json_decode($this->testJsonMethodString, true); $jsonAssoc = json_decode($this->testJsonMethodString, true);
$jsonArrayDepth2 = Utils::jsonToArray($json, 2); $jsonArray2 = Utils::jsonToArray($jsonAssoc);
$this->assertEquals($jsonAssoc, $jsonArray);
$this->assertEquals($jsonAssoc, $jsonArray2);
$this->assertEquals($jsonArrayDepth2, $jsonAssoc); $jsonAssoc = json_decode($this->testIssue112Json, true);
$jsonArray = Utils::jsonToArray($jsonAssoc);
$jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2); $this->assertEquals($jsonAssoc, $jsonArray);
$this->assertEquals($jsonArrayDepth2, $jsonAssoc);
$jsonArray = Utils::jsonToArray($this->testJsonMethodString);
$this->assertEquals($jsonArray, $jsonAssoc);
try {
$jsonArray = Utils::jsonToArray($json, 0);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
$jsonArray = Utils::jsonToArray(mb_substr($this->testJsonMethodString, 0, 50), 1);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
try {
$jsonArray = Utils::jsonToArray(0, 1);
} catch (InvalidArgumentException $e) {
$this->assertTrue($e !== null);
}
} }
/** /**