11
11
12
12
namespace Symfony \Component \Validator \Constraints ;
13
13
14
+ use Symfony \Component \Intl \Intl ;
14
15
use Symfony \Component \Validator \Constraint ;
15
16
use Symfony \Component \Validator \ConstraintValidator ;
17
+ use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
16
18
17
19
/**
18
20
* @author Michael Hirschler <michael.vhirsch@gmail.com>
@@ -30,6 +32,10 @@ public function validate($value, Constraint $constraint)
30
32
return ;
31
33
}
32
34
35
+ if (!is_scalar ($ value ) && !(\is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
36
+ throw new UnexpectedTypeException ($ value , 'string ' );
37
+ }
38
+
33
39
$ canonicalize = str_replace (' ' , '' , $ value );
34
40
35
41
// the bic must be either 8 or 11 characters long
@@ -63,7 +69,11 @@ public function validate($value, Constraint $constraint)
63
69
}
64
70
65
71
// 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 )])) {
67
77
$ this ->context ->buildViolation ($ constraint ->message )
68
78
->setParameter ('{{ value }} ' , $ this ->formatValue ($ value ))
69
79
->setCode (Bic::INVALID_COUNTRY_CODE_ERROR )
0 commit comments