Skip to content

Commit

Permalink
Add BG validation algorithm for foreign natural persons
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-coder authored and DragonBe committed Oct 28, 2020
1 parent 3b2fd2e commit 6c006df
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Vies/Validator/ValidatorBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function validate(string $vatNumber): bool
}

if (10 === $vatNumberLength) {
return $this->validateNaturalPerson($vatNumber);
return $this->validateNaturalPerson($vatNumber)
|| $this->validateForeignNaturalPerson($vatNumber);
}
return $this->validateBusiness($vatNumber);
}
Expand Down Expand Up @@ -96,4 +97,28 @@ private function validateNaturalPerson(string $vatNumber): bool

return $checkVal == (int) $vatNumber[9];
}

/**
* Validate VAT ID's for foreign natural persons
*
* @param string $vatNumber
* @return bool
* @see https://github.com/yolk/valvat/blob/master/lib/valvat/checksum/bg.rb
*/
private function validateForeignNaturalPerson(string $vatNumber): bool
{
$weights = [21, 19, 17, 13, 11, 9, 7, 3, 1];
$checkVal = $this->sumWeights($weights, $vatNumber);

if ($checkVal % 11 == 10) {
$weights = [3, 4, 5, 6, 7, 8, 9, 10];
$checkVal = $this->sumWeights($weights, $vatNumber);

$checkVal = ($checkVal % 11) == 10 ? 0 : ($checkVal % 11);
} else {
$checkVal = $checkVal % 10;
}

return $checkVal == (int) $vatNumber[9];
}
}

0 comments on commit 6c006df

Please sign in to comment.