Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This method logically validates Turkish VAT number
  5. *
  6. * @param string $taxNumber
  7. * @return bool
  8. */
  9. public function validateTaxNumber(string $taxNumber): bool
  10. {
  11. if (strlen($taxNumber) !== 10) {
  12. return false;
  13. }
  14.  
  15. $total = 0;
  16. $checkNum = null;
  17.  
  18. for ($i = 0; $i < 9; $i++) {
  19. $tmp1 = ($taxNumber[$i] + (9 - $i)) % 10;
  20. $tmp2 = ($tmp1 * (2 ** (9 - $i))) % 9;
  21.  
  22. if ($tmp1 !== 0 && $tmp2 === 0) {
  23. $tmp2 = 9;
  24. }
  25.  
  26. $total += $tmp2;
  27. }
  28.  
  29. if ($total % 10 === 0) {
  30. $checkNum = 0;
  31. } else {
  32. $checkNum = 10 - ($total % 10);
  33. }
  34.  
  35. if ((int)$taxNumber[9] !== $checkNum) {
  36. return false;
  37. }
  38.  
  39. return true;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement