A library for bank account validation. Now with badges!
Validating user inputs can be a nightmare. Bank account validation used to be one of the worst cases since there are many formats and algorithms you have to implement. But not anymore - say hello to Banger!
You can't install this library quite yet, though it will hopefully be published to packagist and ready to install via composer soon.
composer require eag/banger # Right now you have to imagine this works.
This library requires PHP 8.0 or later and BCMath extension for some specific algorithms.
All features of this library were made possible via a set of classes
implementing BankAccountValidator
interface. You can use them directly or
wrap one or more in an aggregator class.
use Eag\Banger\Validator\NationalBankAccountValidator;
use Eag\Banger\Validator\NationalBankAccountValidator\CzechBankAccountValidator;
/** @var NationalBankAccountValidator $validator */
$validator = new CzechBankAccountValidator();
There are two ways - either use specific national bank account validator, or use a subset of implemented bank account validators.
This way is perfect if you want an easy and simple solution for validating one specific country and nothing else.
use Eag\Banger\Validator\NationalBankAccountValidator;
use Eag\Banger\Validator\NationalBankAccountValidator\CzechBankAccountValidator;
/** @var NationalBankAccountValidator $validator */
$validator = new CzechBankAccountValidator();
try {
$validator->validate('705-77628031/0710');
} catch (\Eag\Banger\Exception\InvalidBankAccountNumber){
// Ooops, that doesn't seem to be a valid Czech bank account number
}
// We've got a valid Czech bank account number
use Eag\Banger\Validator\BankAccountValidator;
use Eag\Banger\Validator\NationalBankAccountValidator;
use Eag\Banger\Validator\PhpBankAccountValidator;
/**
* An array of validators for each country whose bank accounts you want to validate.
* @var NationalBankAccountValidator[] $validators
*/
$validators = [
new CzechBankAccountValidator(),
new HawaiianBankAccountValidator(),
...
];
/** @var BankAccountValidator $validator */
$validator = new PhpBankAccountValidator($validators);
try {
$validator->validate('705-77628031/0710', 'CZE');
} catch (\Eag\Banger\Exception\InvalidBankAccountNumber){
// Ooops, that doesn't seem to be a valid Czech bank account number
}
// We've got a valid Czech bank account number
Maybe you have noticed you need to know the bank account's country to validate
it. Sometimes you already know that and want to validate a bank account for a
specific country and sometimes you don't really care that much and just want to
know it's valid - BankAccountCountryDeterminer
to the rescue!
A determiner is your friend when you want to know the country of a bank account.
use Eag\Banger\Determiner\BankAccountCountryDeterminer;
use Eag\Banger\Determiner\PhpBankAccountCountryDeterminer;
use Eag\Banger\Validator\NationalBankAccountValidator;
/**
* An array of validators for each country you wish to determine.
* @var NationalBankAccountValidator[] $validators
*/
$validators = [
new CzechBankAccountValidator(),
new HawaiianBankAccountValidator()
];
/** @var BankAccountCountryDeterminer $determiner */
$determiner = new PhpBankAccountCountryDeterminer($validators);
echo $determiner->determine('705-77628031/0710'); // CZE
echo $determiner->determine('123431'); // throws UnknownBankAccountCountry exception
Combine with a BankAccountValidator
to validate bank account whose country you don't know.
use Eag\Banger\Determiner\BankAccountCountryDeterminer;
use Eag\Banger\Determiner\PhpBankAccountCountryDeterminer;
use Eag\Banger\Validator\BankAccountValidator;
use Eag\Banger\Validator\PhpBankAccountValidator;
$bankAccountNumber = '705-77628031/0710';
$validators = [...];
/** @var BankAccountCountryDeterminer $determiner */
$determiner = new PhpBankAccountCountryDeterminer($validators);
/** @var BankAccountValidator $validator */
$validator = new PhpBankAccountValidator($validators);
try {
$validator->validate($bankAccountNumber, $determiner->determine($bankAccountNumber));
} catch (\Eag\Banger\Exception\InvalidBankAccountNumber){
// Ooops, that doesn't seem to be a valid bank account number
}
// We've got a valid bank account number
Why don't you just give me a single simple class that validates all bank accounts?
There are two main reasons:
- This way you can use your own implementations of
NationalBankAccountValidator
when it is not implemented yet or the current implementation doesn't meet your expectations. - We didn't implement all validators just yet. Every time we would add a new validator we would also introduce a breaking change to existing codebases.