Skip to content

Commit bb14958

Browse files
Merge pull request #107 from kitbs/patch-1
[3.0] Audited values will respect $hidden/$visible
2 parents 094fabe + 8cfda04 commit bb14958

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/Auditable.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public function toAudit()
199199
// Auditable data
200200
return [
201201
'id' => (string) Uuid::uuid4(),
202-
'old' => $this->oldData,
203-
'new' => $this->newData,
202+
'old' => $this->cleanHiddenAuditAttributes($this->oldData),
203+
'new' => $this->cleanHiddenAuditAttributes($this->newData),
204204
'type' => $this->typeAuditing,
205205
'auditable_id' => $this->getKey(),
206206
'auditable_type' => $this->getMorphClass(),
@@ -342,4 +342,53 @@ public function isTypeAuditable($key)
342342

343343
return false;
344344
}
345+
346+
/**
347+
* Whether to clean the attributes which are hidden or not visible.
348+
*
349+
* @return bool
350+
*/
351+
public function isAuditRespectsHidden()
352+
{
353+
return isset($this->auditRespectsHidden) && $this->auditRespectsHidden;
354+
}
355+
356+
/**
357+
* Remove the value of attributes which are hidden or not visible on the model.
358+
*
359+
* @param $attributes
360+
*
361+
* @return array
362+
*/
363+
public function cleanHiddenAuditAttributes(array $attributes)
364+
{
365+
if ($this->isAuditRespectsHidden()) {
366+
367+
// Get hidden and visible attributes from the model
368+
$hidden = $this->getHidden();
369+
$visible = $this->getVisible();
370+
371+
// If visible is set, set to null any attributes which are not in visible
372+
if (count($visible) > 0) {
373+
foreach ($attributes as $key => &$value) {
374+
if (!in_array($key, $visible)) {
375+
$value = null;
376+
}
377+
}
378+
}
379+
380+
unset($value);
381+
382+
// If hidden is set, set to null any attributes which are in hidden
383+
if (count($hidden) > 0) {
384+
foreach ($hidden as $key) {
385+
if (array_key_exists($key, $attributes)) {
386+
$attributes[$key] = null;
387+
}
388+
}
389+
}
390+
}
391+
392+
return $attributes;
393+
}
345394
}

0 commit comments

Comments
 (0)