From 128541b13e1c88eaae803287af201b8d532db733 Mon Sep 17 00:00:00 2001 From: mutschler Date: Sat, 23 Mar 2024 01:23:54 +0100 Subject: [PATCH] fix: add diff support for diff strategy --- src/Diff.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Diff.php b/src/Diff.php index 7c0e11c..eaeec58 100644 --- a/src/Diff.php +++ b/src/Diff.php @@ -60,6 +60,32 @@ public function toSideBySideHtml(array $differOptions = [], array $renderOptions return $this->render('SideBySide', $differOptions, $renderOptions); } + protected function getContents(): array { + if ($this->toVersion->versionable->getVersionStrategy() === VersionStrategy::DIFF) { + if ($this->toVersion->previousVersions()->get()->last()) { + $newContents = json_decode($this->toVersion->previousVersions()->get()->last()->getRawOriginal()['contents'], true); + } else { + $newContents = $this->toVersion->versionable()->firstVersion()->get()->first()->toArray(); + }; + + $oldContents = $this->fromVersion->contents; + + $versionsBeforeThis = $this->toVersion->previousVersions()->get(); + foreach ($versionsBeforeThis as $version) { + if (! empty($version->contents)) { + $newContents = array_merge($newContents, $version->contents); + } + } + + $newContents = Arr::only($newContents, array_keys($oldContents)); + } else { + $oldContents = $this->fromVersion->contents; + $newContents = $this->toVersion->contents; + } + + return [$oldContents, $newContents]; + } + public function render(?string $renderer = null, array $differOptions = [], array $renderOptions = []): array { if (empty($differOptions)) { @@ -70,8 +96,7 @@ public function render(?string $renderer = null, array $differOptions = [], arra $renderOptions = $this->renderOptions; } - $oldContents = $this->fromVersion->contents; - $newContents = $this->toVersion->contents; + list($oldContents, $newContents) = $this->getContents(); $diff = []; $createDiff = function ($key, $old, $new) use (&$diff, $renderer, $differOptions, $renderOptions) { @@ -101,8 +126,7 @@ public function getStatistics(array $differOptions = []): array $differOptions = $this->differOptions; } - $oldContents = $this->fromVersion->contents; - $newContents = $this->toVersion->contents; + list($oldContents, $newContents) = $this->getContents(); $diffStats = new Collection;