AddressType

This commit is contained in:
sc0Vu 2017-12-26 16:46:52 +08:00
parent b24139113d
commit 0fa64aecd7
4 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?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\Contracts;
class SolidityType
{
/**
* construct
*
* @return void
*/
public function __construct()
{
//
}
/**
* 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;
}
}

View File

@ -0,0 +1,49 @@
<?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\Contracts\Types;
use Web3\Contracts\SolidityType;
use Web3\Contracts\Types\IType;
class Address extends SolidityType implements IType
{
/**
* construct
*
* @return void
*/
public function __construct()
{
//
}
/**
* isType
*
* @param string $name
* @return bool
*/
public function isType($name)
{
return (preg_match('/address(\[([0-9]+)\])?/', $name) === 1);
}
/**
* isDynamicType
*
* @return bool
*/
public function isDynamicType()
{
return false;
}
}

View File

@ -0,0 +1,30 @@
<?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\Contracts\Types;
interface IType
{
/**
* isType
*
* @param string $name
* @return bool
*/
public function isType($name);
/**
* isDynamicType
*
* @return bool
*/
public function isDynamicType();
}

View File

@ -0,0 +1,69 @@
<?php
namespace Test\Unit;
use InvalidArgumentException;
use Test\TestCase;
use Web3\Contracts\Types\Address;
class AddressTypeTest extends TestCase
{
/**
* testTypes
*
* @var array
*/
protected $testTypes = [
[
'value' => 'address',
'result' => true
], [
'value' => 'address[]',
'result' => true
], [
'value' => 'address[4]',
'result' => true
], [
'value' => 'address[][]',
'result' => true
], [
'value' => 'address[3][]',
'result' => true
], [
'value' => 'address[][6][]',
'result' => true
],
];
/**
* solidityType
*
* @var \Web3\Contracts\SolidityType
*/
protected $solidityType;
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->solidityType = new Address;
}
/**
* testIsType
*
* @return void
*/
public function testIsType()
{
$solidityType = $this->solidityType;
foreach ($this->testTypes as $type) {
$this->assertEquals($solidityType->isType($type['value']), $type['result']);
}
}
}