bug #29755 Improved BIC + IBAN validator for some special cases (javiereguiluz)

This PR was squashed before being merged into the 4.3-dev branch (closes #29755).

Discussion
----------

Improved BIC + IBAN validator for some special cases

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

While blogging about [Improved Financial Constraints in Symfony 4.3](https://symfony.com/blog/new-in-symfony-4-3-improved-financial-constraints) I found this resource that mentions some exceptions when validating BIC + IBAN: https://wiki.xmldation.com/index.php?title=Support/EPC/IBAN_%26_BIC_relation

I don't know if that resource is fully reliable ... but I checked it in the official "European Banking Resources" website (https://www.ecbs.org/iban/) and most of it matches, so I think we can safely make this change.

Commits
-------

88a1696a64 Improved BIC + IBAN validator for some special cases
This commit is contained in:
Nicolas Grekas 2019-01-03 15:56:41 +01:00
commit 743b52fa4e
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