diff --git a/src/Symfony/Component/Validator/Constraints/BicValidator.php b/src/Symfony/Component/Validator/Constraints/BicValidator.php index 23c33c812e..a8e9d07ba6 100644 --- a/src/Symfony/Component/Validator/Constraints/BicValidator.php +++ b/src/Symfony/Component/Validator/Constraints/BicValidator.php @@ -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); + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php index 2d57ab0ec3..e0d7e0db63 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php @@ -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