JSONRPC.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * This file is part of web3.php package.
  4. *
  5. * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
  6. *
  7. * @author Peter Lai <alk03073135@gmail.com>
  8. * @license MIT
  9. */
  10. namespace Web3\Methods;
  11. use InvalidArgumentException;
  12. use Web3\Methods\IRPC;
  13. class JSONRPC implements IRPC
  14. {
  15. /**
  16. * id
  17. *
  18. * @var int
  19. */
  20. protected $id = 0;
  21. /**
  22. * rpcVersion
  23. *
  24. * @var string
  25. */
  26. protected $rpcVersion = '2.0';
  27. /**
  28. * method
  29. *
  30. * @var string
  31. */
  32. protected $method = '';
  33. /**
  34. * arguments
  35. *
  36. * @var array
  37. */
  38. protected $arguments = [];
  39. /**
  40. * construct
  41. *
  42. * @param string $method
  43. * @param array $arguments
  44. * @return void
  45. */
  46. public function __construct($method, $arguments)
  47. {
  48. $this->method = $method;
  49. $this->arguments = $arguments;
  50. }
  51. /**
  52. * get
  53. *
  54. * @param string $name
  55. * @return mixed
  56. */
  57. public function __get($name)
  58. {
  59. $method = 'get' . ucfirst($name);
  60. if (method_exists($this, $method)) {
  61. return call_user_func_array([$this, $method], []);
  62. }
  63. return false;
  64. }
  65. /**
  66. * set
  67. *
  68. * @param string $name
  69. * @param mixed $value
  70. * @return mixed
  71. */
  72. public function __set($name, $value)
  73. {
  74. $method = 'set' . ucfirst($name);
  75. if (method_exists($this, $method)) {
  76. return call_user_func_array([$this, $method], [$value]);
  77. }
  78. return false;
  79. }
  80. /**
  81. * __toString
  82. *
  83. * @return string
  84. */
  85. public function __toString()
  86. {
  87. $payload = $this->toPayload();
  88. return json_encode($payload);
  89. }
  90. /**
  91. * setId
  92. *
  93. * @param int $id
  94. * @return bool
  95. */
  96. public function setId($id)
  97. {
  98. if (!is_int($id)) {
  99. throw new InvalidArgumentException('Id must be integer.');
  100. }
  101. $this->id = $id;
  102. return true;
  103. }
  104. /**
  105. * getId
  106. *
  107. * @return int
  108. */
  109. public function getId()
  110. {
  111. return $this->id;
  112. }
  113. /**
  114. * getRpcVersion
  115. *
  116. * @return string
  117. */
  118. public function getRpcVersion()
  119. {
  120. return $this->rpcVersion;
  121. }
  122. /**
  123. * getMethod
  124. *
  125. * @return string
  126. */
  127. public function getMethod()
  128. {
  129. return $this->method;
  130. }
  131. /**
  132. * setArguments
  133. *
  134. * @param array $arguments
  135. * @return bool
  136. */
  137. public function setArguments($arguments)
  138. {
  139. if (!is_array($arguments)) {
  140. throw new InvalidArgumentException('Please use array when call setArguments.');
  141. }
  142. $this->arguments = $arguments;
  143. return true;
  144. }
  145. /**
  146. * getArguments
  147. *
  148. * @return array
  149. */
  150. public function getArguments()
  151. {
  152. return $this->arguments;
  153. }
  154. /**
  155. * toPayload
  156. *
  157. * @return array
  158. */
  159. public function toPayload()
  160. {
  161. if (empty($this->method) || !is_string($this->method)) {
  162. throw new InvalidArgumentException('Please check the method set properly.');
  163. }
  164. if (empty($this->id)) {
  165. $id = rand();
  166. } else {
  167. $id = $this->id;
  168. }
  169. $rpc = [
  170. 'id' => $id,
  171. 'jsonrpc' => $this->rpcVersion,
  172. 'method' => $this->method
  173. ];
  174. if (count($this->arguments) > 0) {
  175. $rpc['params'] = $this->arguments;
  176. }
  177. return $rpc;
  178. }
  179. /**
  180. * toPayloadString
  181. *
  182. * @return string
  183. */
  184. public function toPayloadString()
  185. {
  186. $payload = $this->toPayload();
  187. return json_encode($payload);
  188. }
  189. }