From 2f7a9f901b0e7a1d840040e2a3e492a45d63e8f3 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Wed, 13 Dec 2017 21:58:23 +0800 Subject: [PATCH] Add BlockHashValidator and fix validators. --- src/Eth.php | 19 ++++++++++++++----- src/Validators/BlockHashValidator.php | 22 ++++++++++++++++++++++ src/Web3.php | 19 ++++++++++++++----- test/unit/EthTest.php | 1 - 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/Validators/BlockHashValidator.php diff --git a/src/Eth.php b/src/Eth.php index be4cadf..78dad86 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -148,16 +148,25 @@ class Eth 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]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) { - if (isset($param['default']) && !isset($arguments[$key])) { - $arguments[$key] = $param['default']; + 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; - } else { - throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); } } } + 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])) { diff --git a/src/Validators/BlockHashValidator.php b/src/Validators/BlockHashValidator.php new file mode 100644 index 0000000..04cd7c2 --- /dev/null +++ b/src/Validators/BlockHashValidator.php @@ -0,0 +1,22 @@ += 1); + } +} \ No newline at end of file diff --git a/src/Web3.php b/src/Web3.php index 383d603..f185ce2 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -98,16 +98,25 @@ class Web3 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]) || call_user_func([$rule, 'validate'], $arguments[$key]) === false) { - if (isset($param['default']) && !isset($arguments[$key])) { - $arguments[$key] = $param['default']; + 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; - } else { - throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); } } } + 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])) { diff --git a/test/unit/EthTest.php b/test/unit/EthTest.php index 332fcea..320cfe7 100644 --- a/test/unit/EthTest.php +++ b/test/unit/EthTest.php @@ -301,7 +301,6 @@ class EthTest extends TestCase return $this->fail($err->getMessage()); } if (isset($transactionCount->result)) { - var_dump($transactionCount->result); $this->assertTrue(is_string($transactionCount->result)); } else { if (isset($transactionCount->error)) {