IntegerFormatter
This commit is contained in:
parent
0513bbe560
commit
6476c6066c
34
src/Formatters/Integer.php
Normal file
34
src/Formatters/Integer.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?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 Integer implements IFormatter
|
||||
{
|
||||
/**
|
||||
* format
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
public static function format($value)
|
||||
{
|
||||
$bn = Utils::toBn($value);
|
||||
$bnHex = $bn->toHex(true);
|
||||
$padded = mb_substr($bnHex, 0, 1);
|
||||
|
||||
return implode('', array_fill(0, 64-mb_strlen($bnHex), $padded)) . $bnHex;
|
||||
}
|
||||
}
|
49
test/unit/IntegerFormatterTest.php
Normal file
49
test/unit/IntegerFormatterTest.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Test\Unit;
|
||||
|
||||
use Test\TestCase;
|
||||
use Web3\Formatters\Integer;
|
||||
|
||||
class IntegerFormatterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* formatter
|
||||
*
|
||||
* @var \Web3\Formatters\Integer
|
||||
*/
|
||||
protected $formatter;
|
||||
|
||||
/**
|
||||
* setUp
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->formatter = new Integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* testFormat
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFormat()
|
||||
{
|
||||
$formatter = $this->formatter;
|
||||
|
||||
$hex = $formatter->format('1');
|
||||
|
||||
$this->assertEquals($hex, implode('', array_fill(0, 63, '0')) . '1');
|
||||
|
||||
$hex = $formatter->format('-1');
|
||||
|
||||
$this->assertEquals($hex, implode('', array_fill(0, 64, 'f')));
|
||||
|
||||
$hex = $formatter->format('ae');
|
||||
|
||||
$this->assertEquals($hex, implode('', array_fill(0, 62, '0')) . 'ae');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user