File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -11,9 +11,16 @@ class Hex implements Rule
11
11
*/
12
12
protected $ forceFull ;
13
13
14
- public function __construct ($ forceFull = false )
14
+ /**
15
+ * @var bool
16
+ */
17
+ protected $ allowAlpha ;
18
+
19
+ public function __construct ($ forceFull = false , $ allowAlpha = false )
15
20
{
16
21
$ this ->forceFull = $ forceFull ;
22
+
23
+ $ this ->allowAlpha = $ allowAlpha ;
17
24
}
18
25
19
26
/**
@@ -32,6 +39,14 @@ public function passes($attribute, $value)
32
39
$ pattern .= '|[a-fA-F0-9]{3} ' ;
33
40
}
34
41
42
+ if ($ this ->allowAlpha ) {
43
+ $ pattern .= '|[a-fA-F0-9]{8} ' ;
44
+
45
+ if (!$ this ->forceFull ) {
46
+ $ pattern .= '|[a-fA-F0-9]{4} ' ;
47
+ }
48
+ }
49
+
35
50
$ pattern .= ')$/ ' ;
36
51
37
52
return (bool ) preg_match ($ pattern , $ value );
Original file line number Diff line number Diff line change @@ -37,4 +37,20 @@ public function three_characters_with_hash()
37
37
38
38
$ this ->assertTrue ($ this ->validator ('#fff ' , false )->passes ()); // full
39
39
}
40
+
41
+ public function eight_characters_with_hash ()
42
+ {
43
+ $ this ->assertTrue ($ this ->validator ('#ff008000 ' )->fails ());
44
+ $ this ->assertTrue ($ this ->validator ('#fg00800g ' , true , true )->fails ());
45
+ $ this ->assertTrue ($ this ->validator ('#ff008000 ' , true , true )->passes ());
46
+ }
47
+
48
+ /** @test */
49
+ public function four_characters_with_hash ()
50
+ {
51
+ $ this ->assertTrue ($ this ->validator ('#ffff ' , true , true )->passes ());
52
+ $ this ->assertTrue ($ this ->validator ('#ffff ' , true , false )->fails ());
53
+ $ this ->assertTrue ($ this ->validator ('#gggg ' , true , true )->fails ());
54
+ $ this ->assertTrue ($ this ->validator ('#ffff ' , false , true )->passes ());
55
+ }
40
56
}
You can’t perform that action at this time.
0 commit comments