Skip to content

Instantly share code, notes, and snippets.

@ferdiunal
Forked from emir/tax_validation.php
Last active August 4, 2021 12:21
Show Gist options
  • Save ferdiunal/a8bd673da6aaebd8f92239ddac2a6194 to your computer and use it in GitHub Desktop.
Save ferdiunal/a8bd673da6aaebd8f92239ddac2a6194 to your computer and use it in GitHub Desktop.
PHP Vergi Numarası Doğrulama
<?php
/**
* This method logically validates Turkish VAT number
*
* @param string $taxNumber
* @return bool
*/
public function validateTaxNumber(string $taxNumber): bool
{
if (strlen($taxNumber) !== 10) {
return false;
}
$total = 0;
$checkNum = null;
for ($i = 0; $i < 9; $i++) {
$tmp1 = ($taxNumber[$i] + (9 - $i)) % 10;
$tmp2 = ($tmp1 * (2 ** (9 - $i))) % 9;
if ($tmp1 !== 0 && $tmp2 === 0) {
$tmp2 = 9;
}
$total += $tmp2;
}
$checkNum = ($total % 10 !== 0) ? (10 - ($total % 10)) : 0;
return ((int)$taxNumber[9] === $checkNum) ?? false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment