40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class IdentityProfile
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $fields
|
|
*/
|
|
public function fill(Model $subject, array $fields, ?User $actor = null): void
|
|
{
|
|
foreach ($fields as $field => $value) {
|
|
$next = is_string($value) ? trim($value) : $value;
|
|
$next = $next === '' ? null : $next;
|
|
$before = $subject->getAttribute($field);
|
|
$before = $before === '' ? null : $before;
|
|
|
|
if ($before === $next) {
|
|
continue;
|
|
}
|
|
|
|
$subject->setAttribute($field, $next);
|
|
IdentityChange::query()->create([
|
|
'subject_type' => $subject->getMorphClass(),
|
|
'subject_id' => $subject->getKey(),
|
|
'field' => $field,
|
|
'before' => $before,
|
|
'after' => $next,
|
|
'actor_id' => $actor?->id,
|
|
'occurred_at' => now(),
|
|
]);
|
|
}
|
|
|
|
$subject->save();
|
|
}
|
|
}
|