Skip to content

Commit

Permalink
Merge pull request #786 from dees040/bug/copy
Browse files Browse the repository at this point in the history
General : Fixed copying bug when presentation had multiple slides
  • Loading branch information
Progi1984 committed Dec 13, 2023
2 parents 85c7821 + 50b908c commit dae3f3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 dae3f3d

Please sign in to comment.