50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support\Audit;
|
|
|
|
final readonly class AuditTargetSnapshot
|
|
{
|
|
public function __construct(
|
|
public ?string $type = null,
|
|
public int|string|null $id = null,
|
|
public ?string $label = null,
|
|
) {}
|
|
|
|
public function labelOrFallback(): ?string
|
|
{
|
|
if (filled($this->label)) {
|
|
return (string) $this->label;
|
|
}
|
|
|
|
if (! filled($this->type) && ! filled($this->id)) {
|
|
return null;
|
|
}
|
|
|
|
$type = is_string($this->type) && trim($this->type) !== ''
|
|
? trim(str_replace(['_', '.'], ' ', $this->type))
|
|
: 'Record';
|
|
|
|
$type = ucfirst($type);
|
|
|
|
if (filled($this->id)) {
|
|
return sprintf('%s #%s', $type, (string) $this->id);
|
|
}
|
|
|
|
return $type;
|
|
}
|
|
|
|
/**
|
|
* @return array{target_type: ?string, target_id: int|string|null, target_label: ?string}
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'target_type' => $this->type,
|
|
'target_id' => $this->id,
|
|
'target_label' => $this->labelOrFallback(),
|
|
];
|
|
}
|
|
}
|