From c6c5cc9251256375164cbec85bf0b1f5cfb3cab7 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 28 Dec 2017 11:04:56 +0800 Subject: [PATCH] Boolean formatter. --- src/Formatters/Boolean.php | 33 ++++++++++++++++++++++ test/unit/BooleanFormatterTest.php | 45 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/Formatters/Boolean.php create mode 100644 test/unit/BooleanFormatterTest.php diff --git a/src/Formatters/Boolean.php b/src/Formatters/Boolean.php new file mode 100644 index 0000000..652190e --- /dev/null +++ b/src/Formatters/Boolean.php @@ -0,0 +1,33 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class Boolean implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return int + */ + public static function format($value) + { + if (!is_bool($value)) { + throw new InvalidArgumentException('The value to inputFormat function must be boolean.'); + } + return (int) $value; + } +} \ No newline at end of file diff --git a/test/unit/BooleanFormatterTest.php b/test/unit/BooleanFormatterTest.php new file mode 100644 index 0000000..f5dff3b --- /dev/null +++ b/test/unit/BooleanFormatterTest.php @@ -0,0 +1,45 @@ +formatter = new Boolean; + } + + /** + * testFormat + * + * @return void + */ + public function testFormat() + { + $formatter = $this->formatter; + + $boolean = $formatter->format(true); + + $this->assertEquals($boolean, 1); + + $boolean = $formatter->format(false); + + $this->assertEquals($boolean, 0); + } +} \ No newline at end of file