Created
September 18, 2023 09:43
-
-
Save addeeandra/d5f5fd0b09991247b565912e0e33613a to your computer and use it in GitHub Desktop.
Using OwenIt\Auditing, this trait extends the functionality to verify and undo, also to get unverified fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Concerns\Audit; | |
use JetBrains\PhpStorm\ArrayShape; | |
/** | |
* @method \Illuminate\Database\Eloquent\Relations\MorphMany<OwenIt\Auditing\Contracts\Audit> audits() | |
* | |
* @method save() | |
* @method fill(array $attributes) | |
*/ | |
trait VerifiableTrait | |
{ | |
protected function getVerificationColumnProperty(): string | |
{ | |
return $this->verificationColumn ?? 'is_verified'; | |
} | |
/** | |
* Take fields that applied when Model being verified. | |
* | |
* @return array | |
*/ | |
#[ArrayShape(['new_values' => "array", 'old_values' => "array"])] | |
public function unverifiedFields(): array | |
{ | |
$auditRecords = $this->audits()->where('event', 'updated')->latest()->cursor(); | |
$newChanges = []; | |
$oldChanges = []; | |
## Get audits since last verified | |
foreach ($auditRecords as $record) { ## loop from latest to find verificationColumn | |
## When found verification column is true, stop. | |
if ( | |
isset($record->new_values[$this->getVerificationColumnProperty()]) && | |
$record->new_values[$this->getVerificationColumnProperty()] == true | |
) { | |
## break, so the records[] will contain unverified fields only. | |
break; | |
} | |
$newChanges[] = $record->new_values; | |
$oldChanges[] = $record->old_values; | |
} | |
$newChanges = array_reverse($newChanges); | |
$verifiableFields = []; | |
foreach ($newChanges as $record) { | |
## merge all fields that need being verification | |
## late fields should be shown instead of all fields. | |
$verifiableFields = array_merge($verifiableFields, $record); | |
} | |
$oldChanges = array_reverse($oldChanges); | |
$originalFields = []; | |
foreach ($oldChanges as $record) { | |
$originalFields = array_merge($originalFields, $record); | |
} | |
foreach ($this->hidden as $hiddenField) { | |
if (isset($verifiableFields[$hiddenField])) unset($verifiableFields[$hiddenField]); | |
if (isset($originalFields[$hiddenField])) unset($originalFields[$hiddenField]); | |
} | |
return [ | |
'new_values' => $verifiableFields, | |
'old_values' => $originalFields | |
]; | |
} | |
public function verifyChanges(array $changes = []) | |
{ | |
$changedFields = $this->unverifiedFields(); | |
$this->fill(array_merge( | |
$changedFields['new_values'], | |
[$this->getVerificationColumnProperty() => true] | |
)); | |
if (!empty($changes)) { | |
$this->fill($changes); | |
} | |
return $this->save(); | |
} | |
public function undoChanges(array $changes = []): bool | |
{ | |
$changedFields = $this->unverifiedFields(); | |
$this->fill(array_merge( | |
$changedFields['old_values'], | |
[$this->getVerificationColumnProperty() => true] | |
)); | |
if (!empty($changes)) { | |
$this->fill($changes); | |
} | |
return $this->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment