Skip to content

Commit

Permalink
fix: presentation with multiple slides should be copyable
Browse files Browse the repository at this point in the history
  • Loading branch information
dees040 committed Dec 12, 2023
1 parent 85c7821 commit f3cb8d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit f3cb8d1

Please sign in to comment.