| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?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], []);
- }
- return false;
- }
- /**
- * set
- *
- * @param string $name
- * @param mixed $value
- * @return mixed
- */
- 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 string
- */
- 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 bool
- */
- public function setArguments($arguments)
- {
- if (!is_array($arguments)) {
- throw new InvalidArgumentException('Please use array when call setArguments.');
- }
- $this->arguments = $arguments;
- return true;
- }
- /**
- * 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);
- }
- }
|