Skip to content

Commit c95bc2d

Browse files
committed
Allow skipping empty versions if null
1 parent ae1f867 commit c95bc2d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Diff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function toSideBySideHtml(array $differOptions = [], array $renderOptions
6868

6969
protected function getContents(bool $stripTags = false): array
7070
{
71-
$newContents = $this->newVersion->contents;
71+
$newContents = $this->newVersion->contents ?? [];
7272

7373
// if the version strategy is DIFF, we need to merge the contents of all versions
7474
// from v1 to v2, v2 to v3, ..., vn-1 to vn.
@@ -85,12 +85,12 @@ protected function getContents(bool $stripTags = false): array
8585

8686
$oldContents = Arr::only($oldContents, array_keys($newContents));
8787
} else {
88-
$oldContents = $this->oldVersion->contents;
88+
$oldContents = $this->oldVersion->contents ?? [];
8989
}
9090

9191
if ($stripTags) {
92-
$oldContents = array_map(fn ($item) => strip_tags($item), $oldContents);
93-
$newContents = array_map(fn ($item) => strip_tags($item), $newContents);
92+
$oldContents = array_map(fn ($item) => strip_tags($item), $oldContents ?? []);
93+
$newContents = array_map(fn ($item) => strip_tags($item), $newContents ?? []);
9494
}
9595

9696
return [$oldContents, $newContents];

src/Versionable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ trait Versionable
2020
// You can add these properties to you versionable model
2121
// protected $versionable = [];
2222
// protected $dontVersionable = ['*'];
23+
// protected bool $skipEmptyVersions = false; // Skip creating versions when all versionable attributes are empty
2324

2425
// You can define this variable in class, that used this trait to change Model(table) for versions
2526
// Model MUST extend \Visualbuilder\Versionable\Version
@@ -244,6 +245,23 @@ public function shouldBeVersioning(): bool
244245

245246
$versionableAttributes = $this->getVersionableAttributes($this->getVersionStrategy());
246247

248+
// Check if we should skip empty versions
249+
if (property_exists($this, 'skipEmptyVersions') && $this->skipEmptyVersions) {
250+
// Check if all versionable attributes are empty
251+
$hasContent = false;
252+
foreach ($versionableAttributes as $key => $value) {
253+
if (! empty($value)) {
254+
$hasContent = true;
255+
break;
256+
}
257+
}
258+
259+
// Don't create version if no content
260+
if (! $hasContent) {
261+
return false;
262+
}
263+
}
264+
247265
return $this->versions()->count() === 0 || Arr::hasAny($this->getDirty(), array_keys($versionableAttributes));
248266
}
249267

0 commit comments

Comments
 (0)