From 46189b795a26e392d769113e17b705c28123ff0c Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 17:24:42 +0800 Subject: [PATCH] Optional quantity formatter. --- src/Formatters/OptionalQuantityFormatter.php | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Formatters/OptionalQuantityFormatter.php diff --git a/src/Formatters/OptionalQuantityFormatter.php b/src/Formatters/OptionalQuantityFormatter.php new file mode 100644 index 0000000..c191755 --- /dev/null +++ b/src/Formatters/OptionalQuantityFormatter.php @@ -0,0 +1,37 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; +use Web3\Validators\TagValidator; + +class OptionalQuantityFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + if (TagValidator::validate($value)) { + return $value; + } + $value = Utils::toString($value); + $bn = Utils::toBn($value); + + return '0x' . $bn->toHex(true); + } +} \ No newline at end of file