Only allow either colour or object, add outline parameter

This commit is contained in:
2024-08-08 16:45:07 +01:00
parent fd9c946c94
commit 977a7d65a6

View File

@@ -21,11 +21,23 @@ final class Badge
/** /**
* @param BadgeableInterface $object The object to be converted into a badge * @param BadgeableInterface $object The object to be converted into a badge
* @param string $class Extra classes you want to add to the badge template * @param string $class Extra classes you want to add to the badge template
* @param ?string $colour Specify the colour for an objectless badge
*/ */
public function mount(?BadgeableInterface $object = null, string $class = '', string $colour = null): void public function mount(?BadgeableInterface $object = null, string $class = '', string $colour = null, bool $outline = false): void
{ {
if (null !== $colour) { if (!$object && !$colour) {
throw new \RuntimeException(sprintf("You must specify either a colour for the badge or an instance of %s.", BadgeableInterface::class));
}
if ($object && $colour) {
throw new \RuntimeException(sprintf("You have specified both the colour \"%s\" and an instance of \"%s\", please choose one or the other.", $colour, $object::class));
}
if ($object) {
$palette = $object->getBadgeColour()->getPalette();
}
if ($colour) {
$cases = array_map(fn(EnumBadge $b) => strtolower($b->name), EnumBadge::cases()); $cases = array_map(fn(EnumBadge $b) => strtolower($b->name), EnumBadge::cases());
if (!in_array($colour, $cases)) { if (!in_array($colour, $cases)) {
@@ -35,19 +47,17 @@ final class Badge
$colour = strtoupper($colour); $colour = strtoupper($colour);
$palette = EnumBadge::{$colour}->getPalette(); $palette = EnumBadge::{$colour}->getPalette();
} else {
if (null === $object) {
throw new \RuntimeException(sprintf("You must specify a colour for the badge if no instance of %s is provided.", BadgeableInterface::class));
}
$palette = $object->getBadgeColour()->getPalette();
} }
$merger = TailwindMerge::instance(); $merger = TailwindMerge::instance();
$this->finalClasses = $merger->merge(sprintf('text-white %s %s %s %s', $palette->borderColourClass, $palette->backgroundColourClass, $this->baseClasses, $class)); if (true === $outline) {
$classes = sprintf('text-white %s %s %s %s', $palette->borderColourClass, $palette->backgroundColourClass, $this->baseClasses, $class);
} else {
$classes = sprintf('bg-white %s %s %s %s', $palette->borderColourClass, $palette->textColourClass, $this->baseClasses, $class);
}
$this->finalClasses = $merger->merge($classes);
} }
} }