Add validator and update validators
Change: * Add validator and verify message * Update validators
This commit is contained in:
parent
b6fc44d9e3
commit
dcb0721077
@ -118,11 +118,13 @@ class EthMethod extends JSONRPC implements IMethod
|
||||
}
|
||||
}
|
||||
if ($isError) {
|
||||
throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.');
|
||||
$verifyMessage = call_user_func([$subRule, 'getVerifyMessage']);
|
||||
throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . ', message: ' . $verifyMessage . '.');
|
||||
}
|
||||
} else {
|
||||
if (call_user_func([$rule, 'validate'], $params[$key]) === false) {
|
||||
throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . '.');
|
||||
$verifyMessage = call_user_func([$rule, 'getVerifyMessage']);
|
||||
throw new RuntimeException('Wrong type of ' . $this->method . ' method argument ' . $key . ', message: ' . $verifyMessage . '.');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class AddressValidator
|
||||
class AddressValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class BlockHashValidator
|
||||
class BlockHashValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class BooleanValidator
|
||||
class BooleanValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,11 +12,12 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Validators\TagValidator;
|
||||
use Web3\Validators\HexValidator;
|
||||
|
||||
class CallValidator
|
||||
class CallValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
@ -27,30 +28,39 @@ class CallValidator
|
||||
public static function validate($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
self::$verifyMessage = 'call must be array';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['from']) && AddressValidator::validate($value['from']) === false) {
|
||||
self::$verifyMessage = 'from is not valid';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['to'])) {
|
||||
self::$verifyMessage = 'to is required';
|
||||
return false;
|
||||
}
|
||||
if (AddressValidator::validate($value['to']) === false) {
|
||||
self::$verifyMessage = 'to is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) {
|
||||
self::$verifyMessage = 'gas is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['gasPrice']) && QuantityValidator::validate($value['gasPrice']) === false) {
|
||||
self::$verifyMessage = 'gasPrice is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) {
|
||||
self::$verifyMessage = 'value is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['data']) && HexValidator::validate($value['data']) === false) {
|
||||
self::$verifyMessage = 'data is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) {
|
||||
self::$verifyMessage = 'nonce is not valid';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -12,12 +12,13 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Validators\TagValidator;
|
||||
use Web3\Validators\HexValidator;
|
||||
use Web3\Validators\AddressValidator;
|
||||
|
||||
class FilterValidator
|
||||
class FilterValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
@ -28,6 +29,7 @@ class FilterValidator
|
||||
public static function validate($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
self::$verifyMessage = 'filter must be array';
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
@ -35,6 +37,7 @@ class FilterValidator
|
||||
QuantityValidator::validate($value['fromBlock']) === false &&
|
||||
TagValidator::validate($value['fromBlock']) === false
|
||||
) {
|
||||
self::$verifyMessage = 'fromBlock is not valid';
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
@ -42,16 +45,19 @@ class FilterValidator
|
||||
QuantityValidator::validate($value['toBlock']) === false &&
|
||||
TagValidator::validate($value['toBlock']) === false
|
||||
) {
|
||||
self::$verifyMessage = 'toBlock is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['address'])) {
|
||||
if (is_array($value['address'])) {
|
||||
foreach ($value['address'] as $address) {
|
||||
if (AddressValidator::validate($address) === false) {
|
||||
self::$verifyMessage = 'address is not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} elseif (AddressValidator::validate($value['address']) === false) {
|
||||
self::$verifyMessage = 'address is not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -60,11 +66,13 @@ class FilterValidator
|
||||
if (is_array($topic)) {
|
||||
foreach ($topic as $v) {
|
||||
if (isset($v) && HexValidator::validate($v) === false) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isset($topic) && HexValidator::validate($topic) === false) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class HexValidator
|
||||
class HexValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class IdentityValidator
|
||||
class IdentityValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class NonceValidator
|
||||
class NonceValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,11 +12,12 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Validators\HexValidator;
|
||||
use Web3\Validators\IdentityValidator;
|
||||
|
||||
class PostValidator
|
||||
class PostValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
@ -27,38 +28,49 @@ class PostValidator
|
||||
public static function validate($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
self::$verifyMessage = 'post must be array';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['from']) && IdentityValidator::validate($value['from']) === false) {
|
||||
self::$verifyMessage = 'from is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['to']) && IdentityValidator::validate($value['to']) === false) {
|
||||
self::$verifyMessage = 'to is not valid';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['topics']) || !is_array($value['topics'])) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
foreach ($value['topics'] as $topic) {
|
||||
if (IdentityValidator::validate($topic) === false) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!isset($value['payload'])) {
|
||||
self::$verifyMessage = 'payload is required';
|
||||
return false;
|
||||
}
|
||||
if (HexValidator::validate($value['payload']) === false) {
|
||||
self::$verifyMessage = 'payload is not valid';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['priority'])) {
|
||||
self::$verifyMessage = 'priority is required';
|
||||
return false;
|
||||
}
|
||||
if (QuantityValidator::validate($value['priority']) === false) {
|
||||
self::$verifyMessage = 'priority is not valid';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['ttl'])) {
|
||||
self::$verifyMessage = 'ttl is required';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['ttl']) && QuantityValidator::validate($value['ttl']) === false) {
|
||||
self::$verifyMessage = 'ttl is not valid';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class QuantityValidator
|
||||
class QuantityValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,11 +12,12 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Validators\HexValidator;
|
||||
use Web3\Validators\IdentityValidator;
|
||||
|
||||
class ShhFilterValidator
|
||||
class ShhFilterValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
@ -27,18 +28,26 @@ class ShhFilterValidator
|
||||
public static function validate($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
self::$verifyMessage = 'filter of shh must be array';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['to']) && IdentityValidator::validate($value['to']) === false) {
|
||||
self::$verifyMessage = 'to is not valid';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['topics']) || !is_array($value['topics'])) {
|
||||
if (!isset($value['topics'])) {
|
||||
self::$verifyMessage = 'topics are required';
|
||||
return false;
|
||||
}
|
||||
if (!is_array($value['topics'])) {
|
||||
self::$verifyMessage = 'topics must be array';
|
||||
return false;
|
||||
}
|
||||
foreach ($value['topics'] as $topic) {
|
||||
if (is_array($topic)) {
|
||||
foreach ($topic as $subTopic) {
|
||||
if (HexValidator::validate($subTopic) === false) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -46,6 +55,7 @@ class ShhFilterValidator
|
||||
}
|
||||
if (HexValidator::validate($topic) === false) {
|
||||
if (!is_null($topic)) {
|
||||
self::$verifyMessage = 'topics are not valid';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,9 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
|
||||
class StringValidator
|
||||
class StringValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,9 +12,10 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Utils;
|
||||
|
||||
class TagValidator implements IValidator
|
||||
class TagValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
|
@ -12,11 +12,12 @@
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
use Web3\Validators\Validator;
|
||||
use Web3\Validators\QuantityValidator;
|
||||
use Web3\Validators\TagValidator;
|
||||
use Web3\Validators\HexValidator;
|
||||
|
||||
class TransactionValidator
|
||||
class TransactionValidator extends Validator implements IValidator
|
||||
{
|
||||
/**
|
||||
* validate
|
||||
@ -29,36 +30,39 @@ class TransactionValidator
|
||||
public static function validate($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
self::$verifyMessage = 'transaction must be array';
|
||||
return false;
|
||||
}
|
||||
if (!isset($value['from'])) {
|
||||
self::$verifyMessage = 'from is required';
|
||||
return false;
|
||||
}
|
||||
if (AddressValidator::validate($value['from']) === false) {
|
||||
self::$verifyMessage = 'from must be address';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['to']) && AddressValidator::validate($value['to']) === false && $value['to'] !== '') {
|
||||
self::$verifyMessage = 'to must be address';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) {
|
||||
self::$verifyMessage = 'gas is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['gasPrice']) && QuantityValidator::validate($value['gasPrice']) === false) {
|
||||
self::$verifyMessage = 'gasPrice is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) {
|
||||
self::$verifyMessage = 'value is not valid';
|
||||
return false;
|
||||
}
|
||||
// if (!isset($value['data'])) {
|
||||
// return false;
|
||||
// }
|
||||
// if (HexValidator::validate($value['data']) === false) {
|
||||
// return false;
|
||||
// }
|
||||
if (isset($value['data']) && HexValidator::validate($value['data']) === false) {
|
||||
self::$verifyMessage = 'data is not valid';
|
||||
return false;
|
||||
}
|
||||
if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) {
|
||||
self::$verifyMessage = 'nonce is not valid';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
68
src/Validators/Validator.php
Normal file
68
src/Validators/Validator.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of web3.php package.
|
||||
*
|
||||
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
|
||||
*
|
||||
* @author Peter Lai <alk03073135@gmail.com>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Web3\Validators;
|
||||
|
||||
use Web3\Validators\IValidator;
|
||||
|
||||
class Validator
|
||||
{
|
||||
/**
|
||||
* verifyMessage
|
||||
* verify result from calling validate function
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $verifyMessage;
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
$method = 'get' . ucfirst($name);
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
return call_user_func_array([$this, $method], []);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* set
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
return call_user_func_array([$this, $method], [$value]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* getVerifyMessage
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getVerifyMessage()
|
||||
{
|
||||
return self::$verifyMessage;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user