Improved BIC + IBAN validator for some special cases

This commit is contained in:
Javier Eguiluz 2019-01-03 11:00:42 +01:00 committed by Nicolas Grekas
parent 9634a8fd75
commit 88a1696a64
2 changed files with 60 additions and 1 deletions

View File

@ -29,6 +29,25 @@ use Symfony\Component\Validator\Exception\UnexpectedValueException;
*/
class BicValidator extends ConstraintValidator
{
private const BIC_COUNTRY_TO_IBAN_COUNTRY_MAP = array(
// Reference: https://www.ecbs.org/iban/france-bank-account-number.html
'GF' => 'FR', // French Guiana
'PF' => 'FR', // French Polynesia
'TF' => 'FR', // French Southern Territories
'GP' => 'FR', // Guadeloupe
'MQ' => 'FR', // Martinique
'YT' => 'FR', // Mayotte
'NC' => 'FR', // New Caledonia
'RE' => 'FR', // Reunion
'PM' => 'FR', // Saint Pierre and Miquelon
'WF' => 'FR', // Wallis and Futuna Islands
// Reference: https://www.ecbs.org/iban/united-kingdom-uk-bank-account-number.html
'JE' => 'GB', // Jersey
'IM' => 'GB', // Isle of Man
'GG' => 'GB', // Guernsey
'VG' => 'GB', // British Virgin Islands
);
private $propertyAccessor;
public function __construct(PropertyAccessor $propertyAccessor = null)
@ -126,7 +145,7 @@ class BicValidator extends ConstraintValidator
return;
}
$ibanCountryCode = substr($iban, 0, 2);
if (ctype_alpha($ibanCountryCode) && substr($canonicalize, 4, 2) !== $ibanCountryCode) {
if (ctype_alpha($ibanCountryCode) && !$this->bicAndIbanCountriesMatch(substr($canonicalize, 4, 2), $ibanCountryCode)) {
$this->context->buildViolation($constraint->ibanMessage)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ iban }}', $iban)
@ -146,4 +165,9 @@ class BicValidator extends ConstraintValidator
return $this->propertyAccessor;
}
private function bicAndIbanCountriesMatch(string $bicCountryCode, string $ibanCountryCode): bool
{
return $ibanCountryCode === $bicCountryCode || $ibanCountryCode === (self::BIC_COUNTRY_TO_IBAN_COUNTRY_MAP[$bicCountryCode] ?? null);
}
}

View File

@ -221,6 +221,41 @@ class BicValidatorTest extends ConstraintValidatorTestCase
array('DEUTAT2lxxx', Bic::INVALID_CASE_ERROR),
);
}
/**
* @dataProvider getValidBicSpecialCases
*
* Some territories have their own ISO country code but can use another country code
* for IBAN accounts. Example: "French Guiana" (country code "GF") can use FR too.
*/
public function testValidBicSpecialCases(string $bic, string $iban)
{
$constraint = new Bic(array('iban' => $iban));
$this->validator->validate($bic, $constraint);
$this->assertNoViolation();
}
public function getValidBicSpecialCases()
{
// FR related special cases
yield array('BNPAGFGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAPFGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPATFGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAGPGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAMQGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAYTGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPANCGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAREGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAPMGX', 'FR14 2004 1010 0505 0001 3M02 606');
yield array('BNPAWFGX', 'FR14 2004 1010 0505 0001 3M02 606');
// GB related special cases
yield array('BARCJESA', 'GB12 CPBK 0892 9965 0449 911');
yield array('BARCIMSA', 'GB12 CPBK 0892 9965 0449 911');
yield array('BARCGGSA', 'GB12 CPBK 0892 9965 0449 911');
yield array('BARCVGSA', 'GB12 CPBK 0892 9965 0449 911');
}
}
class BicComparisonTestClass