@@ -40,23 +40,25 @@ public function validate($value, Constraint $constraint)
4040 $ charset = 'UTF-8 ' ;
4141 }
4242
43- if (function_exists ('iconv_strlen ' )) {
44- $ length = @iconv_strlen ($ stringValue , $ constraint ->charset );
45- $ invalidCharset = false === $ length ;
43+ if ('UTF-8 ' === $ charset ) {
44+ if (!preg_match ('//u ' , $ stringValue )) {
45+ $ invalidCharset = true ;
46+ } elseif (function_exists ('utf8_decode ' )) {
47+ $ length = strlen (utf8_decode ($ stringValue ));
48+ } else {
49+ preg_replace ('/./u ' , '' , $ stringValue , -1 , $ length );
50+ }
4651 } elseif (function_exists ('mb_strlen ' )) {
47- if (mb_check_encoding ($ stringValue , $ constraint ->charset )) {
52+ if (@ mb_check_encoding ($ stringValue , $ constraint ->charset )) {
4853 $ length = mb_strlen ($ stringValue , $ constraint ->charset );
4954 } else {
5055 $ invalidCharset = true ;
5156 }
52- } elseif ('UTF-8 ' !== $ charset ) {
53- $ length = strlen ($ stringValue );
54- } elseif (!preg_match ('//u ' , $ stringValue )) {
55- $ invalidCharset = true ;
56- } elseif (function_exists ('utf8_decode ' )) {
57- $ length = strlen (utf8_decode ($ stringValue ));
57+ } elseif (function_exists ('iconv_strlen ' )) {
58+ $ length = @iconv_strlen ($ stringValue , $ constraint ->charset );
59+ $ invalidCharset = false === $ length ;
5860 } else {
59- preg_replace ( ' /./u ' , '' , $ stringValue , - 1 , $ length );
61+ $ length = strlen ( $ stringValue );
6062 }
6163
6264 if ($ invalidCharset ) {
0 commit comments