web3.php/src/Formatters/IntegerFormatter.php
Dan 6bd06644ae
set toHex() params
set toHex() params $twos_compliment to false when is not negative
2018-04-09 19:02:49 +08:00

45 lines
972 B
PHP

<?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 IntegerFormatter implements IFormatter
{
/**
* format
*
* @param mixed $value
* @return string
*/
public static function format($value)
{
$value = (string) $value;
$arguments = func_get_args();
$digit = 64;
if (isset($arguments[1]) && is_numeric($arguments[1])) {
$digit = intval($arguments[1]);
}
$bn = Utils::toBn($value);
$bnHex = $bn->toHex($bn->is_negative);
$padded = mb_substr($bnHex, 0, 1);
if ($padded !== 'f') {
$padded = '0';
}
return implode('', array_fill(0, $digit-mb_strlen($bnHex), $padded)) . $bnHex;
}
}