Skip to content

Commit 2d629c9

Browse files
Rework
1 parent 423ef1f commit 2d629c9

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

src/Field/Configurator/ChoiceConfigurator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ private function getBadgeCssClassAndStyle(array|bool|callable|null $badgeSelecto
198198
}
199199

200200
if ($badgeType instanceof BadgeStyle) {
201-
$style = $badgeType->toStyle();
201+
$style = $badgeType->getStyle();
202+
203+
$extraClasses = $badgeType->getClasses();
204+
if ('' !== $extraClasses) {
205+
$cssClass .= ' '.$extraClasses;
206+
}
202207
} elseif ('' !== $badgeType) {
203208
$cssClass .= ' '.u($badgeType)->ensureStart('badge-')->toString();
204209
}

src/Field/Style/BadgeStyle.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,68 @@
44

55
final class BadgeStyle
66
{
7-
private ?string $textColor = null;
8-
9-
public function __construct(
10-
private string $backgroundColor,
11-
?string $textColor = null
12-
) {
13-
if (!$this->isSupportedColor($this->backgroundColor)) {
14-
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $this->backgroundColor));
7+
private function __construct(private string $classes, private string $style)
8+
{
9+
}
10+
11+
public static function fromColor(string $backgroundColor, ?string $textColor = null): self
12+
{
13+
if (!self::isSupportedColor($backgroundColor)) {
14+
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor));
1515
}
1616

17+
$classes = [];
18+
$styleProperties = ['background-color' => $backgroundColor];
19+
1720
if (null === $textColor) {
18-
$this->textColor = $this->computeTextColor($this->backgroundColor);
19-
} elseif (!$this->isSupportedColor($this->textColor)) {
20-
throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $this->textColor));
21+
$classes[] = self::computeTextClass($backgroundColor);
22+
} elseif (self::isSupportedColor($textColor)) {
23+
$styleProperties['color'] = $textColor;
24+
} else {
25+
throw new \InvalidArgumentException(sprintf('The text color must be a full 6-digit hexadecimal color ("%s" given).', $textColor));
2126
}
27+
28+
return new self(implode(' ', $classes), self::generateStyle($styleProperties));
29+
}
30+
31+
public function getClasses(): string
32+
{
33+
return $this->classes;
2234
}
2335

24-
public function toStyle(): string
36+
public function getStyle(): string
2537
{
26-
return sprintf('background-color:%s; color:%s;', $this->backgroundColor, $this->textColor);
38+
return $this->style;
39+
}
40+
41+
/**
42+
* @param array<string, string> $properties
43+
*/
44+
private static function generateStyle(array $properties): string
45+
{
46+
$style = [];
47+
foreach ($properties as $key => $value) {
48+
$style[] = sprintf('%s:%s;', $key, $value);
49+
}
50+
51+
return implode(' ', $style);
2752
}
2853

29-
private function isSupportedColor(string $color): bool
54+
private static function isSupportedColor(string $color): bool
3055
{
3156
return 1 === preg_match('/^#[0-9a-f]{6}$/iD', $color);
3257
}
3358

34-
private function computeTextColor(string $bgColor): string
59+
private static function computeTextClass(string $backgroundColor): string
3560
{
3661
[$r, $g, $b] = [
37-
hexdec(substr($bgColor, 1, 2)),
38-
hexdec(substr($bgColor, 3, 2)),
39-
hexdec(substr($bgColor, 5, 2)),
62+
hexdec(substr($backgroundColor, 1, 2)),
63+
hexdec(substr($backgroundColor, 3, 2)),
64+
hexdec(substr($backgroundColor, 5, 2)),
4065
];
4166

4267
$luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
4368

44-
return $luminance > 0.5 ? '#000000' : '#FFFFFF';
69+
return $luminance > 0.5 ? 'text-dark' : 'text-light';
4570
}
4671
}

tests/Field/ChoiceFieldTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ public function testBadges()
173173
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? 'success' : 'primary'; });
174174
self::assertSame('<span class="badge badge-primary">a</span><span class="badge badge-success">c</span>', (string) $this->configure($field)->getFormattedValue());
175175

176-
$field->setValue(1)->renderAsBadges([1 => new BadgeStyle('#123456'), '3' => new BadgeStyle('#AAAAAA')]);
177-
self::assertSame('<span class="badge" style="background-color:#123456; color:#FFFFFF;">a</span>', (string) $this->configure($field)->getFormattedValue());
176+
$field->setValue(1)->renderAsBadges([1 => BadgeStyle::fromColor('#123456'), '3' => BadgeStyle::fromColor('#AAAAAA')]);
177+
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span>', (string) $this->configure($field)->getFormattedValue());
178178

179-
$field->setValue([1, 3])->renderAsBadges([1 => new BadgeStyle('#123456'), '3' => new BadgeStyle('#AAAAAA')]);
180-
self::assertSame('<span class="badge" style="background-color:#123456; color:#FFFFFF;">a</span><span class="badge" style="background-color:#AAAAAA; color:#000000;">c</span>', (string) $this->configure($field)->getFormattedValue());
179+
$field->setValue([1, 3])->renderAsBadges([1 => BadgeStyle::fromColor('#123456'), '3' => BadgeStyle::fromColor('#AAAAAA')]);
180+
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span><span class="badge text-dark" style="background-color:#AAAAAA;">c</span>', (string) $this->configure($field)->getFormattedValue());
181181

182-
$field->setValue(1)->renderAsBadges(function ($value) { return $value > 1 ? new BadgeStyle('#AAAAAA') : new BadgeStyle('#123456'); });
183-
self::assertSame('<span class="badge" style="background-color:#123456; color:#FFFFFF;">a</span>', (string) $this->configure($field)->getFormattedValue());
182+
$field->setValue(1)->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::fromColor('#AAAAAA') : BadgeStyle::fromColor('#123456'); });
183+
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span>', (string) $this->configure($field)->getFormattedValue());
184184

185-
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? new BadgeStyle('#AAAAAA') : new BadgeStyle('#123456'); });
186-
self::assertSame('<span class="badge" style="background-color:#123456; color:#FFFFFF;">a</span><span class="badge" style="background-color:#AAAAAA; color:#000000;">c</span>', (string) $this->configure($field)->getFormattedValue());
185+
$field->setValue([1, 3])->renderAsBadges(function ($value) { return $value > 1 ? BadgeStyle::fromColor('#AAAAAA') : BadgeStyle::fromColor('#123456'); });
186+
self::assertSame('<span class="badge text-light" style="background-color:#123456;">a</span><span class="badge text-dark" style="background-color:#AAAAAA;">c</span>', (string) $this->configure($field)->getFormattedValue());
187187
}
188188
}

0 commit comments

Comments
 (0)