Address formatter.
This commit is contained in:
parent
e10020d2dc
commit
7ba116e5a0
39
src/Formatters/Address.php
Normal file
39
src/Formatters/Address.php
Normal 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.');
|
||||
}
|
||||
}
|
23
src/Formatters/IFormatter.php
Normal file
23
src/Formatters/IFormatter.php
Normal 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);
|
||||
}
|
49
test/unit/AddressFormatterTest.php
Normal file
49
test/unit/AddressFormatterTest.php
Normal 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');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user