Skip to content

Commit 2767b47

Browse files
committed
Use Laravels attribute comparison
1 parent 4e477a6 commit 2767b47

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/Concerns/LogsActivity.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ public function getCreatedEventAttributes(): array
6363
*/
6464
public function getUpdatedEventAttributes(): array
6565
{
66+
$new = [];
6667
$old = $this->getOldAttributes();
67-
$new = $this->getLoggedAttributeValues();
6868

69-
$new = array_diff_uassoc($new, $old, function ($new, $old) {
70-
return $new <=> $old;
71-
});
69+
foreach ($this->getLoggedAttributeValues() as $key => $value) {
70+
if (!$this->oldIsEquivalent($key, $value)) {
71+
$new[$key] = $value;
72+
}
73+
}
74+
7275
$old = array_intersect_key($old, $new);
7376

7477
return [
@@ -77,6 +80,35 @@ public function getUpdatedEventAttributes(): array
7780
];
7881
}
7982

83+
/**
84+
* Determine if the new and old value for a given key are equivalent.
85+
*
86+
* @return bool
87+
*/
88+
protected function oldIsEquivalent($key, $new): bool
89+
{
90+
if (! array_key_exists($key, $this->oldAttributes)) {
91+
return false;
92+
}
93+
94+
$original = $this->oldAttributes[$key];
95+
96+
if ($new === $original) {
97+
return true;
98+
} elseif (is_null($new)) {
99+
return false;
100+
} elseif ($this->isDateAttribute($key)) {
101+
return $this->fromDateTime($new) ===
102+
$this->fromDateTime($original);
103+
} elseif ($this->hasCast($key)) {
104+
return $this->castAttribute($key, $new) ===
105+
$this->castAttribute($key, $original);
106+
}
107+
108+
return is_numeric($new) && is_numeric($original)
109+
&& strcmp((string) $new, (string) $original) === 0;
110+
}
111+
80112
/**
81113
* Get the attributes for the deleted event.
82114
*

0 commit comments

Comments
 (0)