Personal.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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;
  11. use Web3\Providers\Provider;
  12. use Web3\Providers\HttpProvider;
  13. use Web3\RequestManagers\RequestManager;
  14. use Web3\RequestManagers\HttpRequestManager;
  15. class Personal
  16. {
  17. /**
  18. * provider
  19. *
  20. * @var \Web3\Providers\Provider
  21. */
  22. protected $provider;
  23. /**
  24. * methods
  25. *
  26. * @var array
  27. */
  28. private $methods = [];
  29. /**
  30. * allowedMethods
  31. *
  32. * @var array
  33. */
  34. private $allowedMethods = [
  35. 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_sendTransaction'
  36. ];
  37. /**
  38. * construct
  39. *
  40. * @param string|\Web3\Providers\Provider $provider
  41. * @return void
  42. */
  43. public function __construct($provider)
  44. {
  45. if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
  46. // check the uri schema
  47. if (preg_match('/^https?:\/\//', $provider) === 1) {
  48. $requestManager = new HttpRequestManager($provider);
  49. $this->provider = new HttpProvider($requestManager);
  50. }
  51. } else if ($provider instanceof Provider) {
  52. $this->provider = $provider;
  53. }
  54. }
  55. /**
  56. * call
  57. *
  58. * @param string $name
  59. * @param array $arguments
  60. * @return void
  61. */
  62. public function __call($name, $arguments)
  63. {
  64. if (empty($this->provider)) {
  65. throw new \RuntimeException('Please set provider first.');
  66. }
  67. $class = explode('\\', get_class());
  68. if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
  69. $method = strtolower($class[1]) . '_' . $name;
  70. if (!in_array($method, $this->allowedMethods)) {
  71. throw new \RuntimeException('Unallowed rpc method: ' . $method);
  72. }
  73. if ($this->provider->isBatch) {
  74. $callback = null;
  75. } else {
  76. $callback = array_pop($arguments);
  77. if (is_callable($callback) !== true) {
  78. throw new \InvalidArgumentException('The last param must be callback function.');
  79. }
  80. }
  81. if (!array_key_exists($method, $this->methods)) {
  82. // new the method
  83. $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
  84. $methodObject = new $methodClass($method, $arguments);
  85. $this->methods[$method] = $methodObject;
  86. } else {
  87. $methodObject = $this->methods[$method];
  88. }
  89. if ($methodObject->validate($arguments)) {
  90. $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
  91. $methodObject->arguments = $inputs;
  92. $this->provider->send($methodObject, $callback);
  93. }
  94. }
  95. }
  96. /**
  97. * get
  98. *
  99. * @param string $name
  100. * @return mixed
  101. */
  102. public function __get($name)
  103. {
  104. $method = 'get' . ucfirst($name);
  105. if (method_exists($this, $method)) {
  106. return call_user_func_array([$this, $method], []);
  107. }
  108. return false;
  109. }
  110. /**
  111. * set
  112. *
  113. * @param string $name
  114. * @param mixed $value
  115. * @return mixed
  116. */
  117. public function __set($name, $value)
  118. {
  119. $method = 'set' . ucfirst($name);
  120. if (method_exists($this, $method)) {
  121. return call_user_func_array([$this, $method], [$value]);
  122. }
  123. return false;
  124. }
  125. /**
  126. * getProvider
  127. *
  128. * @return \Web3\Providers\Provider
  129. */
  130. public function getProvider()
  131. {
  132. return $this->provider;
  133. }
  134. /**
  135. * setProvider
  136. *
  137. * @param \Web3\Providers\Provider $provider
  138. * @return bool
  139. */
  140. public function setProvider($provider)
  141. {
  142. if ($provider instanceof Provider) {
  143. $this->provider = $provider;
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * batch
  150. *
  151. * @param bool $status
  152. * @return void
  153. */
  154. public function batch($status)
  155. {
  156. $status = is_bool($status);
  157. $this->provider->batch($status);
  158. }
  159. }