Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Mpociot/Versionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ] );
Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jez500 - have you tested this for performance changes? Is this slower, the same, faster?

Also, is this adding recursivity, which this didn't have before?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nonoesp

This will certainly be slower than the native php functions, but it is also doing more and doesn't cause a fatal error if a property is not a string (eg json fields). That said I have noticed no notable difference in performance with my testing.

Recursively is likely unavoidable with nested arrays, but will only kick in if there are actually nested arrays.

{
$difference = [];

foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (isset($array2[$key])) {
$new_diff = $this->arrayDiffAssocRecursive($value, $array2[$key]);
if (! empty($new_diff)) {
$difference[$key] = $new_diff;
}
} else {
$difference[$key] = $value;
}
} elseif (! in_array($value, $array2, true)) {
$difference[$key] = $value;
}
}

return $difference;
}
}
15 changes: 14 additions & 1 deletion src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )) {
Expand All @@ -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.
*
Expand Down