Skip to content

Commit 581a282

Browse files
Rework
1 parent 423ef1f commit 581a282

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

doc/fields/ChoiceField.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ The built-in badge styles are the same as Bootstrap: ``'success'``,
9494

9595
yield ChoiceField::new('...')->renderAsBadges([
9696
// $value => $badgeStyleName
97-
'paid' => new BadgeStyle('#00FF00'),
98-
'pending' => new BadgeStyle('#FFFF00'),
99-
'refunded' => new BadgeStyle('#FF0000'),
97+
'paid' => BadgeStyle::fromBgColor('#00FF00'),
98+
'pending' => BadgeStyle::fromBgColor('#FFFF00'),
99+
'refunded' => BadgeStyle::fromBgColor('#FF0000'),
100100
]);
101101

102102
renderAsNativeWidget

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 fromBgColor(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::fromBgColor('#123456'), '3' => BadgeStyle::fromBgColor('#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::fromBgColor('#123456'), '3' => BadgeStyle::fromBgColor('#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::fromBgColor('#AAAAAA') : BadgeStyle::fromBgColor('#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::fromBgColor('#AAAAAA') : BadgeStyle::fromBgColor('#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)