Skip to content

Commit 16b507d

Browse files
authored
Add option to validate transparent color codes
1 parent 4c5f337 commit 16b507d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Hex.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ class Hex implements Rule
1111
*/
1212
protected $forceFull;
1313

14-
public function __construct($forceFull = false)
14+
/**
15+
* @var bool
16+
*/
17+
protected $allowAlpha;
18+
19+
public function __construct($forceFull = false, $allowAlpha = false)
1520
{
1621
$this->forceFull = $forceFull;
22+
23+
$this->allowAlpha = $allowAlpha;
1724
}
1825

1926
/**
@@ -27,11 +34,20 @@ public function __construct($forceFull = false)
2734
public function passes($attribute, $value)
2835
{
2936
$pattern = '/^#([a-fA-F0-9]{6}';
30-
37+
3138
if (!$this->forceFull) {
3239
$pattern .= '|[a-fA-F0-9]{3}';
3340
}
3441

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+
50+
3551
$pattern .= ')$/';
3652

3753
return (bool) preg_match($pattern, $value);

0 commit comments

Comments
 (0)