TenantAtlas/app/Support/Diff/DiffRow.php
ahmido 0b5cadc234 feat: add shared diff presentation foundation (#170)
## Summary
- add a shared diff presentation layer under `app/Support/Diff` with deterministic row classification, summary derivation, and value stringification
- centralize diff-state badge semantics through `BadgeCatalog` with a dedicated `DiffRowStatusBadge`
- add reusable Filament diff partials, focused Pest coverage, and the full SpecKit artifact set for spec 141

## Testing
- `vendor/bin/sail artisan test --compact tests/Unit/Support/Diff/DiffRowStatusTest.php tests/Unit/Support/Diff/DiffRowTest.php tests/Unit/Support/Diff/DiffPresenterTest.php tests/Unit/Support/Diff/ValueStringifierTest.php tests/Unit/Badges/DiffRowStatusBadgeTest.php tests/Feature/Support/Diff/SharedDiffSummaryPartialTest.php tests/Feature/Support/Diff/SharedDiffRowPartialTest.php tests/Feature/Support/Diff/SharedInlineListDiffPartialTest.php`
- `vendor/bin/sail bin pint --dirty --format agent`

## Filament / Livewire Contract
- Livewire v4.0+ compliance: unchanged and respected; this feature adds presentation support only within the existing Filament v5 / Livewire v4 stack
- Provider registration: unchanged; no panel/provider changes were required, so `bootstrap/providers.php` remains the correct registration location
- Global search: unchanged; no Resource or global-search behavior was added or modified
- Destructive actions: none introduced in this feature
- Asset strategy: no new registered Filament assets; shared Blade partials rely on the existing asset pipeline and standard deploy step for `php artisan filament:assets` when assets change generally
- Testing coverage: presenter, DTOs, stringifier, badge semantics, summary partial, row partial, and inline-list partial are covered by focused Pest unit and feature tests

## Notes
- Spec checklist status is complete for `specs/141-shared-diff-presentation-foundation/checklists/requirements.md`
- This PR preserves specialized diff renderers and documents incremental adoption rather than forcing migration in the same change

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #170
2026-03-14 12:32:08 +00:00

133 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\Diff;
use BackedEnum;
use InvalidArgumentException;
use Stringable;
final readonly class DiffRow
{
public string $key;
public string $label;
public DiffRowStatus $status;
public mixed $oldValue;
public mixed $newValue;
public bool $isListLike;
/**
* @var array<int, mixed>
*/
public array $addedItems;
/**
* @var array<int, mixed>
*/
public array $removedItems;
/**
* @var array<int, mixed>
*/
public array $unchangedItems;
/**
* @var array<string, mixed>
*/
public array $meta;
/**
* @param array<int, mixed> $addedItems
* @param array<int, mixed> $removedItems
* @param array<int, mixed> $unchangedItems
* @param array<string, mixed> $meta
*/
public function __construct(
string $key,
string $label,
DiffRowStatus $status,
mixed $oldValue = null,
mixed $newValue = null,
bool $isListLike = false,
array $addedItems = [],
array $removedItems = [],
array $unchangedItems = [],
array $meta = [],
) {
if (trim($key) === '') {
throw new InvalidArgumentException('DiffRow key must be a non-empty string.');
}
if (trim($label) === '') {
throw new InvalidArgumentException('DiffRow label must be a non-empty string.');
}
$this->key = $key;
$this->label = $label;
$this->status = $status;
$this->oldValue = $oldValue;
$this->newValue = $newValue;
$this->isListLike = $isListLike;
$this->addedItems = array_values($addedItems);
$this->removedItems = array_values($removedItems);
$this->unchangedItems = array_values($unchangedItems);
$this->meta = $this->normalizeMeta($meta);
}
/**
* @param array<string, mixed> $meta
* @return array<string, mixed>
*/
private function normalizeMeta(array $meta): array
{
$normalized = [];
foreach ($meta as $key => $value) {
if (! is_string($key) || trim($key) === '') {
continue;
}
$normalized[$key] = $this->normalizeMetaValue($value);
}
return $normalized;
}
private function normalizeMetaValue(mixed $value): mixed
{
if ($value === null || is_scalar($value)) {
return $value;
}
if ($value instanceof BackedEnum) {
return $value->value;
}
if ($value instanceof Stringable) {
return (string) $value;
}
if (! is_array($value)) {
return null;
}
$normalized = [];
foreach ($value as $key => $item) {
if (! is_int($key) && ! is_string($key)) {
continue;
}
$normalized[$key] = $this->normalizeMetaValue($item);
}
return array_is_list($normalized) ? array_values($normalized) : $normalized;
}
}