AddressFormatter.php 930 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * This file is part of web3.php package.
  4. *
  5. * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
  6. *
  7. * @author Peter Lai <alk03073135@gmail.com>
  8. * @license MIT
  9. */
  10. namespace blockchain\web3\src\Formatters;
  11. use InvalidArgumentException;
  12. use blockchain\web3\src\Utils;
  13. use blockchain\web3\src\Formatters\IFormatter;
  14. use blockchain\web3\src\Formatters\IntegerFormatter;
  15. class AddressFormatter implements IFormatter
  16. {
  17. /**
  18. * format
  19. * to do: iban
  20. *
  21. * @param mixed $value
  22. * @return string
  23. */
  24. public static function format($value)
  25. {
  26. $value = (string) $value;
  27. if (Utils::isAddress($value)) {
  28. $value = mb_strtolower($value);
  29. if (Utils::isZeroPrefixed($value)) {
  30. return $value;
  31. }
  32. return '0x' . $value;
  33. }
  34. $value = IntegerFormatter::format($value, 40);
  35. return '0x' . $value;
  36. }
  37. }