diff --git a/src/Formatters/Integer.php b/src/Formatters/Integer.php new file mode 100644 index 0000000..fbf045f --- /dev/null +++ b/src/Formatters/Integer.php @@ -0,0 +1,34 @@ + + * + * @author Peter Lai + * @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; + } +} \ No newline at end of file diff --git a/test/unit/IntegerFormatterTest.php b/test/unit/IntegerFormatterTest.php new file mode 100644 index 0000000..5885f69 --- /dev/null +++ b/test/unit/IntegerFormatterTest.php @@ -0,0 +1,49 @@ +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'); + } +} \ No newline at end of file