Skip to content

Improved BIC + IBAN validator for some special cases #29755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Symfony/Component/Validator/Constraints/BicValidator.php
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
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