Web3 __call() and provider interface.
This commit is contained in:
parent
917f08e74f
commit
1a55f03f88
@ -31,19 +31,18 @@ class HttpProvider extends Provider implements IProvider
|
|||||||
/**
|
/**
|
||||||
* send
|
* send
|
||||||
*
|
*
|
||||||
* @param string $method
|
* @param \Web3\Methods\Method $method
|
||||||
* @param array $arguments
|
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function send($method, $arguments, $callback)
|
public function send($method, $callback)
|
||||||
{
|
{
|
||||||
$rpc = $this->createRpc($method, $arguments);
|
$payload = $method->toPayloadString();
|
||||||
|
|
||||||
if (!$this->isBatch) {
|
if (!$this->isBatch) {
|
||||||
$this->requestManager->sendPayload(json_encode($rpc), $callback);
|
$this->requestManager->sendPayload($payload, $callback);
|
||||||
} else {
|
} else {
|
||||||
$this->batch[] = json_encode($rpc);
|
$this->batch[] = $payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,12 +16,11 @@ interface IProvider
|
|||||||
/**
|
/**
|
||||||
* send
|
* send
|
||||||
*
|
*
|
||||||
* @param string $method
|
* @param \Web3\Methods\Method $method
|
||||||
* @param array $arguments
|
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function send($method, $arguments, $callback);
|
public function send($method, $callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* batch
|
* batch
|
||||||
|
66
src/Web3.php
66
src/Web3.php
@ -63,15 +63,15 @@ class Web3
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $methods = [
|
private $methods = [];
|
||||||
'web3_clientVersion' => [],
|
|
||||||
'web3_sha3' => [
|
/**
|
||||||
'params' => [
|
* allowedMethods
|
||||||
[
|
*
|
||||||
'validators' => HexValidator::class
|
* @var array
|
||||||
]
|
*/
|
||||||
]
|
private $allowedMethods = [
|
||||||
]
|
'web3_clientVersion', 'web3_sha3'
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,11 +112,9 @@ class Web3
|
|||||||
if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
||||||
$method = strtolower($class[1]) . '_' . $name;
|
$method = strtolower($class[1]) . '_' . $name;
|
||||||
|
|
||||||
if (!array_key_exists($method, $this->methods)) {
|
if (!in_array($method, $this->allowedMethods)) {
|
||||||
throw new \RuntimeException('Unallowed rpc method: ' . $method);
|
throw new \RuntimeException('Unallowed rpc method: ' . $method);
|
||||||
}
|
}
|
||||||
$allowedMethod = $this->methods[$method];
|
|
||||||
|
|
||||||
if ($this->provider->isBatch) {
|
if ($this->provider->isBatch) {
|
||||||
$callback = null;
|
$callback = null;
|
||||||
} else {
|
} else {
|
||||||
@ -126,43 +124,15 @@ class Web3
|
|||||||
throw new \InvalidArgumentException('The last param must be callback function.');
|
throw new \InvalidArgumentException('The last param must be callback function.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) {
|
if (!array_key_exists($method, $this->methods)) {
|
||||||
// validate params
|
// new the method
|
||||||
foreach ($allowedMethod['params'] as $key => $param) {
|
$methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
|
||||||
if (isset($param['validators'])) {
|
$methodObject = new $methodClass($method, $arguments);
|
||||||
if (is_array($param['validators'])) {
|
|
||||||
$isError = true;
|
|
||||||
|
|
||||||
foreach ($param['validators'] as $rule) {
|
|
||||||
if (isset($arguments[$key])) {
|
|
||||||
if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) {
|
|
||||||
$isError = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (isset($param['default'])) {
|
|
||||||
$arguments[$key] = $param['default'];
|
|
||||||
$isError = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($isError === true) {
|
|
||||||
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) {
|
|
||||||
if (isset($param['default']) && !isset($arguments[$key])) {
|
|
||||||
$arguments[$key] = $param['default'];
|
|
||||||
} else {
|
|
||||||
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$this->provider->send($method, $arguments, $callback);
|
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
|
||||||
|
$methodObject->arguments = $inputs;
|
||||||
|
|
||||||
|
$this->provider->send($methodObject, $callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user