diff --git a/docs/changes/1.1.0.md b/docs/changes/1.1.0.md index 40b49b0db..5059e997e 100644 --- a/docs/changes/1.1.0.md +++ b/docs/changes/1.1.0.md @@ -34,6 +34,7 @@ - PowerPoint2007 Writer : Fixed broken animation for first shape - [@shannan1989](https://github.com/shannan1989) in [#783](https://github.com/PHPOffice/PHPPresentation/pull/783) - Samples : Allow to run without composer - [@pal-software](https://github.com/pal-software) in [#784](https://github.com/PHPOffice/PHPPresentation/pull/784) - PowerPoint2007 Writer: Extract relations from nested ShapeContainerInterface objects - [@DennisBirkholz](https://github.com/DennisBirkholz) in [#785](https://github.com/PHPOffice/PHPPresentation/pull/785) +- General : Fixed copying bug when presentation had multiple slides [@dees040](https://github.com/dees040) in [#786](https://github.com/PHPOffice/PHPPresentation/pull/786) ## Miscellaneous diff --git a/src/PhpPresentation/PhpPresentation.php b/src/PhpPresentation/PhpPresentation.php index 8435d26db..8af44718f 100644 --- a/src/PhpPresentation/PhpPresentation.php +++ b/src/PhpPresentation/PhpPresentation.php @@ -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; } diff --git a/tests/PhpPresentation/Tests/PhpPresentationTest.php b/tests/PhpPresentation/Tests/PhpPresentationTest.php index a093e6b02..a57a6f9d4 100644 --- a/tests/PhpPresentation/Tests/PhpPresentationTest.php +++ b/tests/PhpPresentation/Tests/PhpPresentationTest.php @@ -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()); } /**