Address formatter.

This commit is contained in:
sc0Vu 2017-12-28 10:52:30 +08:00
parent e10020d2dc
commit 7ba116e5a0
3 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?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\Formatters;
use InvalidArgumentException;
use Web3\Utils;
use Web3\Formatters\IFormatter;
class Address implements IFormatter
{
/**
* format
* to do: iban
*
* @param mixed $value
* @return string
*/
public static function format($value)
{
if (Utils::isAddress($value)) {
$value = mb_strtolower($value);
if (Utils::isZeroPrefixed($value)) {
return $value;
}
return '0x' . $value;
}
throw new InvalidArgumentException('The address to inputFormat is invalid.');
}
}

View File

@ -0,0 +1,23 @@
<?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\Formatters;
interface IFormatter
{
/**
* format
*
* @param mixed $value
* @return string
*/
public static function format($value);
}

View File

@ -0,0 +1,49 @@
<?php
namespace Test\Unit;
use Test\TestCase;
use Web3\Formatters\Address;
class AddressFormatterTest extends TestCase
{
/**
* formatter
*
* @var \Web3\Formatters\Address
*/
protected $formatter;
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->formatter = new Address;
}
/**
* testFormat
*
* @return void
*/
public function testFormat()
{
$formatter = $this->formatter;
$address = $formatter->format('0Xca35b7d915458ef540ade6068dfe2f44e8fa733c');
$this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c');
$address = $formatter->format('0XCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
$this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c');
$address = $formatter->format('0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
$this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c');
}
}