Skip to content

Commit

Permalink
fix: add diff support for diff strategy (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mutschler committed Mar 23, 2024
1 parent 3e12aa2 commit 5de32f6
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5de32f6

Please sign in to comment.