Web3\Utils

- toHex
- hexToBin
This commit is contained in:
sc0Vu 2017-12-20 11:49:52 +08:00
parent 303c14a842
commit ffb02edee7
4 changed files with 141 additions and 0 deletions

52
src/Utils.php Normal file
View File

@ -0,0 +1,52 @@
<?php
namespace Web3;
use InvalidArgumentException;
class Utils
{
/**
* construct
*
* @return void
*/
public function __construct()
{
//
}
/**
* toHex
*
* @param string $value
* @param bool $isPrefix
* @return string
*/
public static function toHex($value, $isPrefix=false)
{
if (!is_string($value)) {
throw new InvalidArgumentException('The value to toHex function must be string.');
}
if ($isPrefix) {
return '0x' . implode('', unpack('H*', $value));
}
return implode('', unpack('H*', $value));
}
/**
* hexToBin
*
* @param string
* @return string
*/
public static function hexToBin($value)
{
if (!is_string($value)) {
throw new InvalidArgumentException('The value to toHex function must be string.');
}
$hexString = str_replace('0x', '', $value);
return pack('H*', $hexString);
}
}

View File

@ -14,6 +14,7 @@ namespace Web3;
use Web3\Eth;
use Web3\Net;
use Web3\Personal;
use Web3\Utils;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
@ -50,6 +51,13 @@ class Web3
*/
protected $personal;
/**
* utils
*
* @var \Web3\Utils
*/
protected $utils;
/**
* methods
*
@ -257,6 +265,20 @@ class Web3
return $this->personal;
}
/**
* getUtils
*
* @return \Web3\Utils
*/
public function getUtils()
{
if (!isset($this->utils)) {
$utils = new Utils;
$this->utils = $utils;
}
return $this->utils;
}
/**
* batch
*

65
test/unit/UtilsTest.php Normal file
View File

@ -0,0 +1,65 @@
<?php
namespace Test\Unit;
use InvalidArgumentException;
use Test\TestCase;
use Web3\Utils;
class UtilsTest extends TestCase
{
/**
* testHex
* 'hello world'
* you can check by call pack('H*', $hex)
*
* @var string
*/
protected $testHex = '68656c6c6f20776f726c64';
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();
}
/**
* testToHex
*
* @return void
*/
public function testToHex()
{
$hex = Utils::toHex('hello world');
$this->assertEquals($hex, $this->testHex);
$hexPrefixed = Utils::toHex('hello world', true);
$this->assertEquals($hexPrefixed, '0x' . $this->testHex);
}
/**
* testHexToBin
*
* @return void
*/
public function testHexToBin()
{
$str = Utils::hexToBin($this->testHex);
$this->assertEquals($str, 'hello world');
$str = Utils::hexToBin('0x' . $this->testHex);
$this->assertEquals($str, 'hello world');
$str = Utils::hexToBin('0xe4b883e5bda9e7a59ee4bb99e9b1bc');
$this->assertEquals($str, '七彩神仙鱼');
}
}

View File

@ -8,6 +8,7 @@ use Web3\Web3;
use Web3\Eth;
use Web3\Net;
use Web3\Personal;
use Web3\Utils;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
@ -54,6 +55,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->utils instanceof Utils);
}
/**