Change params in Web3\Eth and add default value

This commit is contained in:
sc0Vu 2017-12-13 18:05:53 +08:00
parent bdfbace76b
commit 9a6ba5c92c
4 changed files with 64 additions and 32 deletions

View File

@ -35,15 +35,28 @@ class Eth
'eth_blockNumber' => [], 'eth_blockNumber' => [],
'eth_getBalance' => [ 'eth_getBalance' => [
'params'=> [ 'params'=> [
AddressValidator::class, [
TagValidator::class 'validators' => AddressValidator::class,
], [
'default' => 'latest',
'validators' => [
TagValidator::class, QuantityValidator::class,
]
]
] ]
], ],
'eth_getStorageAt' => [ 'eth_getStorageAt' => [
'params'=> [ 'params'=> [
AddressValidator::class, [
QuantityValidator::class, 'validators' => AddressValidator::class,
TagValidator::class ], [
'validators' => QuantityValidator::class,
], [
'default' => 'latest',
'validators' => [
TagValidator::class, QuantityValidator::class,
]
],
] ]
], ],
]; ];
@ -91,24 +104,43 @@ class Eth
} }
$allowedMethod = $this->methods[$method]; $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) { if ($this->provider->isBatch) {
$this->provider->send($method, $arguments, null); $callback = null;
} else { } else {
$callback = array_pop($arguments); $callback = array_pop($arguments);
if (is_callable($callback) !== true) { if (is_callable($callback) !== true) {
throw new \InvalidArgumentException('The last param must be callback function.'); 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);
} }
} }

View File

@ -18,7 +18,6 @@ class TagValidator
'latest', 'earliest', 'pending' 'latest', 'earliest', 'pending'
]; ];
// maybe change in_int and preg_match future return in_array($value, $tags);
return (is_int($value) || preg_match('/^0x[a-fA-f0-9]+/', $value) >= 1 || in_array($value, $tags));
} }
} }

View File

@ -84,23 +84,33 @@ class Web3
} }
$allowedMethod = $this->methods[$method]; $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'])) { if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) {
// validate params // validate params
foreach ($allowedMethod['params'] as $key => $param) { foreach ($allowedMethod['params'] as $key => $param) {
if (isset($param['validators'])) { if (isset($param['validators'])) {
if (is_array($param['validators'])) { if (is_array($param['validators'])) {
foreach ($param['validators'] as $rule) { 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])) { if (isset($param['default']) && !isset($arguments[$key])) {
$arguments[$key] = $param['default']; $arguments[$key] = $param['default'];
break;
} else { } else {
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
} }
} }
} }
} else { } else {
if (call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) { if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) {
if (!isset($param['default']) && !isset($arguments[$key])) { if (isset($param['default']) && !isset($arguments[$key])) {
$arguments[$key] = $param['default']; $arguments[$key] = $param['default'];
} else { } else {
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
@ -110,16 +120,7 @@ class Web3
} }
} }
} }
if ($this->provider->isBatch) { $this->provider->send($method, $arguments, $callback);
$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);
}
} }
} }

View File

@ -208,7 +208,7 @@ class EthTest extends TestCase
{ {
$eth = $this->web3->eth; $eth = $this->web3->eth;
$eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', 'latest', function ($err, $balance) { $eth->getBalance('0x407d73d8a49eeb85d32cf465507dd71d507100c1', function ($err, $balance) {
if ($err !== null) { if ($err !== null) {
return $this->fail($err->getMessage()); return $this->fail($err->getMessage());
} }
@ -229,7 +229,7 @@ class EthTest extends TestCase
{ {
$eth = $this->web3->eth; $eth = $this->web3->eth;
$eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', 'latest', function ($err, $storage) { $eth->getStorageAt('0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x0', function ($err, $storage) {
if ($err !== null) { if ($err !== null) {
return $this->fail($err->getMessage()); return $this->fail($err->getMessage());
} }