Skip to content

Commit

Permalink
bug #29755 Improved BIC + IBAN validator for some special cases (javi…
Browse files Browse the repository at this point in the history
…ereguiluz)

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
-------

88a1696 Improved BIC + IBAN validator for some special cases
  • Loading branch information
nicolas-grekas committed Jan 3, 2019
2 parents 3860d68 + 88a1696 commit 743b52f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Symfony/Component/Validator/Constraints/BicValidator.php
Expand Up @@ -29,6 +29,25 @@
*/
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)
Expand Down Expand Up @@ -126,7 +145,7 @@ public function validate($value, Constraint $constraint)
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)
Expand All @@ -146,4 +165,9 @@ private function getPropertyAccessor(): PropertyAccessor

return $this->propertyAccessor;
}

private function bicAndIbanCountriesMatch(string $bicCountryCode, string $ibanCountryCode): bool
{
return $ibanCountryCode === $bicCountryCode || $ibanCountryCode === (self::BIC_COUNTRY_TO_IBAN_COUNTRY_MAP[$bicCountryCode] ?? null);
}
}
Expand Up @@ -221,6 +221,41 @@ public function getInvalidBics()
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
Expand Down

0 comments on commit 743b52f

Please sign in to comment.