Skip to content

Commit fc8e1ea

Browse files
Simplify
1 parent 0312237 commit fc8e1ea

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/Field/Style/BadgeStyle.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ public static function new(): self
1919

2020
public function withBgColor(string $backgroundColor, bool $autoTextContrast = true): self
2121
{
22-
if (!self::isSupportedColor($backgroundColor)) {
23-
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor));
24-
}
25-
26-
$this->style['background-color'] = $backgroundColor;
2722
if ($autoTextContrast) {
28-
$this->classes[] = self::generateTextClassFromBackgroundColor($backgroundColor);
23+
$this->addClass(self::generateTextClassFromBackgroundColor($backgroundColor));
2924
}
3025

31-
return $this;
26+
return $this->addStyle('background-color', $backgroundColor);
3227
}
3328

3429
public function withTextColor(string $textColor): self
3530
{
36-
if (!self::isSupportedColor($textColor)) {
37-
throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $textColor));
38-
}
31+
return $this->addStyle('color', $textColor);
32+
}
3933

40-
$this->style['color'] = $textColor;
34+
public function addClass(string $class): self
35+
{
36+
$this->classes[] = $class;
37+
38+
return $this;
39+
}
40+
41+
public function addStyle(string $key, string $value): self
42+
{
43+
$this->style[$key] = $value;
4144

4245
return $this;
4346
}
@@ -72,6 +75,10 @@ private static function isSupportedColor(string $color): bool
7275

7376
private static function generateTextClassFromBackgroundColor(string $backgroundColor): string
7477
{
78+
if (!self::isSupportedColor($backgroundColor)) {
79+
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor));
80+
}
81+
7582
[$r, $g, $b] = [
7683
hexdec(substr($backgroundColor, 1, 2)),
7784
hexdec(substr($backgroundColor, 3, 2)),

0 commit comments

Comments
 (0)