Skip to content

Commit 38e3325

Browse files
Rework
1 parent 2cda7cf commit 38e3325

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

src/Field/ChoiceField.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ final class ChoiceField implements FieldInterface
2323
public const OPTION_WIDGET = 'widget';
2424
public const OPTION_ESCAPE_HTML_CONTENTS = 'escapeHtml';
2525

26-
public const VALID_BADGE_TYPES = ['success', 'warning', 'danger', 'info', 'primary', 'secondary', 'light', 'dark'];
26+
/** @deprecated use BadgeStyle::VALID_BADGE_TYPES instead */
27+
public const VALID_BADGE_TYPES = BadgeStyle::VALID_BADGE_TYPES;
2728

2829
public const WIDGET_AUTOCOMPLETE = 'autocomplete';
2930
public const WIDGET_NATIVE = 'native';
@@ -126,11 +127,17 @@ public function renderAsBadges($badgeSelector = true): self
126127
}
127128

128129
if (\is_array($badgeSelector)) {
129-
foreach ($badgeSelector as $badgeType) {
130-
if (!$badgeType instanceof BadgeStyle && !\in_array($badgeType, self::VALID_BADGE_TYPES, true)) {
131-
throw new \InvalidArgumentException(sprintf('The values of the array passed to the "%s" method must be an instance of "%s" or one of the following valid badge types: "%s" ("%s" given).', __METHOD__, BadgeStyle::class, implode(', ', self::VALID_BADGE_TYPES), $badgeType));
130+
$badges = [];
131+
foreach ($badgeSelector as $key => $badge) {
132+
if ($badge instanceof BadgeStyle) {
133+
$badges[$key] = $badge;
134+
} elseif (\in_array($badge, BadgeStyle::VALID_BADGE_TYPES, true)) {
135+
$badges[$key] = BadgeStyle::new()->withType($badge);
136+
} else {
137+
throw new \InvalidArgumentException(sprintf('The values of the array passed to the "%s" method must be an instance of "%s" or one of the following valid badge types: "%s" ("%s" given).', __METHOD__, BadgeStyle::class, implode(', ', BadgeStyle::VALID_BADGE_TYPES), $badge));
132138
}
133139
}
140+
$badgeSelector = $badges;
134141
}
135142

136143
$this->setCustomOption(self::OPTION_RENDER_AS_BADGES, $badgeSelector);

src/Field/Configurator/ChoiceConfigurator.php

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,13 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
142142
);
143143
}
144144

145-
[$cssClass, $style] = $isRenderedAsBadge
146-
? $this->getBadgeCssClassAndStyle($badgeSelector, $selectedValue, $field)
147-
: [null, null];
145+
$badge = $isRenderedAsBadge ? $this->getBadgeStyle($badgeSelector, $selectedValue, $field) : null;
148146

149147
/** @var TranslatableMessage $choiceMessage */
150148
$choiceMessages[] = new TranslatableChoiceMessage(
151149
$choiceMessage,
152-
$cssClass,
153-
$style,
150+
$badge?->getClasses(),
151+
$badge?->getStyle(),
154152
);
155153
}
156154
}
@@ -176,39 +174,27 @@ private function getChoices(array|callable|null $choiceGenerator, EntityDto $ent
176174
}
177175

178176
/**
179-
* @param array<BadgeStyle|string>|bool|callable|null $badgeSelector
180-
*
181-
* @return array{string, string|null}
177+
* @param array<BadgeStyle>|bool|callable|null $badgeSelector
182178
*/
183-
private function getBadgeCssClassAndStyle(array|bool|callable|null $badgeSelector, mixed $value, FieldDto $field): array
179+
private function getBadgeStyle(array|bool|callable|null $badgeSelector, mixed $value, FieldDto $field): ?BadgeStyle
184180
{
185-
$cssClass = 'badge';
186-
$style = null;
187-
188-
$badgeType = '';
181+
$badge = null;
189182
if (true === $badgeSelector) {
190-
$badgeType = 'badge-secondary';
183+
$badge = BadgeStyle::new()->withType('secondary');
191184
} elseif (\is_array($badgeSelector)) {
192-
$badgeType = $badgeSelector[$value] ?? 'badge-secondary';
185+
$badge = $badgeSelector[$value] ?? BadgeStyle::new()->withType('secondary');
193186
} elseif (\is_callable($badgeSelector)) {
194-
$badgeType = $badgeSelector($value, $field);
195-
if (!$badgeType instanceof BadgeStyle && !\in_array($badgeType, ChoiceField::VALID_BADGE_TYPES, true)) {
196-
throw new \RuntimeException(sprintf('The value returned by the callable passed to the "renderAsBadges()" method must be an instance of "%s" or one of the following valid badge types: "%s" ("%s" given).', BadgeStyle::class, implode(', ', ChoiceField::VALID_BADGE_TYPES), $badgeType));
197-
}
198-
}
199-
200-
if ($badgeType instanceof BadgeStyle) {
201-
$style = $badgeType->getStyle();
202-
203-
$extraClasses = $badgeType->getClasses();
204-
if ('' !== $extraClasses) {
205-
$cssClass .= ' '.$extraClasses;
187+
$result = $badgeSelector($value, $field);
188+
if ($result instanceof BadgeStyle) {
189+
$badge = $result;
190+
} elseif (\in_array($result, BadgeStyle::VALID_BADGE_TYPES, true)) {
191+
$badge = BadgeStyle::new()->withType($result);
192+
} else {
193+
throw new \RuntimeException(sprintf('The value returned by the callable passed to the "renderAsBadges()" method must be an instance of "%s" or one of the following valid badge types: "%s" ("%s" given).', BadgeStyle::class, implode(', ', BadgeStyle::VALID_BADGE_TYPES), $result));
206194
}
207-
} elseif ('' !== $badgeType) {
208-
$cssClass .= ' '.u($badgeType)->ensureStart('badge-')->toString();
209195
}
210196

211-
return [$cssClass, $style];
197+
return $badge;
212198
}
213199

214200
/**

src/Field/Style/BadgeStyle.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
final class BadgeStyle
66
{
7+
public const VALID_BADGE_TYPES = ['success', 'warning', 'danger', 'info', 'primary', 'secondary', 'light', 'dark'];
8+
79
/**
810
* @param array<string> $classes
911
* @param array<string, string> $style
@@ -14,7 +16,7 @@ private function __construct(private array $classes, private array $style)
1416

1517
public static function new(): self
1618
{
17-
return new self([], []);
19+
return new self(['badge'], []);
1820
}
1921

2022
public function withBgColor(string $backgroundColor, bool $autoTextContrast = true): self
@@ -31,30 +33,55 @@ public function withTextColor(string $textColor): self
3133
return $this->addStyle('color', $textColor);
3234
}
3335

34-
public function addClass(string $class): self
36+
/**
37+
* @param value-of<self::VALID_BADGE_TYPES> $type
38+
*/
39+
public function withType(string $type): self
3540
{
36-
$this->classes[] = $class;
41+
if (!\in_array($type, self::VALID_BADGE_TYPES, true)) {
42+
throw new \InvalidArgumentException(sprintf('Invalid badge type "%s". Allowed types are: "%s".', $type, implode(', ', self::VALID_BADGE_TYPES)));
43+
}
3744

38-
return $this;
45+
return $this->addClass('badge-'.$type);
3946
}
4047

41-
public function addStyle(string $key, string $value): self
48+
public function asPill(): self
4249
{
43-
$this->style[$key] = $value;
44-
45-
return $this;
50+
return $this->addClass('badge-pill');
4651
}
4752

48-
public function getClasses(): string
53+
public function getClasses(): ?string
4954
{
55+
if ([] === $this->classes) {
56+
return null;
57+
}
58+
5059
return implode(' ', $this->classes);
5160
}
5261

53-
public function getStyle(): string
62+
public function getStyle(): ?string
5463
{
64+
if ([] === $this->style) {
65+
return null;
66+
}
67+
5568
return self::generateStyle($this->style);
5669
}
5770

71+
private function addClass(string $class): self
72+
{
73+
$this->classes[] = $class;
74+
75+
return $this;
76+
}
77+
78+
private function addStyle(string $key, string $value): self
79+
{
80+
$this->style[$key] = $value;
81+
82+
return $this;
83+
}
84+
5885
/**
5986
* @param array<string, string> $properties
6087
*/
@@ -76,7 +103,7 @@ private static function isSupportedColor(string $color): bool
76103
private static function generateTextClassFromBackgroundColor(string $backgroundColor): string
77104
{
78105
if (!self::isSupportedColor($backgroundColor)) {
79-
throw new \InvalidArgumentException(sprintf('The background color must be a full 6-digit hexadecimal color ("%s" given).', $backgroundColor));
106+
throw new \InvalidArgumentException(sprintf('Only full 6-digit hexadecimal color are supported to generate the appropriate text color ("%s" given).', $backgroundColor));
80107
}
81108

82109
[$r, $g, $b] = [

0 commit comments

Comments
 (0)