diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php index d044265..5337223 100644 --- a/src/Methods/Web3/ClientVersion.php +++ b/src/Methods/Web3/ClientVersion.php @@ -22,8 +22,7 @@ class ClientVersion extends JSONRPC implements IMethod * * @var array */ - protected $inputFormatters = [ - ]; + protected $inputFormatters = []; /** * outputFormatters diff --git a/src/Net.php b/src/Net.php index 549e3e1..4ddf6a4 100644 --- a/src/Net.php +++ b/src/Net.php @@ -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; - - 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 . '.'); - } - } - } - } - } + 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); } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } }