-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathIntegerFormatter.php
More file actions
44 lines (38 loc) · 981 Bytes
/
IntegerFormatter.php
File metadata and controls
44 lines (38 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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(Utils::isNegative($value));
$padded = mb_substr($bnHex, 0, 1);
if ($padded !== 'f') {
$padded = '0';
}
return implode('', array_fill(0, $digit-mb_strlen($bnHex), $padded)) . $bnHex;
}
}