From 9aacaffff85449bf4033a10fd2af550b982666db Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Mon, 5 Mar 2018 16:52:03 +0800 Subject: [PATCH] Return 0 when number is empty string. --- src/Utils.php | 7 +++++-- test/unit/UtilsTest.php | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index c9b0777..fbcf188 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -470,7 +470,6 @@ class Utils } else { $bn = new BigNumber($number); } - if (isset($negative1)) { $bn = $bn->multiply($negative1); } @@ -485,12 +484,16 @@ class Utils if (self::isZeroPrefixed($number) || preg_match('/[a-f]+/', $number) === 1) { $number = self::stripZero($number); $bn = new BigNumber($number, 16); + } elseif (empty($number)) { + $bn = new BigNumber(0); + } else { + throw new InvalidArgumentException('toBn number must be valid hex string.'); } if (isset($negative1)) { $bn = $bn->multiply($negative1); } } else { - throw new InvalidArgumentException('toBn number must be BigNumber, numeric string or int.'); + throw new InvalidArgumentException('toBn number must be BigNumber, string or int.'); } return $bn; } diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index ae94fae..a519ab4 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -469,6 +469,9 @@ class UtilsTest extends TestCase */ public function testToBn() { + $bn = Utils::toBn(''); + $this->assertEquals($bn->toString(), '0'); + $bn = Utils::toBn(11); $this->assertEquals($bn->toString(), '11');