File tree Expand file tree Collapse file tree 5 files changed +220
-0
lines changed
src/Illuminate/Validation Expand file tree Collapse file tree 5 files changed +220
-0
lines changed Original file line number Diff line number Diff line change 5
5
use Illuminate \Contracts \Support \Arrayable ;
6
6
use Illuminate \Support \Traits \Macroable ;
7
7
use Illuminate \Validation \Rules \Dimensions ;
8
+ use Illuminate \Validation \Rules \ExcludeIf ;
8
9
use Illuminate \Validation \Rules \Exists ;
9
10
use Illuminate \Validation \Rules \In ;
10
11
use Illuminate \Validation \Rules \NotIn ;
12
+ use Illuminate \Validation \Rules \ProhibitedIf ;
11
13
use Illuminate \Validation \Rules \RequiredIf ;
12
14
use Illuminate \Validation \Rules \Unique ;
13
15
@@ -103,6 +105,28 @@ public static function requiredIf($callback)
103
105
return new RequiredIf ($ callback );
104
106
}
105
107
108
+ /**
109
+ * Get a exclude_if constraint builder instance.
110
+ *
111
+ * @param callable|bool $callback
112
+ * @return \Illuminate\Validation\Rules\ProhibitedIf
113
+ */
114
+ public static function excludeIf ($ callback )
115
+ {
116
+ return new ExcludeIf ($ callback );
117
+ }
118
+
119
+ /**
120
+ * Get a prohibited_if constraint builder instance.
121
+ *
122
+ * @param callable|bool $callback
123
+ * @return \Illuminate\Validation\Rules\ProhibitedIf
124
+ */
125
+ public static function prohibitedIf ($ callback )
126
+ {
127
+ return new ProhibitedIf ($ callback );
128
+ }
129
+
106
130
/**
107
131
* Get a unique constraint builder instance.
108
132
*
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Validation \Rules ;
4
+
5
+ use InvalidArgumentException ;
6
+
7
+ class ExcludeIf
8
+ {
9
+ /**
10
+ * The condition that validates the attribute.
11
+ *
12
+ * @var callable|bool
13
+ */
14
+ public $ condition ;
15
+
16
+ /**
17
+ * Create a new exclude validation rule based on a condition.
18
+ *
19
+ * @param callable|bool $condition
20
+ * @return void
21
+ */
22
+ public function __construct ($ condition )
23
+ {
24
+ if (! is_string ($ condition )) {
25
+ $ this ->condition = $ condition ;
26
+ } else {
27
+ throw new InvalidArgumentException ('The provided condition must be a callable or boolean. ' );
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Convert the rule to a validation string.
33
+ *
34
+ * @return string
35
+ */
36
+ public function __toString ()
37
+ {
38
+ if (is_callable ($ this ->condition )) {
39
+ return call_user_func ($ this ->condition ) ? 'exclude ' : '' ;
40
+ }
41
+
42
+ return $ this ->condition ? 'exclude ' : '' ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Validation \Rules ;
4
+
5
+ use InvalidArgumentException ;
6
+
7
+ class ProhibitedIf
8
+ {
9
+ /**
10
+ * The condition that validates the attribute.
11
+ *
12
+ * @var callable|bool
13
+ */
14
+ public $ condition ;
15
+
16
+ /**
17
+ * Create a new prohibited validation rule based on a condition.
18
+ *
19
+ * @param callable|bool $condition
20
+ * @return void
21
+ */
22
+ public function __construct ($ condition )
23
+ {
24
+ if (! is_string ($ condition )) {
25
+ $ this ->condition = $ condition ;
26
+ } else {
27
+ throw new InvalidArgumentException ('The provided condition must be a callable or boolean. ' );
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Convert the rule to a validation string.
33
+ *
34
+ * @return string
35
+ */
36
+ public function __toString ()
37
+ {
38
+ if (is_callable ($ this ->condition )) {
39
+ return call_user_func ($ this ->condition ) ? 'prohibited ' : '' ;
40
+ }
41
+
42
+ return $ this ->condition ? 'prohibited ' : '' ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Tests \Validation ;
4
+
5
+ use Exception ;
6
+ use Illuminate \Validation \Rules \ExcludeIf ;
7
+ use InvalidArgumentException ;
8
+ use PHPUnit \Framework \TestCase ;
9
+
10
+ class ValidationExcludeIfTest extends TestCase
11
+ {
12
+ public function testItClousureReturnsFormatsAStringVersionOfTheRule ()
13
+ {
14
+ $ rule = new ExcludeIf (function () {
15
+ return true ;
16
+ });
17
+
18
+ $ this ->assertSame ('exclude ' , (string ) $ rule );
19
+
20
+ $ rule = new ExcludeIf (function () {
21
+ return false ;
22
+ });
23
+
24
+ $ this ->assertSame ('' , (string ) $ rule );
25
+
26
+ $ rule = new ExcludeIf (true );
27
+
28
+ $ this ->assertSame ('exclude ' , (string ) $ rule );
29
+
30
+ $ rule = new ExcludeIf (false );
31
+
32
+ $ this ->assertSame ('' , (string ) $ rule );
33
+ }
34
+
35
+ public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule ()
36
+ {
37
+ $ rule = new ExcludeIf (false );
38
+
39
+ $ rule = new ExcludeIf (true );
40
+
41
+ $ this ->expectException (InvalidArgumentException::class);
42
+
43
+ $ rule = new ExcludeIf ('phpinfo ' );
44
+ }
45
+
46
+ public function testItReturnedRuleIsNotSerializable ()
47
+ {
48
+ $ this ->expectException (Exception::class);
49
+
50
+ $ rule = serialize (new ExcludeIf (function () {
51
+ return true ;
52
+ }));
53
+ }
54
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Tests \Validation ;
4
+
5
+ use Exception ;
6
+ use Illuminate \Validation \Rules \ProhibitedIf ;
7
+ use InvalidArgumentException ;
8
+ use PHPUnit \Framework \TestCase ;
9
+
10
+ class ValidationProhibitedIfTest extends TestCase
11
+ {
12
+ public function testItClousureReturnsFormatsAStringVersionOfTheRule ()
13
+ {
14
+ $ rule = new ProhibitedIf (function () {
15
+ return true ;
16
+ });
17
+
18
+ $ this ->assertSame ('prohibited ' , (string ) $ rule );
19
+
20
+ $ rule = new ProhibitedIf (function () {
21
+ return false ;
22
+ });
23
+
24
+ $ this ->assertSame ('' , (string ) $ rule );
25
+
26
+ $ rule = new ProhibitedIf (true );
27
+
28
+ $ this ->assertSame ('prohibited ' , (string ) $ rule );
29
+
30
+ $ rule = new ProhibitedIf (false );
31
+
32
+ $ this ->assertSame ('' , (string ) $ rule );
33
+ }
34
+
35
+ public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule ()
36
+ {
37
+ $ rule = new ProhibitedIf (false );
38
+
39
+ $ rule = new ProhibitedIf (true );
40
+
41
+ $ this ->expectException (InvalidArgumentException::class);
42
+
43
+ $ rule = new ProhibitedIf ('phpinfo ' );
44
+ }
45
+
46
+ public function testItReturnedRuleIsNotSerializable ()
47
+ {
48
+ $ this ->expectException (Exception::class);
49
+
50
+ $ rule = serialize (new ProhibitedIf (function () {
51
+ return true ;
52
+ }));
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments