TenantAtlas/app/Support/Badges/BadgeSpec.php
ahmido 3c3daae405 feat: normalize operator outcome taxonomy (#186)
## Summary
- introduce a shared operator outcome taxonomy with semantic axes, severity bands, and next-action policy
- apply the taxonomy to operations, evidence/review completeness, baseline semantics, and restore semantics
- harden badge rendering, tenant-safe filtering/search behavior, and operator-facing summary/notification wording
- add the spec kit artifacts, reference documentation, and regression coverage for diagnostic-vs-primary state handling

## Testing
- focused Pest coverage for taxonomy registry and badge guardrails
- operations presentation and notification tests
- evidence, baseline, restore, and tenant-scope regression tests

## Notes
- Livewire v4.0+ compliance is preserved in the existing Filament v5 stack
- panel provider registration remains unchanged in bootstrap/providers.php
- no new globally searchable resource was added; adopted resources remain tenant-safe and out of global search where required
- no new destructive action family was introduced; existing actions keep their current authorization and confirmation behavior
- no new frontend asset strategy was introduced; existing deploy flow with filament:assets remains unchanged

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #186
2026-03-22 12:13:34 +00:00

106 lines
3.9 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,
public readonly ?OperatorSemanticAxis $semanticAxis = null,
public readonly ?OperatorStateClassification $classification = null,
public readonly ?OperatorNextActionPolicy $nextActionPolicy = null,
public readonly ?string $diagnosticLabel = null,
/**
* @var list<string>
*/
public readonly array $legacyAliases = [],
public readonly ?string $notes = 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));
}
$hasTaxonomyMetadata = $this->semanticAxis !== null
|| $this->classification !== null
|| $this->nextActionPolicy !== null
|| $this->diagnosticLabel !== null
|| $this->legacyAliases !== []
|| $this->notes !== null;
if ($hasTaxonomyMetadata && ($this->semanticAxis === null || $this->classification === null || $this->nextActionPolicy === null)) {
throw new InvalidArgumentException('BadgeSpec taxonomy metadata requires semanticAxis, classification, and nextActionPolicy together.');
}
if ($this->diagnosticLabel !== null && trim($this->diagnosticLabel) === '') {
throw new InvalidArgumentException('BadgeSpec diagnosticLabel must be null or a non-empty string.');
}
foreach ($this->legacyAliases as $legacyAlias) {
if (! is_string($legacyAlias) || trim($legacyAlias) === '') {
throw new InvalidArgumentException('BadgeSpec legacyAliases must contain only non-empty strings.');
}
}
if ($this->notes !== null && trim($this->notes) === '') {
throw new InvalidArgumentException('BadgeSpec notes must be null or a non-empty string.');
}
if ($this->classification === OperatorStateClassification::Diagnostic && in_array($this->color, ['warning', 'danger'], true)) {
throw new InvalidArgumentException('Diagnostic badge specs cannot use warning or danger colors.');
}
if ($this->classification === OperatorStateClassification::Primary
&& in_array($this->color, ['warning', 'danger'], true)
&& $this->nextActionPolicy === OperatorNextActionPolicy::None) {
throw new InvalidArgumentException('Primary warning or danger badge specs must declare an operator next-action policy.');
}
}
/**
* @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',
);
}
}