diff --git a/src/Shh.php b/src/Shh.php new file mode 100644 index 0000000..7e6c1f2 --- /dev/null +++ b/src/Shh.php @@ -0,0 +1,179 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3; + +use Web3\Providers\Provider; +use Web3\Providers\HttpProvider; +use Web3\RequestManagers\RequestManager; +use Web3\RequestManagers\HttpRequestManager; + +class Shh +{ + /** + * provider + * + * @var \Web3\Providers\Provider + */ + protected $provider; + + /** + * methods + * + * @var array + */ + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'shh_version' + ]; + + /** + * construct + * + * @param mixed string | Web3\Providers\Provider $provider + * @return void + */ + public function __construct($provider) + { + if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) { + // check the uri schema + if (preg_match('/^https?:\/\//', $provider) === 1) { + $requestManager = new HttpRequestManager($provider); + + $this->provider = new HttpProvider($requestManager); + } + } else if ($provider instanceof Provider) { + $this->provider = $provider; + } + } + + /** + * call + * + * @param string $name + * @param array $arguments + * @return void + */ + public function __call($name, $arguments) + { + if (empty($this->provider)) { + throw new \RuntimeException('Please set provider first.'); + } + + $class = explode('\\', get_class()); + + if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { + $method = strtolower($class[1]) . '_' . $name; + + if (!in_array($method, $this->allowedMethods)) { + throw new \RuntimeException('Unallowed rpc method: ' . $method); + } + if ($this->provider->isBatch) { + $callback = null; + } else { + $callback = array_pop($arguments); + + if (is_callable($callback) !== true) { + throw new \InvalidArgumentException('The last param must be callback function.'); + } + } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; + } + if ($methodObject->validate($arguments)) { + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + $this->provider->send($methodObject, $callback); + } + } + } + + /** + * 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; + } + + /** + * getProvider + * + * @return \Web3\Providers\Provider + */ + public function getProvider() + { + return $this->provider; + } + + /** + * setProvider + * + * @param $provider + * @return bool + */ + public function setProvider($provider) + { + if ($provider instanceof Provider) { + $this->provider = $provider; + return true; + } + return false; + } + + /** + * batch + * + * @param bool $status + * @return void + */ + public function batch($status) + { + $status = is_bool($status); + + $this->provider->batch($status); + } +} \ No newline at end of file diff --git a/src/Web3.php b/src/Web3.php index aa3fafb..ebf7572 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -14,6 +14,7 @@ namespace Web3; use Web3\Eth; use Web3\Net; use Web3\Personal; +use Web3\Shh; use Web3\Utils; use Web3\Providers\Provider; use Web3\Providers\HttpProvider; @@ -50,6 +51,13 @@ class Web3 */ protected $personal; + /** + * shh + * + * @var \Web3\Shh + */ + protected $shh; + /** * utils * @@ -238,6 +246,20 @@ class Web3 return $this->personal; } + /** + * getShh + * + * @return \Web3\Shh + */ + public function getShh() + { + if (!isset($this->shh)) { + $shh = new Shh($this->provider); + $this->shh = $shh; + } + return $this->shh; + } + /** * getUtils * diff --git a/test/unit/Web3Test.php b/test/unit/Web3Test.php index 3f33d67..aee0a4c 100644 --- a/test/unit/Web3Test.php +++ b/test/unit/Web3Test.php @@ -8,6 +8,7 @@ use Web3\Web3; use Web3\Eth; use Web3\Net; use Web3\Personal; +use Web3\Shh; use Web3\Utils; use Web3\Providers\HttpProvider; use Web3\RequestManagers\RequestManager; @@ -55,6 +56,7 @@ class Web3Test extends TestCase $this->assertTrue($web3->eth instanceof Eth); $this->assertTrue($web3->net instanceof Net); $this->assertTrue($web3->personal instanceof Personal); + $this->assertTrue($web3->shh instanceof Shh); $this->assertTrue($web3->utils instanceof Utils); }