8000 feature #28473 [Validator] Check the BIC country with symfony/intl (s… · symfony/symfony@bf4d011 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf4d011

Browse files
feature #28473 [Validator] Check the BIC country with symfony/intl (sylfabre)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Validator] Check the BIC country with symfony/intl | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28167 | License | MIT | Doc PR | N/A Check the BIC country code against the list from Intl component instead of a simple check alphabetical test. This PR uses the Intl component which is not part of the required dependencies of the Validator component (https://github.com/symfony/validator/blob/master/composer.json): `symfony/intl` is only required for dev. So I'm making a PR against master because it may break existing code. But `CountryValidator` does the same so it may not be an issue after all. Commits ------- 27bd3a8 [Validator] Check the BIC country with symfony/intl Fix #28167
2 parents a1ca55b + 27bd3a8 commit bf4d011

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