From 7479f0be546af98727897c675c663896534d416f Mon Sep 17 00:00:00 2001 From: Jeremy Graham Date: Mon, 30 Aug 2021 16:48:41 +1000 Subject: [PATCH 1/3] Respect $dontVersionFields when versioning attributes --- src/Mpociot/Versionable/VersionableTrait.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index 9be633a..9235ed3 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -176,7 +176,7 @@ protected function versionablePostSave() $versionedHiddenFields = $this->versionedHiddenFields ?? []; $this->makeVisible($versionedHiddenFields); - $version->model_data = serialize($this->attributesToArray()); + $version->model_data = serialize($this->attributesToVersion()); $this->makeHidden($versionedHiddenFields); if (!empty( $this->reason )) { @@ -189,6 +189,19 @@ protected function versionablePostSave() } } + /** + * Wrapper for getting attributes to version, respecting $dontVersionFields. + * + * @return array + */ + protected function attributesToVersion() + { + $attributes = $this->attributesToArray(); + $ignore = $this->dontVersionFields ?? []; + + return collect($attributes)->except($ignore)->all(); + } + /** * Delete old versions of this model when the reach a specific count. * From c48774225936b30646dcdb3b298ca6a211844443 Mon Sep 17 00:00:00 2001 From: Jeremy Graham Date: Tue, 31 Aug 2021 10:15:46 +1000 Subject: [PATCH 2/3] Added support for recursive diff --- src/Mpociot/Versionable/Version.php | 31 ++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Mpociot/Versionable/Version.php b/src/Mpociot/Versionable/Version.php index d145eb1..4829132 100644 --- a/src/Mpociot/Versionable/Version.php +++ b/src/Mpociot/Versionable/Version.php @@ -89,7 +89,7 @@ public function diff(Version $againstVersion = null) $model = $this->getModel(); $diff = $againstVersion ? $againstVersion->getModel() : $this->versionable()->withTrashed()->first()->currentVersion()->getModel(); - $diffArray = array_diff_assoc($diff->getAttributes(), $model->getAttributes()); + $diffArray = $this->arrayDiffAssocRecursive($diff->getAttributes(), $model->getAttributes()); if (isset( $diffArray[ $model->getCreatedAtColumn() ] )) { unset( $diffArray[ $model->getCreatedAtColumn() ] ); @@ -104,4 +104,33 @@ public function diff(Version $againstVersion = null) return $diffArray; } + /** + * Recursive version of array_diff_assoc. + * + * @param $array1 + * @param $array2 + * + * @return array + */ + public function arrayDiffAssocRecursive($array1, $array2) + { + $difference = []; + + foreach ($array1 as $key => $value) { + if (is_array($value)) { + if (!isset($array2[$key]) || !is_array($array2[$key])) { + $difference[$key] = $value; + } else { + $new_diff = $this->arrayDiffAssocRecursive($value, $array2[$key]); + if (!empty($new_diff)) { + $difference[$key] = $new_diff; + } + } + } elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) { + $difference[$key] = $value; + } + } + + return $difference; + } } From 57d0bdd4c3f5c7a9db7c439b6a134d3ad220b835 Mon Sep 17 00:00:00 2001 From: Jeremy Graham Date: Fri, 3 Sep 2021 06:25:25 +1000 Subject: [PATCH 3/3] Minor performance improvement to arrayDiffAssocRecursive --- src/Mpociot/Versionable/Version.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Mpociot/Versionable/Version.php b/src/Mpociot/Versionable/Version.php index 4829132..5509547 100644 --- a/src/Mpociot/Versionable/Version.php +++ b/src/Mpociot/Versionable/Version.php @@ -118,15 +118,15 @@ public function arrayDiffAssocRecursive($array1, $array2) foreach ($array1 as $key => $value) { if (is_array($value)) { - if (!isset($array2[$key]) || !is_array($array2[$key])) { - $difference[$key] = $value; - } else { + if (isset($array2[$key])) { $new_diff = $this->arrayDiffAssocRecursive($value, $array2[$key]); - if (!empty($new_diff)) { + if (! empty($new_diff)) { $difference[$key] = $new_diff; } + } else { + $difference[$key] = $value; } - } elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) { + } elseif (! in_array($value, $array2, true)) { $difference[$key] = $value; } }