Add negative string support.
This commit is contained in:
parent
4df36f155b
commit
5a80fb4245
@ -62,6 +62,14 @@ class Utils
|
|||||||
'tether' => '1000000000000000000000000000000'
|
'tether' => '1000000000000000000000000000000'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NEGATIVE1
|
||||||
|
* Cannot work, see: http://php.net/manual/en/language.constants.syntax.php
|
||||||
|
*
|
||||||
|
* @const
|
||||||
|
*/
|
||||||
|
// const NEGATIVE1 = new BigNumber(-1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct
|
* construct
|
||||||
*
|
*
|
||||||
@ -126,7 +134,7 @@ class Utils
|
|||||||
public static function isZeroPrefixed($value)
|
public static function isZeroPrefixed($value)
|
||||||
{
|
{
|
||||||
if (!is_string($value)) {
|
if (!is_string($value)) {
|
||||||
throw new InvalidArgumentException('The value to zeroPrefixed function must be string.');
|
throw new InvalidArgumentException('The value to isZeroPrefixed function must be string.');
|
||||||
}
|
}
|
||||||
return (strpos($value, '0x') === 0);
|
return (strpos($value, '0x') === 0);
|
||||||
}
|
}
|
||||||
@ -146,6 +154,20 @@ class Utils
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isNegative
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isNegative($value)
|
||||||
|
{
|
||||||
|
if (!is_string($value)) {
|
||||||
|
throw new InvalidArgumentException('The value to isNegative function must be string.');
|
||||||
|
}
|
||||||
|
return (strpos($value, '-') === 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* isAddress
|
* isAddress
|
||||||
*
|
*
|
||||||
@ -415,25 +437,44 @@ class Utils
|
|||||||
* Change number or number string to bignumber.
|
* Change number or number string to bignumber.
|
||||||
*
|
*
|
||||||
* @param BigNumber|string|int $number
|
* @param BigNumber|string|int $number
|
||||||
* @return \phpseclib\Math\BigInteger
|
* @return array|\phpseclib\Math\BigInteger
|
||||||
*/
|
*/
|
||||||
public static function toBn($number)
|
public static function toBn($number)
|
||||||
{
|
{
|
||||||
if (is_numeric($number)) {
|
if ($number instanceof BigNumber){
|
||||||
|
$bn = $number;
|
||||||
|
} elseif (is_int($number)) {
|
||||||
$bn = new BigNumber($number);
|
$bn = new BigNumber($number);
|
||||||
|
} elseif (is_numeric($number)) {
|
||||||
|
$number = (string) $number;
|
||||||
|
|
||||||
|
if (self::isNegative($number)) {
|
||||||
|
$count = 1;
|
||||||
|
$number = str_replace('-', '', $number, $count);
|
||||||
|
$negative1 = new BigNumber(-1);
|
||||||
|
}
|
||||||
|
$bn = new BigNumber($number);
|
||||||
|
|
||||||
|
if (isset($negative1)) {
|
||||||
|
$bn = $bn->multiply($negative1);
|
||||||
|
}
|
||||||
} elseif (is_string($number)) {
|
} elseif (is_string($number)) {
|
||||||
$number = mb_strtolower($number);
|
$number = mb_strtolower($number);
|
||||||
|
|
||||||
|
if (self::isNegative($number)) {
|
||||||
|
$count = 1;
|
||||||
|
$number = str_replace('-', '', $number, $count);
|
||||||
|
$negative1 = new BigNumber(-1);
|
||||||
|
}
|
||||||
if (self::isZeroPrefixed($number) || preg_match('/[a-f]+/', $number) === 1) {
|
if (self::isZeroPrefixed($number) || preg_match('/[a-f]+/', $number) === 1) {
|
||||||
$number = self::stripZero($number);
|
$number = self::stripZero($number);
|
||||||
$bn = new BigNumber($number, 16);
|
$bn = new BigNumber($number, 16);
|
||||||
} else {
|
|
||||||
$bn = new BigNumber($number);
|
|
||||||
}
|
}
|
||||||
} elseif ($number instanceof BigNumber){
|
if (isset($negative1)) {
|
||||||
$bn = $number;
|
$bn = $bn->multiply($negative1);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('toBn number must be BigNumber, string or int.');
|
throw new InvalidArgumentException('toBn number must be BigNumber, numeric string or int.');
|
||||||
}
|
}
|
||||||
return $bn;
|
return $bn;
|
||||||
}
|
}
|
||||||
|
@ -448,6 +448,20 @@ class UtilsTest extends TestCase
|
|||||||
$this->assertFalse($isHex);
|
$this->assertFalse($isHex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testIsNegative
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testIsNegative()
|
||||||
|
{
|
||||||
|
$isNegative = Utils::isNegative('-1');
|
||||||
|
$this->assertTrue($isNegative);
|
||||||
|
|
||||||
|
$isNegative = Utils::isNegative('1');
|
||||||
|
$this->assertFalse($isNegative);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testToBn
|
* testToBn
|
||||||
*
|
*
|
||||||
@ -461,12 +475,27 @@ class UtilsTest extends TestCase
|
|||||||
$bn = Utils::toBn('0x12');
|
$bn = Utils::toBn('0x12');
|
||||||
$this->assertEquals($bn->toString(), '18');
|
$this->assertEquals($bn->toString(), '18');
|
||||||
|
|
||||||
|
$bn = Utils::toBn('-0x12');
|
||||||
|
$this->assertEquals($bn->toString(), '-18');
|
||||||
|
|
||||||
$bn = Utils::toBn(0x12);
|
$bn = Utils::toBn(0x12);
|
||||||
$this->assertEquals($bn->toString(), '18');
|
$this->assertEquals($bn->toString(), '18');
|
||||||
|
|
||||||
$bn = Utils::toBn('ae');
|
$bn = Utils::toBn('ae');
|
||||||
$this->assertEquals($bn->toString(), '174');
|
$this->assertEquals($bn->toString(), '174');
|
||||||
|
|
||||||
|
$bn = Utils::toBn('-ae');
|
||||||
|
$this->assertEquals($bn->toString(), '-174');
|
||||||
|
|
||||||
|
$bn = Utils::toBn('-1');
|
||||||
|
$this->assertEquals($bn->toString(), '-1');
|
||||||
|
|
||||||
|
// $bn = Utils::toBn('-0.1');
|
||||||
|
// $this->assertEquals($bn->toString(), '-0.1');
|
||||||
|
|
||||||
|
// $bn = Utils::toBn(-0.1);
|
||||||
|
// $this->assertEquals($bn->toString(), -0.1);
|
||||||
|
|
||||||
$bn = Utils::toBn(new BigNumber(1));
|
$bn = Utils::toBn(new BigNumber(1));
|
||||||
$this->assertEquals($bn->toString(), '1');
|
$this->assertEquals($bn->toString(), '1');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user