From e25e0cc3f1f26ac46dd87638fe7219e9157f09a0 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Mon, 25 Dec 2017 11:19:14 +0800 Subject: [PATCH] jsonMethodToString --- src/Utils.php | 44 +++++++++++++++++++++++++++++++++++++ test/unit/UtilsTest.php | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/Utils.php b/src/Utils.php index e6a1d07..1792200 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -12,6 +12,7 @@ namespace Web3; use InvalidArgumentException; +use stdClass; use kornrunner\Keccak; use phpseclib\Math\BigInteger as BigNumber; @@ -251,4 +252,47 @@ class Utils return $bn->divide($bnt); } + + /** + * jsonMethodToString + * + * @param stdClass|array $json + * @return string + */ + public static function jsonMethodToString($json) + { + if ($json instanceof stdClass) { + // one way to change whole json stdClass to array type + // $jsonString = json_encode($json); + + // if (JSON_ERROR_NONE !== json_last_error()) { + // throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg()); + // } + // $json = json_decode($jsonString, true); + + // another way to change json to array type but not whole json stdClass + $json = (array) $json; + $typeName = []; + + foreach ($json['inputs'] as $param) { + if (isset($param->type)) { + $typeName[] = $param->type; + } + } + return $json['name'] . '(' . implode(',', $typeName) . ')'; + } elseif (!is_array($json)) { + throw new InvalidArgumentException('jsonMethodToString json must be array or stdClass.'); + } + if (isset($json['name']) && (bool) strpos('(', $json['name']) === true) { + return $json['name']; + } + $typeName = []; + + foreach ($json['inputs'] as $param) { + if (isset($param['type'])) { + $typeName[] = $param['type']; + } + } + return $json['name'] . '(' . implode(',', $typeName) . ')'; + } } \ No newline at end of file diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index c79307b..366d70d 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -17,6 +17,36 @@ class UtilsTest extends TestCase */ protected $testHex = '68656c6c6f20776f726c64'; + /** + * testJsonMethodString + * from GameToken approve function + * + * @var string + */ + protected $testJsonMethodString = '{ + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "success", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }'; + /** * setUp * @@ -213,4 +243,22 @@ class UtilsTest extends TestCase $this->assertEquals($bnq->toString(), '21'); $this->assertEquals($bnr->toString(), '16'); } + + /** + * testJsonMethodToString + * + * @return void + */ + public function testJsonMethodToString() + { + $json = json_decode($this->testJsonMethodString); + $methodString = Utils::jsonMethodToString($json); + + $this->assertEquals($methodString, 'approve(address,uint256)'); + + $json = json_decode($this->testJsonMethodString, true); + $methodString = Utils::jsonMethodToString($json); + + $this->assertEquals($methodString, 'approve(address,uint256)'); + } } \ No newline at end of file