TenantAtlas/app/Support/Badges/BadgeSpec.php
2026-01-23 00:40:52 +01:00

62 lines
1.6 KiB
PHP

<?php
namespace App\Support\Badges;
use InvalidArgumentException;
final class BadgeSpec
{
/**
* @var array<int, string>
*/
private const ALLOWED_COLORS = [
'gray',
'info',
'success',
'warning',
'danger',
'primary',
];
public function __construct(
public readonly string $label,
public readonly string $color,
public readonly ?string $icon = null,
public readonly ?string $iconColor = null,
) {
if (trim($this->label) === '') {
throw new InvalidArgumentException('BadgeSpec label must be a non-empty string.');
}
if (! in_array($this->color, self::ALLOWED_COLORS, true)) {
throw new InvalidArgumentException('BadgeSpec color must be one of: '.implode(', ', self::ALLOWED_COLORS));
}
if ($this->icon !== null && trim($this->icon) === '') {
throw new InvalidArgumentException('BadgeSpec icon must be null or a non-empty string.');
}
if ($this->iconColor !== null && ! in_array($this->iconColor, self::ALLOWED_COLORS, true)) {
throw new InvalidArgumentException('BadgeSpec iconColor must be null or one of: '.implode(', ', self::ALLOWED_COLORS));
}
}
/**
* @return array<int, string>
*/
public static function allowedColors(): array
{
return self::ALLOWED_COLORS;
}
public static function unknown(): self
{
return new self(
label: 'Unknown',
color: 'gray',
icon: 'heroicon-m-question-mark-circle',
iconColor: 'gray',
);
}
}