From 0fa64aecd74c3687e8786b321fbe13c31ce0f368 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 26 Dec 2017 16:46:52 +0800 Subject: [PATCH] AddressType --- src/Contracts/SolidityType.php | 57 +++++++++++++++++++++++++++ src/Contracts/Types/Address.php | 49 +++++++++++++++++++++++ src/Contracts/Types/IType.php | 30 ++++++++++++++ test/unit/AddressTypeTest.php | 69 +++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 src/Contracts/SolidityType.php create mode 100644 src/Contracts/Types/Address.php create mode 100644 src/Contracts/Types/IType.php create mode 100644 test/unit/AddressTypeTest.php diff --git a/src/Contracts/SolidityType.php b/src/Contracts/SolidityType.php new file mode 100644 index 0000000..dffacc7 --- /dev/null +++ b/src/Contracts/SolidityType.php @@ -0,0 +1,57 @@ + + * + * @author Peter Lai + * @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; + } +} \ No newline at end of file diff --git a/src/Contracts/Types/Address.php b/src/Contracts/Types/Address.php new file mode 100644 index 0000000..a7112db --- /dev/null +++ b/src/Contracts/Types/Address.php @@ -0,0 +1,49 @@ + + * + * @author Peter Lai + * @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; + } +} \ No newline at end of file diff --git a/src/Contracts/Types/IType.php b/src/Contracts/Types/IType.php new file mode 100644 index 0000000..363e14d --- /dev/null +++ b/src/Contracts/Types/IType.php @@ -0,0 +1,30 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Contracts\Types; + +interface IType +{ + /** + * isType + * + * @param string $name + * @return bool + */ + public function isType($name); + + /** + * isDynamicType + * + * @return bool + */ + public function isDynamicType(); +} \ No newline at end of file diff --git a/test/unit/AddressTypeTest.php b/test/unit/AddressTypeTest.php new file mode 100644 index 0000000..40b3a2a --- /dev/null +++ b/test/unit/AddressTypeTest.php @@ -0,0 +1,69 @@ + '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']); + } + } +} \ No newline at end of file