Skip to content

Commit f3cb8d1

Browse files
committed
fix: presentation with multiple slides should be copyable
1 parent 85c7821 commit f3cb8d1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/PhpPresentation/PhpPresentation.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,26 @@ public function copy(): self
312312
$copied = clone $this;
313313

314314
$slideCount = count($this->slideCollection);
315+
316+
// Because the rebindParent() method on AbstractSlide removes the slide
317+
// from the parent (current $this which we're cloning) presentation, we
318+
// save the collection. This way, after the copying has finished, we can
319+
// return the slides to the original presentation.
320+
$oldSlideCollection = $this->slideCollection;
321+
$newSlideCollection = [];
322+
315323
for ($i = 0; $i < $slideCount; ++$i) {
316-
$this->slideCollection[$i] = $this->slideCollection[$i]->copy();
317-
$this->slideCollection[$i]->rebindParent($this);
324+
$newSlideCollection[$i] = $oldSlideCollection[$i]->copy();
325+
$newSlideCollection[$i]->rebindParent($copied);
318326
}
319327

328+
// Give the copied presentation a copied slide collection which the
329+
// copied slides have been rebind to the copied presentation.
330+
$copied->slideCollection = $newSlideCollection;
331+
332+
// Return the original slides to the original presentation.
333+
$this->slideCollection = $oldSlideCollection;
334+
320335
return $copied;
321336
}
322337

tests/PhpPresentation/Tests/PhpPresentationTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ public function testAddExternalSlide(): void
8686
public function testCopy(): void
8787
{
8888
$object = new PhpPresentation();
89-
self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->copy());
89+
$object->createSlide();
90+
91+
$copy = $object->copy();
92+
93+
self::assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $copy);
94+
self::assertEquals(2, $copy->getSlideCount());
9095
}
9196

9297
/**

0 commit comments

Comments
 (0)