15
15
use Symfony \Component \PropertyAccess \PropertyAccess ;
16
16
use Symfony \Component \Validator \Constraint ;
17
17
use Symfony \Component \Validator \Exception \ConstraintDefinitionException ;
18
+ use Symfony \Component \Validator \Exception \InvalidArgumentException ;
18
19
use Symfony \Component \Validator \Exception \LogicException ;
19
20
20
21
/**
27
28
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )]
28
29
class Bic extends Constraint
29
30
{
31
+ public const VALIDATION_MODE_STRICT = 'strict ' ;
32
+ public const VALIDATION_MODE_CASE_INSENSITIVE = 'case-insensitive ' ;
33
+
34
+ public const VALIDATION_MODES = [
35
+ self ::VALIDATION_MODE_STRICT ,
36
+ self ::VALIDATION_MODE_CASE_INSENSITIVE ,
37
+ ];
38
+
30
39
public const INVALID_LENGTH_ERROR = '66dad313-af0b-4214-8566-6c799be9789c ' ;
31
40
public const INVALID_CHARACTERS_ERROR = 'f424c529-7add-4417-8f2d-4b656e4833e2 ' ;
32
41
/**
@@ -49,25 +58,42 @@ class Bic extends Constraint
49
58
public string $ ibanMessage = 'This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}. ' ;
50
59
public ?string $ iban = null ;
51
60
public ?string $ ibanPropertyPath = null ;
61
+ public ?string $ mode = self ::VALIDATION_MODE_STRICT ;
52
62
53
63
/**
54
64
* @param array<string,mixed>|null $options
55
65
* @param string|null $iban An IBAN value to validate that its country code is the same as the BIC's one
56
66
* @param string|null $ibanPropertyPath Property path to the IBAN value when validating objects
57
67
* @param string[]|null $groups
68
+ * @param string|null $mode The mode used to validate the BIC; pass null to use the default mode (strict)
58
69
*/
59
- public function __construct (?array $ options = null , ?string $ message = null , ?string $ iban = null , ?string $ ibanPropertyPath = null , ?string $ ibanMessage = null , ?array $ groups = null , mixed $ payload = null )
60
- {
70
+ public function __construct (
71
+ ?array $ options = null ,
72
+ ?string $ message = null ,
73
+ ?string $ iban = null ,
74
+ ?string $ ibanPropertyPath = null ,
75
+ ?string $ ibanMessage = null ,
76
+ ?array $ groups = null ,
77
+ mixed $ payload = null ,
78
+ ?string $ mode = null ,
79
+ ) {
61
80
if (!class_exists (Countries::class)) {
62
81
throw new LogicException ('The Intl component is required to use the Bic constraint. Try running "composer require symfony/intl". ' );
63
82
}
83
+ if (\is_array ($ options ) && \array_key_exists ('mode ' , $ options ) && !\in_array ($ options ['mode ' ], self ::VALIDATION_MODES , true )) {
84
+ throw new InvalidArgumentException ('The "mode" parameter value is not valid. ' );
85
+ }
86
+ if (null !== $ mode && !\in_array ($ mode , self ::VALIDATION_MODES , true )) {
87
+ throw new InvalidArgumentException ('The "mode" parameter value is not valid. ' );
88
+ }
64
89
65
90
parent ::__construct ($ options , $ groups , $ payload );
66
91
67
92
$ this ->message = $ message ?? $ this ->message ;
68
93
$ this ->ibanMessage = $ ibanMessage ?? $ this ->ibanMessage ;
69
94
$ this ->iban = $ iban ?? $ this ->iban ;
70
95
$ this ->ibanPropertyPath = $ ibanPropertyPath ?? $ this ->ibanPropertyPath ;
96
+ $ this ->mode = $ mode ?? $ this ->mode ;
71
97
72
98
if (null !== $ this ->iban && null !== $ this ->ibanPropertyPath ) {
73
99
throw new ConstraintDefinitionException ('The "iban" and "ibanPropertyPath" options of the Iban constraint cannot be used at the same time. ' );
0 commit comments