Helper classes to calculate and validate ckecksums
Install using composer. Exists as byrokrat/checkdigit in the packagist repository.
composer require byrokrat/checkdigit
The Calculator
interface defines two methods:
isValid(string $number) : bool
checks if number contains a valid check digit.calculateCheckDigit(string $number) : string
calculates the check digit for number.
Implementations include:
Modulo10
andLuhn
for modulo 10 check digits (Luhn is simply a shorthand for Modulo10).Modulo11
for modulo 11 check digits.Modulo97
for modulo 97 check digits.
use byrokrat\checkdigit\Luhn;
$luhn = new Luhn();
$luhn->isValid('55555551'); // true
$luhn->isValid('55555550'); // false
$luhn->calculateCheckDigit('5555555'); // '1'
Checkdigit is covered under the WTFPL.
@author Hannes Forsgård (hannes.forsgard@fripost.org)