UK Bank Modulus Check Library
This PHP library allows you to check the validity of UK bank account information. To perform this task it uses publicly available information and the specifications published by VocaLink.
You must be aware that this library cannot tell you that the account number and sort code associate to an active account, it only tells you that they could be assigned to an account. Neither does it cover all sort codes, some of which are outside the specification and you should assume those details are valid unless otherwise known.
This library provides a simple class to perform bank account validation and
normalisation, through the
By default the
BankModulus class will use the current modulus specification
issued by VocaLink, the default specification factory will pick the correct
specification based on the date.
normalize(string &$sortCode, string &$accountNumber)
This performs normalisation of sort code and account numbers according to rules issued by some banks. As an example 6 digit account numbers get padded with two zeros.
The result of normalisation is assigned back to the input parameters.
check(string $sortCode, string $accountNumber) : boolean
This is the primary method to ascertain if the details supplied are invalid, it returns
falseif the sort code and account number are invalid otherwise it will return
true. If the details provided are syntactically valid but fall outside the specification then this method considers them valid.
lookup(string $sortCode, string $accountNumber) : Result
Use this method to fetch more in depth information on the sort code and account number. See documentation on the
Resultclass for more information.
__construct(SpecFactoryInterface $specFactory, NormalizerInterface $normalizer)
You can change which specification factory and normalizer is used at the time of construction, otherwise defaults will be used.
<?php use Cs278\BankModulus\BankModulus; $modulus = new BankModulus(); $modulus->check('08-16-32', '12481632'); $sortCode = '12-24-48'; $accountNumber = '123456'; $modulus->normalize($sortCode, $accountNumber); var_dump($sortCode, $accountNumber); // string(6) "122448" // string(8) "00123456"
Result class provides detailed information on the validation and
getSortCode() : SortCode
SortCodeobject representing the normalised sort code.
getAccountNumber() : string
Returns a string of the normalised account number.
isValidated() : boolean
Returns a boolean which is true iff the sort code and account number could be validated using the specification.
isValid($assume = true) : boolean
Returns a boolean representing the result of the modulus validation, if the sort code and account number could not be validated the value of the
$assumeargument is returned.
The default specification factory (
Spec\DefaultSpecFactory) picks the most
appropriate specification for the current time.
If you need to test the validity of a sort code and account number at a particular
point in time, you can use the
withDate(\DateTimeInterface|string $date): self
method to overload the currently used time reference. If you supply an object to
this method it will convert it to the date in
Europe/London, string date is
This library will throw exceptions implementing
something unexpected occurs.
The supplied account number was not a string of 8 numerical digits.
This is thrown by an instance of
Spec\SpecInterfacewhen it cannot determine if the details are correct because they fall outside the specification.
The supplied sort code was not a string of 6 numerical digits.
Currently this library implements 3 similar algorithms to perform modulus calculations, these are defined in the VocaLink specification.
Standard Modulus (
Multiplication of each digit in sort code and account number with a corresponding weight value, the results are summed and then divided by
11depending on the algorithm being used. A remainder of
0means the input was valid.
Double Alternate (
Each digit of the sort code and account number is multiplied by the corresponding weight value, each result is then split into digits and summed (e.g.
2+4). Then the results are summed and divided by
10, again a remainder of
0is indicative of success.