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