commit
fbf2ba4f97
@ -115,9 +115,18 @@ class Contract
|
||||
} else if ($provider instanceof 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 ($item['type'] === 'function') {
|
||||
$this->functions[$item['name']] = $item;
|
||||
@ -128,7 +137,7 @@ class Contract
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->abi = $abi;
|
||||
$this->abi = $abiArray;
|
||||
$this->eth = new Eth($this->provider);
|
||||
$this->ethabi = new Ethabi([
|
||||
'address' => new Address,
|
||||
@ -359,9 +368,18 @@ class Contract
|
||||
if (StringValidator::validate($abi) === false) {
|
||||
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 ($item['type'] === 'function') {
|
||||
$this->functions[$item['name']] = $item;
|
||||
@ -372,7 +390,7 @@ class Contract
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->abi = $abi;
|
||||
$this->abi = $abiArray;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -422,52 +422,34 @@ class Utils
|
||||
/**
|
||||
* jsonToArray
|
||||
*
|
||||
* @param stdClass|array|string $json
|
||||
* @param int $depth
|
||||
* @param stdClass|array $json
|
||||
* @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) {
|
||||
$json = (array) $json;
|
||||
$typeName = [];
|
||||
|
||||
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);
|
||||
foreach ($json as $key => $param) {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $subKey => $subParam) {
|
||||
$json[$key][$subKey] = self::jsonToArray($subParam);
|
||||
}
|
||||
} elseif ($param instanceof stdClass) {
|
||||
$json[$key] = self::jsonToArray($param);
|
||||
}
|
||||
}
|
||||
return $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);
|
||||
foreach ($json as $key => $param) {
|
||||
if (is_array($param)) {
|
||||
foreach ($param as $subKey => $subParam) {
|
||||
$json[$key][$subKey] = self::jsonToArray($subParam);
|
||||
}
|
||||
} elseif ($param instanceof stdClass) {
|
||||
$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;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use stdClass;
|
||||
use Test\TestCase;
|
||||
use phpseclib\Math\BigInteger as BigNumber;
|
||||
use Web3\Utils;
|
||||
use Web3\Contract;
|
||||
|
||||
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
|
||||
*
|
||||
@ -418,39 +498,16 @@ class UtilsTest extends TestCase
|
||||
*/
|
||||
public function testJsonToArray()
|
||||
{
|
||||
$json = json_decode($this->testJsonMethodString);
|
||||
$jsonArrayDepth1 = Utils::jsonToArray($json);
|
||||
|
||||
$this->assertEquals($jsonArrayDepth1, (array) $json);
|
||||
|
||||
$decodedJson = json_decode($this->testJsonMethodString);
|
||||
$jsonArray = Utils::jsonToArray($decodedJson);
|
||||
$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);
|
||||
|
||||
$jsonArrayDepth2 = Utils::jsonToArray($jsonArrayDepth1, 2);
|
||||
$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);
|
||||
}
|
||||
$jsonAssoc = json_decode($this->testIssue112Json, true);
|
||||
$jsonArray = Utils::jsonToArray($jsonAssoc);
|
||||
$this->assertEquals($jsonAssoc, $jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user