Net __call.

This commit is contained in:
sc0Vu 2018-01-09 17:53:53 +08:00
parent 15506cc2b2
commit 8ed8d69362
2 changed files with 19 additions and 45 deletions

View File

@ -22,8 +22,7 @@ class ClientVersion extends JSONRPC implements IMethod
*
* @var array
*/
protected $inputFormatters = [
];
protected $inputFormatters = [];
/**
* outputFormatters

View File

@ -30,10 +30,15 @@ class Net
*
* @var array
*/
private $methods = [
'net_version' => [],
'net_peerCount' => [],
'net_listening' => [],
private $methods = [];
/**
* allowedMethods
*
* @var array
*/
private $allowedMethods = [
'net_version', 'net_peerCount', 'net_listening'
];
/**
@ -74,11 +79,9 @@ class Net
if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$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);
}
$allowedMethod = $this->methods[$method];
if ($this->provider->isBatch) {
$callback = null;
} else {
@ -88,43 +91,15 @@ class Net
throw new \InvalidArgumentException('The last param must be callback function.');
}
}
if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) {
// validate params
foreach ($allowedMethod['params'] as $key => $param) {
if (isset($param['validators'])) {
if (is_array($param['validators'])) {
$isError = true;
if (!array_key_exists($method, $this->methods)) {
// new the method
$methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
$methodObject = new $methodClass($method, $arguments);
}
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
$methodObject->arguments = $inputs;
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);
$this->provider->send($methodObject, $callback);
}
}