File tree Expand file tree Collapse file tree 4 files changed +40
-2
lines changed Expand file tree Collapse file tree 4 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ class In extends Rule
99
1010 protected $ message = "The :attribute is not allowing :value " ;
1111
12+ protected $ strict = false ;
13+
1214 public function fillParameters (array $ params )
1315 {
1416 if (count ($ params ) == 1 AND is_array ($ params [0 ])) {
@@ -18,12 +20,17 @@ public function fillParameters(array $params)
1820 return $ this ;
1921 }
2022
23+ public function strict ($ strict = true )
24+ {
25+ $ this ->strict = $ strict ;
26+ }
27+
2128 public function check ($ value )
2229 {
2330 $ this ->requireParameters (['allowed_values ' ]);
2431
2532 $ allowed_values = $ this ->parameter ('allowed_values ' );
26- return in_array ($ value , $ allowed_values );
33+ return in_array ($ value , $ allowed_values, $ this -> strict );
2734 }
2835
2936}
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ class NotIn extends Rule
99
1010 protected $ message = "The :attribute is not allowing :value " ;
1111
12+ protected $ strict = false ;
13+
1214 public function fillParameters (array $ params )
1315 {
1416 if (count ($ params ) == 1 AND is_array ($ params [0 ])) {
@@ -18,11 +20,16 @@ public function fillParameters(array $params)
1820 return $ this ;
1921 }
2022
23+ public function strict ($ strict = true )
24+ {
25+ $ this ->strict = $ strict ;
26+ }
27+
2128 public function check ($ value )
2229 {
2330 $ this ->requireParameters (['disallowed_values ' ]);
2431 $ disallowed_values = (array ) $ this ->parameter ('disallowed_values ' );
25- return !in_array ($ value , $ disallowed_values );
32+ return !in_array ($ value , $ disallowed_values, $ this -> strict );
2633 }
2734
2835}
Original file line number Diff line number Diff line change @@ -21,4 +21,16 @@ public function testInvalids()
2121 $ this ->assertFalse ($ this ->rule ->fillParameters ([1 ,2 ,3 ])->check (4 ));
2222 }
2323
24+ public function testStricts ()
25+ {
26+ // Not strict
27+ $ this ->assertTrue ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
28+ $ this ->assertTrue ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (true ));
29+
30+ // Strict
31+ $ this ->rule ->strict ();
32+ $ this ->assertFalse ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
33+ $ this ->assertFalse ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
34+ }
35+
2436}
Original file line number Diff line number Diff line change @@ -21,4 +21,16 @@ public function testInvalids()
2121 $ this ->assertFalse ($ this ->rule ->fillParameters (['bar ' , 'baz ' , 'qux ' ])->check ('bar ' ));
2222 }
2323
24+ public function testStricts ()
25+ {
26+ // Not strict
27+ $ this ->assertFalse ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
28+ $ this ->assertFalse ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (true ));
29+
30+ // Strict
31+ $ this ->rule ->strict ();
32+ $ this ->assertTrue ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
33+ $ this ->assertTrue ($ this ->rule ->fillParameters (['1 ' , '2 ' , '3 ' ])->check (1 ));
34+ }
35+
2436}
You can’t perform that action at this time.
0 commit comments