Net __call.
This commit is contained in:
parent
15506cc2b2
commit
8ed8d69362
@ -22,8 +22,7 @@ class ClientVersion extends JSONRPC implements IMethod
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $inputFormatters = [
|
protected $inputFormatters = [];
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* outputFormatters
|
* outputFormatters
|
||||||
|
61
src/Net.php
61
src/Net.php
@ -30,10 +30,15 @@ class Net
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $methods = [
|
private $methods = [];
|
||||||
'net_version' => [],
|
|
||||||
'net_peerCount' => [],
|
/**
|
||||||
'net_listening' => [],
|
* 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) {
|
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 {
|
||||||
@ -88,43 +91,15 @@ class Net
|
|||||||
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