From de192104926ca026867a4a7bea46907c32b426ad Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 22 Dec 2017 18:07:31 +0800 Subject: [PATCH] isZeroPrefixed --- src/Utils.php | 16 +++++++++++++++- test/unit/UtilsTest.php | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Utils.php b/src/Utils.php index 2cad45a..22e4157 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -69,6 +69,20 @@ class Utils return pack('H*', $value); } + /** + * isZeroPrefixed + * + * @param string + * @return bool + */ + public static function isZeroPrefixed($value) + { + if (!is_string($value)) { + throw new InvalidArgumentException('The value to zeroPrefixed function must be string.'); + } + return (strpos($value, '0x') === 0); + } + /** * sha3 * keccak256 @@ -90,5 +104,5 @@ class Utils return null; } return '0x' . $hash; - } + } } \ No newline at end of file diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 6b5c3b7..a95d1be 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -63,6 +63,22 @@ class UtilsTest extends TestCase $this->assertEquals($str, '七彩神仙鱼'); } + /** + * testIsZeroPrefixed + * + * @return void + */ + public function testIsZeroPrefixed() + { + $isPrefixed = Utils::isZeroPrefixed($this->testHex); + + $this->assertEquals($isPrefixed, false); + + $isPrefixed = Utils::isZeroPrefixed('0x' . $this->testHex); + + $this->assertEquals($isPrefixed, true); + } + /** * testSha3 *