diff --git a/src/Formatters/Address.php b/src/Formatters/Address.php new file mode 100644 index 0000000..16839cc --- /dev/null +++ b/src/Formatters/Address.php @@ -0,0 +1,39 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class Address implements IFormatter +{ + /** + * format + * to do: iban + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + if (Utils::isAddress($value)) { + $value = mb_strtolower($value); + + if (Utils::isZeroPrefixed($value)) { + return $value; + } + return '0x' . $value; + } + throw new InvalidArgumentException('The address to inputFormat is invalid.'); + } +} \ No newline at end of file diff --git a/src/Formatters/IFormatter.php b/src/Formatters/IFormatter.php new file mode 100644 index 0000000..c7b0cf6 --- /dev/null +++ b/src/Formatters/IFormatter.php @@ -0,0 +1,23 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +interface IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value); +} \ No newline at end of file diff --git a/test/unit/AddressFormatterTest.php b/test/unit/AddressFormatterTest.php new file mode 100644 index 0000000..5b8429e --- /dev/null +++ b/test/unit/AddressFormatterTest.php @@ -0,0 +1,49 @@ +formatter = new Address; + } + + /** + * testFormat + * + * @return void + */ + public function testFormat() + { + $formatter = $this->formatter; + + $address = $formatter->format('0Xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $address = $formatter->format('0XCA35B7D915458EF540ADE6068DFE2F44E8FA733C'); + + $this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $address = $formatter->format('0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C'); + + $this->assertEquals($address, '0xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + } +} \ No newline at end of file