IntegerFormatter

This commit is contained in:
sc0Vu 2017-12-28 17:36:09 +08:00
parent 0513bbe560
commit 6476c6066c
2 changed files with 83 additions and 0 deletions

View 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;
}
}

View 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');
}
}