ZipCodeFixer.php 878 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model;
  7. /**
  8. * Fixer class for fixing user errors related to postal codes
  9. *
  10. * 4 digit zipcodes to 5 digit zipcodes prefixed with a 0
  11. */
  12. class ZipCodeFixer
  13. {
  14. /**
  15. * Corrects erroneously entered US ZIP or ZIP+4 codes where 0s have been omitted from the beginning
  16. *
  17. * @param string $postcode A US ZIP or ZIP+4 postcode
  18. * @return string Same ZIP code prepended with 0's if length of first part is less than 5
  19. */
  20. public function fix($postcode)
  21. {
  22. if ($postcode === null) {
  23. return null;
  24. }
  25. $parts = explode('-', $postcode);
  26. $parts[0] = str_pad($parts[0], 5, '0', STR_PAD_LEFT);
  27. return implode('-', $parts);
  28. }
  29. }