This commit is contained in:
sc0Vu 2017-12-18 11:30:32 +08:00
parent 5957652db8
commit a512883519
3 changed files with 220 additions and 0 deletions

196
src/Net.php Normal file
View File

@ -0,0 +1,196 @@
<?php
/**
* This file is part of web3.php package.
*
* @author Peter Lai <alk03073135@gmail.com>
* @license MIT
*/
namespace Web3;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
class Net
{
/**
* provider
*
* @var \Web3\Providers\Provider
*/
protected $provider;
/**
* methods
*
* @var array
*/
private $methods = [
'net_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) {
$requestManeger = new HttpRequestManager($provider);
$this->provider = new HttpProvider($requestManeger);
}
} 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)) {
return;
}
$class = explode('\\', get_class());
if (strtolower($class[1]) === 'net' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name;
if (!array_key_exists($method, $this->methods)) {
throw new \RuntimeException('Unallowed rpc method: ' . $method);
}
$allowedMethod = $this->methods[$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 (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) {
// validate params
foreach ($allowedMethod['params'] as $key => $param) {
if (isset($param['validators'])) {
if (is_array($param['validators'])) {
$isError = true;
foreach ($param['validators'] as $rule) {
if (isset($arguments[$key])) {
if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) {
$isError = false;
break;
}
} else {
if (isset($param['default'])) {
$arguments[$key] = $param['default'];
$isError = false;
break;
}
}
}
if ($isError === true) {
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
}
} else {
if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) {
if (isset($param['default']) && !isset($arguments[$key])) {
$arguments[$key] = $param['default'];
} else {
throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.');
}
}
}
}
}
}
$this->provider->send($method, $arguments, $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 void
*/
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);
}
}

View File

@ -10,6 +10,7 @@
namespace Web3;
use Web3\Eth;
use Web3\Net;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
@ -32,6 +33,13 @@ class Web3
*/
protected $eth;
/**
* net
*
* @var \Web3\Net
*/
protected $net;
/**
* methods
*
@ -211,6 +219,20 @@ class Web3
return $this->eth;
}
/**
* getNet
*
* @return void
*/
public function getNet()
{
if (!isset($this->net)) {
$net = new Net($this->provider);
$this->net = $net;
}
return $this->net;
}
/**
* batch
*

View File

@ -6,6 +6,7 @@ use RuntimeException;
use Test\TestCase;
use Web3\Web3;
use Web3\Eth;
use Web3\Net;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
@ -49,6 +50,7 @@ class Web3Test extends TestCase
$this->assertTrue($web3->provider instanceof HttpProvider);
$this->assertTrue($web3->provider->requestManager instanceof RequestManager);
$this->assertTrue($web3->eth instanceof Eth);
$this->assertTrue($web3->net instanceof Net);
}
/**