66use ProgrammatorDev \YetAnotherPhpValidator \Exception \UnexpectedValueException ;
77use ProgrammatorDev \YetAnotherPhpValidator \Exception \ValidationException ;
88use ProgrammatorDev \YetAnotherPhpValidator \Rule \Util \ValidatableTrait ;
9- use Symfony \Component \OptionsResolver \OptionsResolver ;
109
1110class All extends AbstractRule implements RuleInterface
1211{
1312 use ValidatableTrait;
1413
15- private array $ options ;
14+ private string $ message ;
1615
1716 /**
1817 * @param RuleInterface[] $constraints
1918 */
2019 public function __construct (
2120 private readonly array $ constraints ,
22- array $ options = []
21+ ? string $ message = null
2322 )
2423 {
25- $ resolver = new OptionsResolver ();
26-
27- $ resolver ->setDefaults (['message ' => 'At "{{ key }}": {{ message }} ' ]);
28-
29- $ resolver ->setAllowedTypes ('message ' , 'string ' );
30-
31- $ this ->options = $ resolver ->resolve ($ options );
24+ $ this ->message = $ message ?? 'At "{{ key }}": {{ message }} ' ;
3225 }
3326
3427 public function assert (mixed $ value , string $ name ): void
@@ -45,23 +38,23 @@ public function assert(mixed $value, string $name): void
4538 );
4639 }
4740
48- foreach ( $ value as $ key => $ input ) {
49- foreach ($ this -> constraints as $ constraint ) {
50- try {
41+ try {
42+ foreach ($ value as $ key => $ input ) {
43+ foreach ( $ this -> constraints as $ constraint ) {
5144 $ constraint ->assert ($ input , $ name );
5245 }
53- catch (ValidationException $ exception ) {
54- throw new AllException (
55- message: $ this ->options ['message ' ],
56- parameters: [
57- 'value ' => $ value ,
58- 'name ' => $ name ,
59- 'key ' => $ key ,
60- 'message ' => $ exception ->getMessage ()
61- ]
62- );
63- }
6446 }
6547 }
48+ catch (ValidationException $ exception ) {
49+ throw new AllException (
50+ message: $ this ->message ,
51+ parameters: [
52+ 'value ' => $ value ,
53+ 'name ' => $ name ,
54+ 'key ' => $ key ,
55+ 'message ' => $ exception ->getMessage ()
56+ ]
57+ );
58+ }
6659 }
6760}
0 commit comments