Skip to content

Commit

Permalink
Presentation : Added ability to add a slide at any position (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 authored Aug 27, 2024
1 parent 6738a06 commit 3cae28b
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.

Fixes # (issue)

### Checklist:

- [ ] My CI is :green_circle:
- [ ] I have covered by unit tests my new code (check build/coverage for coverage report)
- [ ] I have updated the [documentation](https://github.com/PHPOffice/PHPPresentation/tree/develop/docs) to describe the changes
- [ ] I have updated the [changelog](https://github.com/PHPOffice/PHPPresentation/blob/develop/docs/changes/1.1.0.md)
9 changes: 7 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
run: ./vendor/bin/phpstan analyse -c phpstan.neon.dist

phpunit:
name: PHPUnit
name: PHPUnit ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -75,14 +75,19 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: gd, xml, zip
coverage: xdebug
coverage: ${{ (matrix.php == '7.3') && 'xdebug' || 'none' }}

- uses: actions/checkout@v2

- name: Composer Install
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run phpunit
if: matrix.php != '7.3'
run: ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage

- name: Run phpunit
if: matrix.php == '7.3'
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml

- name: Upload coverage results to Coveralls
Expand Down
1 change: 1 addition & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Shadow : Support for Type Inner & Reflection - [@devX2712](https://github.com/devX2712) in [#788](https://github.com/PHPOffice/PHPPresentation/pull/787)
- PowerPoint2007 Reader
- PowerPoint2007 Writer
- Presentation : Added ability to add a slide at any position - [@Progi1984](https://github.com/Progi1984) in [#810](https://github.com/PHPOffice/PHPPresentation/pull/810)

## Bugfixes

Expand Down
36 changes: 33 additions & 3 deletions docs/usage/slides/introduction.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
# Introduction

Slides are pages in a presentation. Slides are stored as a zero based array in `PHPPresentation` object. Use the method `createSlide` to create a new slide and retrieve the slide for other operation such as creating shapes for that slide.
Slides are pages in a presentation. Slides are stored as a zero based array in `PHPPresentation` object.

## Name
## Create slide

Use the method `createSlide` to create a new slide and retrieve the slide for other operation such as creating shapes for that slide. The slide will be added at the end of slides collection.

``` php
<?php

$slide = $presentation->createSlide();
```

## Add slide to a specific position

Use the method `addSlide` to add an existing slide to a specific position. Without the parameter `$position`, it will be added at the end of slides collection.

``` php
<?php

use PhpOffice\PhpPresentation\Slide;

$slide = new Slide($presentation);
## Add it before all slides
$presentation->addSlide($slide, 0);
## Add it to position 1
$presentation->addSlide($slide, 1);
## Add it after all slides
$presentation->addSlide($slide);
```

## Properties

### Name

By default, a slide has not a name.
You can define it with the method `setName`.
Expand All @@ -14,7 +44,7 @@ $slide = $presentation->createSlide();
$slide->setName('Title of the slide');
```

## Visibility
### Visibility

By default, a slide is visible.
You can define it with the method `setIsVisible`.
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ parameters:
## PHP 8.0 & GdImage
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, GdImage\ given\.#'
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, GdImage\|false given\.#'
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, \(GdImage\|false\) given\.#'
- '#^Parameter \#1 \$image of function imagesx expects GdImage, resource given\.#'
- '#^Parameter \#1 \$image of function imagesy expects GdImage, resource given\.#'
- '#^Parameter \#1 \$image of function imagealphablending expects GdImage, resource given\.#'
Expand Down
8 changes: 6 additions & 2 deletions src/PhpPresentation/PhpPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ public function createSlide(): Slide
/**
* Add slide.
*/
public function addSlide(Slide $slide): Slide
public function addSlide(Slide $slide, int $index = -1): Slide
{
$this->slideCollection[] = $slide;
if ($index > -1) {
array_splice($this->slideCollection, $index, 0, [$slide]);
} else {
$this->slideCollection[] = $slide;
}

return $slide;
}
Expand Down
4 changes: 3 additions & 1 deletion src/PhpPresentation/Shape/RichText/TextElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace PhpOffice\PhpPresentation\Shape\RichText;

use PhpOffice\PhpPresentation\Style\Font;

/**
* Rich text element interface.
*/
Expand All @@ -43,7 +45,7 @@ public function setText($pText = '');
/**
* Get font.
*
* @return \PhpOffice\PhpPresentation\Style\Font
* @return Font
*/
public function getFont();

Expand Down
63 changes: 62 additions & 1 deletion tests/PhpPresentation/Tests/PhpPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PhpOffice\PhpPresentation\Exception\OutOfBoundsException;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\PresentationProperties;
use PhpOffice\PhpPresentation\Slide;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -43,7 +44,7 @@ public function testConstruct(): void

self::assertEquals(new DocumentProperties(), $object->getDocumentProperties());
self::assertEquals(new DocumentLayout(), $object->getLayout());
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Slide', $object->getSlide());
self::assertInstanceOf(Slide::class, $object->getSlide());
self::assertCount(1, $object->getAllSlides());
self::assertEquals(0, $object->getIndex($slide));
self::assertEquals(1, $object->getSlideCount());
Expand Down Expand Up @@ -129,4 +130,64 @@ public function testSetActiveSlideIndexException(): void
$object = new PhpPresentation();
$object->setActiveSlideIndex(1);
}

public function testAddSlideAtStart(): void
{
$presentation = new PhpPresentation();
$presentation->removeSlideByIndex(0);
$slide1 = new Slide($presentation);
$slide1->setName('Slide 1');
$slide2 = new Slide($presentation);
$slide2->setName('Slide 2');
$slide3 = new Slide($presentation);
$slide3->setName('Slide 3');

$presentation->addSlide($slide1);
$presentation->addSlide($slide2);
$presentation->addSlide($slide3, 0);

self::assertEquals('Slide 3', $presentation->getSlide(0)->getName());
self::assertEquals('Slide 1', $presentation->getSlide(1)->getName());
self::assertEquals('Slide 2', $presentation->getSlide(2)->getName());
}

public function testAddSlideAtMiddle(): void
{
$presentation = new PhpPresentation();
$presentation->removeSlideByIndex(0);
$slide1 = new Slide($presentation);
$slide1->setName('Slide 1');
$slide2 = new Slide($presentation);
$slide2->setName('Slide 2');
$slide3 = new Slide($presentation);
$slide3->setName('Slide 3');

$presentation->addSlide($slide1);
$presentation->addSlide($slide2);
$presentation->addSlide($slide3, 1);

self::assertEquals('Slide 1', $presentation->getSlide(0)->getName());
self::assertEquals('Slide 3', $presentation->getSlide(1)->getName());
self::assertEquals('Slide 2', $presentation->getSlide(2)->getName());
}

public function testAddSlideAtEnd(): void
{
$presentation = new PhpPresentation();
$presentation->removeSlideByIndex(0);
$slide1 = new Slide($presentation);
$slide1->setName('Slide 1');
$slide2 = new Slide($presentation);
$slide2->setName('Slide 2');
$slide3 = new Slide($presentation);
$slide3->setName('Slide 3');

$presentation->addSlide($slide1);
$presentation->addSlide($slide2);
$presentation->addSlide($slide3);

self::assertEquals('Slide 1', $presentation->getSlide(0)->getName());
self::assertEquals('Slide 2', $presentation->getSlide(1)->getName());
self::assertEquals('Slide 3', $presentation->getSlide(2)->getName());
}
}

0 comments on commit 3cae28b

Please sign in to comment.