Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General : Fixed copying bug when presentation had multiple slides #786

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions src/PhpPresentation/PhpPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,26 @@ public function copy(): self
$copied = clone $this;

$slideCount = count($this->slideCollection);

// Because the rebindParent() method on AbstractSlide removes the slide
// from the parent (current $this which we're cloning) presentation, we
// save the collection. This way, after the copying has finished, we can
// return the slides to the original presentation.
$oldSlideCollection = $this->slideCollection;
$newSlideCollection = [];

for ($i = 0; $i < $slideCount; ++$i) {
$this->slideCollection[$i] = $this->slideCollection[$i]->copy();
$this->slideCollection[$i]->rebindParent($this);
$newSlideCollection[$i] = $oldSlideCollection[$i]->copy();
$newSlideCollection[$i]->rebindParent($copied);
}

// Give the copied presentation a copied slide collection which the
// copied slides have been rebind to the copied presentation.
$copied->slideCollection = $newSlideCollection;

// Return the original slides to the original presentation.
$this->slideCollection = $oldSlideCollection;

return $copied;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/PhpPresentation/Tests/PhpPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ public function testAddExternalSlide(): void
public function testCopy(): void
{
$object = new PhpPresentation();
self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->copy());
$object->createSlide();

$copy = $object->copy();

self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $copy);
self::assertEquals(2, $copy->getSlideCount());
}

/**
Expand Down
Loading