JSONRPC method and web3 api methods
This commit is contained in:
parent
e6f7a58d3c
commit
48d00a2641
24
src/Methods/IMethod.php
Normal file
24
src/Methods/IMethod.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?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\Methods;
|
||||||
|
|
||||||
|
interface IMethod
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* transform
|
||||||
|
*
|
||||||
|
* @param array &$data
|
||||||
|
* @param array $rules
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function transform($data, $rules);
|
||||||
|
}
|
29
src/Methods/IRPC.php
Normal file
29
src/Methods/IRPC.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?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\Methods;
|
||||||
|
|
||||||
|
interface IRPC
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* __toString
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function __toString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* toPayload
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toPayload();
|
||||||
|
}
|
212
src/Methods/JSONRPC.php
Normal file
212
src/Methods/JSONRPC.php
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
<?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\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);
|
||||||
|
}
|
||||||
|
}
|
89
src/Methods/Web3/ClientVersion.php
Normal file
89
src/Methods/Web3/ClientVersion.php
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
<?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\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;
|
||||||
|
}
|
||||||
|
}
|
91
src/Methods/Web3/Sha3.php
Normal file
91
src/Methods/Web3/Sha3.php
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
<?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\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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user