Change params in Web3\Eth and add default value
This commit is contained in:
parent
bdfbace76b
commit
9a6ba5c92c
62
src/Eth.php
62
src/Eth.php
@ -35,15 +35,28 @@ class Eth
|
||||
'eth_blockNumber' => [],
|
||||
'eth_getBalance' => [
|
||||
'params'=> [
|
||||
AddressValidator::class,
|
||||
TagValidator::class
|
||||
[
|
||||
'validators' => AddressValidator::class,
|
||||
], [
|
||||
'default' => 'latest',
|
||||
'validators' => [
|
||||
TagValidator::class, QuantityValidator::class,
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
'eth_getStorageAt' => [
|
||||
'params'=> [
|
||||
AddressValidator::class,
|
||||
QuantityValidator::class,
|
||||
TagValidator::class
|
||||
[
|
||||
'validators' => AddressValidator::class,
|
||||
], [
|
||||
'validators' => QuantityValidator::class,
|
||||
], [
|
||||
'default' => 'latest',
|
||||
'validators' => [
|
||||
TagValidator::class, QuantityValidator::class,
|
||||
]
|
||||
],
|
||||
]
|
||||
],
|
||||
];
|
||||
@ -91,24 +104,43 @@ class Eth
|
||||
}
|
||||
$allowedMethod = $this->methods[$method];
|
||||
|
||||
if (isset($allowedMethod['params'])) {
|
||||
// validate params
|
||||
foreach ($allowedMethod['params'] as $key => $rule) {
|
||||
if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) {
|
||||
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->provider->isBatch) {
|
||||
$this->provider->send($method, $arguments, null);
|
||||
$callback = null;
|
||||
} else {
|
||||
$callback = array_pop($arguments);
|
||||
|
||||
if (is_callable($callback) !== true) {
|
||||
throw new \InvalidArgumentException('The last param must be callback function.');
|
||||
}
|
||||
$this->provider->send($method, $arguments, $callback);
|
||||
}
|
||||
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'])) {
|
||||
foreach ($param['validators'] as $rule) {
|
||||
if (!isset($arguments[$key]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) {
|
||||
if (isset($param['default']) && !isset($arguments[$key])) {
|
||||
$arguments[$key] = $param['default'];
|
||||
break;
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,6 @@ class TagValidator
|
||||
'latest', 'earliest', 'pending'
|
||||
];
|
||||
|
||||
// maybe change in_int and preg_match future
|
||||
return (is_int($value) || preg_match('/^0x[a-fA-f0-9]+/', $value) >= 1 || in_array($value, $tags));
|
||||
return in_array($value, $tags);
|
||||
}
|
||||
}
|
39
src/Web3.php
39
src/Web3.php
@ -84,13 +84,32 @@ class Web3
|
||||
}
|
||||
$allowedMethod = $this->methods[$method];
|
||||
|
||||
if ($this->provider->isBatch) {
|
||||
$callback = null;
|
||||
} else {
|
||||
$callback = array_pop($arguments);
|
||||
|
||||
if (is_callable($callback) !== true) {
|
||||
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'])) {
|
||||
foreach ($param['validators'] as $rule) {
|
||||
if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) {
|
||||
if (!isset($arguments[$key]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) {
|
||||
if (isset($param['default']) && !isset($arguments[$key])) {
|
||||
$arguments[$key] = $param['default'];
|
||||
break;
|
||||
} else {
|
||||
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 {
|
||||
@ -98,30 +117,12 @@ class Web3
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (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 ($this->provider->isBatch) {
|
||||
$this->provider->send($method, $arguments, null);
|
||||
} else {
|
||||
$callback = array_pop($arguments);
|
||||
|
||||
if (is_callable($callback) !== true) {
|
||||
throw new \InvalidArgumentException('The last param must be callback function.');
|
||||
}
|
||||
$this->provider->send($method, $arguments, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get
|
||||
|
@ -208,7 +208,7 @@ class EthTest extends TestCase
|
||||
{
|
||||
$eth = $this->web3->eth;
|
||||
|
||||
$eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'latest', function ($err, $balance) {
|
||||
$eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $balance) {
|
||||
if ($err !== null) {
|
||||
return $this->fail($err->getMessage());
|
||||
}
|
||||
@ -229,7 +229,7 @@ class EthTest extends TestCase
|
||||
{
|
||||
$eth = $this->web3->eth;
|
||||
|
||||
$eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', 'latest', function ($err, $storage) {
|
||||
$eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', function ($err, $storage) {
|
||||
if ($err !== null) {
|
||||
return $this->fail($err->getMessage());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user