Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 207ace5

Browse files
committed
[Validator] Check the BIC country with symfony/intl
Fix #28167
1 parent 8ab7077 commit 207ace5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Validator/Constraints/BicValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Intl\Intl;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintValidator;
17+
use Symfony\Component\Validator\Exception\RuntimeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1619

1720
/**
1821
* @author Michael Hirschler <[email protected]>
@@ -30,6 +33,10 @@ public function validate($value, Constraint $constraint)
3033
return;
3134
}
3235

36+
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
37+
throw new UnexpectedTypeException($value, 'string');
38+
}
39+
3340
$canonicalize = str_replace(' ', '', $value);
3441

3542
// the bic must be either 8 or 11 characters long
@@ -63,7 +70,11 @@ public function validate($value, Constraint $constraint)
6370
}
6471

6572
// next 2 letters must be alphabetic (country code)
66-
if (!ctype_alpha(substr($canonicalize, 4, 2))) {
73+
if (!class_exists(Intl::class)) {
74+
throw new RuntimeException('The "symfony/intl" component is required to use the Bic constraint.');
75+
}
76+
$countries = Intl::getRegionBundle()->getCountryNames();
77+
if (!isset($countries[substr($canonicalize, 4, 2)])) {
6778
$this->context->buildViolation($constraint->message)
6879
->setParameter('{{ value }}', $this->formatValue($value))
6980
->setCode(Bic::INVALID_COUNTRY_CODE_ERROR)

src/Symfony/Component/Validator/Tests/Constraints/BicValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public function testEmptyStringIsValid()
3636
$this->assertNoViolation();
3737
}
3838

39+
/**
40+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
41+
*/
42+
public function testExpectsStringCompatibleType()
43+
{
44+
$this->validator->validate(new \stdClass(), new Bic());
45+
}
46+
3947
/**
4048
* @dataProvider getValidBics
4149
*/
@@ -92,6 +100,7 @@ public function getInvalidBics()
92100
array('DEUT12HH', Bic::INVALID_COUNTRY_CODE_ERROR),
93101
array('DSBAC6BXSHA', Bic::INVALID_COUNTRY_CODE_ERROR),
94102
array('DSBA5NBXSHA', Bic::INVALID_COUNTRY_CODE_ERROR),
103+
array('DSBAAABXSHA', Bic::INVALID_COUNTRY_CODE_ERROR),
95104

96105
// branch code error
97106
array('THISSVAL1D]', Bic::INVALID_CHARACTERS_ERROR),

0 commit comments

Comments
 (0)