diff --git a/src/Methods/IMethod.php b/src/Methods/IMethod.php new file mode 100644 index 0000000..5c4fd45 --- /dev/null +++ b/src/Methods/IMethod.php @@ -0,0 +1,24 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +interface IMethod +{ + /** + * transform + * + * @param array &$data + * @param array $rules + * @return array + */ + public function transform($data, $rules); +} \ No newline at end of file diff --git a/src/Methods/IRPC.php b/src/Methods/IRPC.php new file mode 100644 index 0000000..e913685 --- /dev/null +++ b/src/Methods/IRPC.php @@ -0,0 +1,29 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +interface IRPC +{ + /** + * __toString + * + * @return array + */ + public function __toString(); + + /** + * toPayload + * + * @return array + */ + public function toPayload(); +} \ No newline at end of file diff --git a/src/Methods/JSONRPC.php b/src/Methods/JSONRPC.php new file mode 100644 index 0000000..3822716 --- /dev/null +++ b/src/Methods/JSONRPC.php @@ -0,0 +1,212 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods; + +use InvalidArgumentException; +use Web3\Methods\IRPC; + +class JSONRPC implements IRPC +{ + /** + * id + * + * @var int + */ + protected $id = 0; + + /** + * rpcVersion + * + * @var string + */ + protected $rpcVersion = '2.0'; + + /** + * method + * + * @var string + */ + protected $method = ''; + + /** + * arguments + * + * @var array + */ + protected $arguments = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + public function __construct($method, $arguments) + { + $this->method = $method; + $this->arguments = $arguments; + } + + /** + * 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], []); + } + } + + /** + * 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; + } + + /** + * __toString + * + * @return array + */ + public function __toString() + { + $payload = $this->toPayload(); + + return json_encode($payload); + } + + /** + * setId + * + * @param int $id + * @return bool + */ + public function setId($id) + { + if (!is_int($id)) { + throw new InvalidArgumentException('Id must be integer.'); + } + $this->id = $id; + + return true; + } + + /** + * getId + * + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * getRpcVersion + * + * @return string + */ + public function getRpcVersion() + { + return $this->rpcVersion; + } + + /** + * getMethod + * + * @return string + */ + public function getMethod() + { + return $this->method; + } + + /** + * setArguments + * + * @param array $arguments + * @return array + */ + public function setArguments($arguments) + { + if (!is_array($arguments)) { + throw new InvalidArgumentException('Please use array when call setArguments.'); + } + $this->arguments = $arguments; + } + + /** + * getArguments + * + * @return array + */ + public function getArguments() + { + return $this->arguments; + } + + /** + * toPayload + * + * @return array + */ + public function toPayload() + { + if (empty($this->method) || !is_string($this->method)) { + throw new InvalidArgumentException('Please check the method set properly.'); + } + if (empty($this->id)) { + $id = rand(); + } else { + $id = $this->id; + } + $rpc = [ + 'id' => $id, + 'jsonrpc' => $this->rpcVersion, + 'method' => $this->method + ]; + + if (count($this->arguments) > 0) { + $rpc['params'] = $this->arguments; + } + return $rpc; + } + + /** + * toPayloadString + * + * @return string + */ + public function toPayloadString() + { + $payload = $this->toPayload(); + + return json_encode($payload); + } +} \ No newline at end of file diff --git a/src/Methods/Web3/ClientVersion.php b/src/Methods/Web3/ClientVersion.php new file mode 100644 index 0000000..d044265 --- /dev/null +++ b/src/Methods/Web3/ClientVersion.php @@ -0,0 +1,89 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Web3; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class ClientVersion extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Web3/Sha3.php b/src/Methods/Web3/Sha3.php new file mode 100644 index 0000000..a1d2fa0 --- /dev/null +++ b/src/Methods/Web3/Sha3.php @@ -0,0 +1,91 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Web3; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\HexFormatter; + +class Sha3 extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + HexFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file