8000 [Validator] Check the BIC country with symfony/intl · symfony/symfony@27bd3a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27bd3a8

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

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
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\UnexpectedTypeException;
1618

1719
/**
1820
* @author Michael Hirschler <michael.vhirsch@gmail.com>
@@ -30,6 +32,10 @@ public function validate($value, Constraint $constraint)
3032
return;
3133
}
3234

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

3541
// the bic must be either 8 or 11 characters long
@@ -63,7 +69,11 @@ public function validate($value, Constraint $constraint)
6369
}
6470

6571
// next 2 letters must be alphabetic (country code)
66-
if (!ctype_alpha(substr($canonicalize, 4, 2))) {
72+
if (!class_exists(Intl::class)) {
73+
throw new \LogicException('The "symfony/intl" component is required to use the Bic constraint.');
74+
}
75+
$countries = Intl::getRegionBundle()->getCountryNames();
76+
if (!isset($countries[substr($canonicalize, 4, 2)])) {
6777
$this->context->buildViolation($constraint->message)
6878
->setParameter('{{ value }}', $this->formatValue($value))
6979
->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)
0