Input formatters for each method.

This commit is contained in:
sc0Vu 2018-01-11 13:03:39 +08:00
parent 2340db233b
commit ee8db87fdf
5 changed files with 61 additions and 29 deletions

View File

@ -14,6 +14,7 @@ namespace Web3\Methods\Net;
use InvalidArgumentException;
use Web3\Methods\IMethod;
use Web3\Methods\JSONRPC;
use Web3\Formatters\BigNumberFormatter;
class PeerCount extends JSONRPC implements IMethod
{
@ -29,7 +30,9 @@ class PeerCount extends JSONRPC implements IMethod
*
* @var array
*/
protected $outputFormatters = [];
protected $outputFormatters = [
BigNumberFormatter::class
];
/**
* construct

View File

@ -17,6 +17,13 @@ use Web3\RequestManagers\RequestManager;
class HttpProvider extends Provider implements IProvider
{
/**
* methods
*
* @var array
*/
protected $methods = [];
/**
* construct
*
@ -40,8 +47,21 @@ class HttpProvider extends Provider implements IProvider
$payload = $method->toPayloadString();
if (!$this->isBatch) {
$this->requestManager->sendPayload($payload, $callback);
$proxy = function ($err, $res) use ($method, $callback) {
if ($err !== null) {
return call_user_func($callback, $err, null);
}
if (!is_array($res)) {
$res = $method->transform([$res], $method->outputFormatters);
return call_user_func($callback, null, $res[0]);
}
$res = $method->transform($res, $method->outputFormatters);
return call_user_func($callback, null, $res);
};
$this->requestManager->sendPayload($payload, $proxy);
} else {
$this->methods[] = $method;
$this->batch[] = $payload;
}
}
@ -70,30 +90,26 @@ class HttpProvider extends Provider implements IProvider
if (!$this->isBatch) {
throw new \RuntimeException('Please batch json rpc first.');
}
$this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $callback);
$methods = $this->methods;
$proxy = function ($err, $res) use ($methods, $callback) {
if ($err !== null) {
return call_user_func($callback, $err, null);
}
foreach ($methods as $key => $method) {
if (isset($res[$key])) {
if (!is_array($res[$key])) {
$transformed = $method->transform([$res[$key]], $method->outputFormatters);
$res[$key] = $transformed[0];
} else {
$transformed = $method->transform($res[$key], $method->outputFormatters);
$res[$key] = $transformed;
}
}
}
return call_user_func($callback, null, $res);
};
$this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
$this->methods[] = [];
$this->batch = [];
}
/**
* createRpc
*
* @param string $rpc
* @param array $arguments
* @return array
*/
protected function createRpc($rpc, $arguments)
{
$this->id += 1;
$rpc = [
'id' => $this->id,
'jsonrpc' => $this->rpcVersion,
'method' => $rpc
];
if (count($arguments) > 0) {
$rpc['params'] = $arguments;
}
return $rpc;
}
}

View File

@ -4,6 +4,7 @@ namespace Test\Unit;
use RuntimeException;
use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber;
class NetApiTest extends TestCase
{
@ -56,7 +57,7 @@ class NetApiTest extends TestCase
if ($err !== null) {
return $this->fail($err->getMessage());
}
$this->assertTrue(is_string($count));
$this->assertTrue($count instanceof BigNumber);
});
}

View File

@ -4,6 +4,7 @@ namespace Test\Unit;
use RuntimeException;
use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber;
class NetBatchTest extends TestCase
{
@ -38,6 +39,7 @@ class NetBatchTest extends TestCase
$net->batch(true);
$net->version();
$net->listening();
$net->peerCount();
$net->provider->execute(function ($err, $data) {
if ($err !== null) {
@ -45,6 +47,7 @@ class NetBatchTest extends TestCase
}
$this->assertTrue(is_string($data[0]));
$this->assertTrue(is_bool($data[1]));
$this->assertTrue($data[2] instanceof BigNumber);
});
}
}

View File

@ -65,6 +65,13 @@ class Web3ApiTest extends TestCase
}
$this->assertEquals($hash, $this->testHash);
});
$web3->sha3('hello world', function ($err, $hash) {
if ($err !== null) {
return $this->fail($err->getMessage());
}
$this->assertEquals($hash, $this->testHash);
});
}
/**
@ -88,6 +95,8 @@ class Web3ApiTest extends TestCase
/**
* testWrongParam
* We transform data and throw invalid argument exception
* instead of runtime exception.
*
* @return void
*/
@ -97,7 +106,7 @@ class Web3ApiTest extends TestCase
$web3 = $this->web3;
$web3->sha3('hello world', function ($err, $hash) {
$web3->sha3($web3, function ($err, $hash) {
if ($err !== null) {
return $this->fail($err->getMessage());
}