Skip to content

Commit 3cae28b

Browse files
authored
Presentation : Added ability to add a slide at any position (#810)
1 parent 6738a06 commit 3cae28b

File tree

8 files changed

+125
-9
lines changed

8 files changed

+125
-9
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### Description
2+
3+
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.
4+
5+
Fixes # (issue)
6+
7+
### Checklist:
8+
9+
- [ ] My CI is :green_circle:
10+
- [ ] I have covered by unit tests my new code (check build/coverage for coverage report)
11+
- [ ] I have updated the [documentation](https://github.com/PHPOffice/PHPPresentation/tree/develop/docs) to describe the changes
12+
- [ ] I have updated the [changelog](https://github.com/PHPOffice/PHPPresentation/blob/develop/docs/changes/1.1.0.md)

.github/workflows/php.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
run: ./vendor/bin/phpstan analyse -c phpstan.neon.dist
6464

6565
phpunit:
66-
name: PHPUnit
66+
name: PHPUnit ${{ matrix.php }}
6767
runs-on: ubuntu-latest
6868
strategy:
6969
fail-fast: false
@@ -75,14 +75,19 @@ jobs:
7575
with:
7676
php-version: ${{ matrix.php }}
7777
extensions: gd, xml, zip
78-
coverage: xdebug
78+
coverage: ${{ (matrix.php == '7.3') && 'xdebug' || 'none' }}
7979

8080
- uses: actions/checkout@v2
8181

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

8585
- name: Run phpunit
86+
if: matrix.php != '7.3'
87+
run: ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage
88+
89+
- name: Run phpunit
90+
if: matrix.php == '7.3'
8691
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml
8792

8893
- name: Upload coverage results to Coveralls

docs/changes/1.1.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Shadow : Support for Type Inner & Reflection - [@devX2712](https://github.com/devX2712) in [#788](https://github.com/PHPOffice/PHPPresentation/pull/787)
3636
- PowerPoint2007 Reader
3737
- PowerPoint2007 Writer
38+
- Presentation : Added ability to add a slide at any position - [@Progi1984](https://github.com/Progi1984) in [#810](https://github.com/PHPOffice/PHPPresentation/pull/810)
3839

3940
## Bugfixes
4041

docs/usage/slides/introduction.md

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
# Introduction
22

3-
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.
3+
Slides are pages in a presentation. Slides are stored as a zero based array in `PHPPresentation` object.
44

5-
## Name
5+
## Create slide
6+
7+
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.
8+
9+
``` php
10+
<?php
11+
12+
$slide = $presentation->createSlide();
13+
```
14+
15+
## Add slide to a specific position
16+
17+
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.
18+
19+
``` php
20+
<?php
21+
22+
use PhpOffice\PhpPresentation\Slide;
23+
24+
$slide = new Slide($presentation);
25+
## Add it before all slides
26+
$presentation->addSlide($slide, 0);
27+
## Add it to position 1
28+
$presentation->addSlide($slide, 1);
29+
## Add it after all slides
30+
$presentation->addSlide($slide);
31+
```
32+
33+
## Properties
34+
35+
### Name
636

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

17-
## Visibility
47+
### Visibility
1848

1949
By default, a slide is visible.
2050
You can define it with the method `setIsVisible`.

phpstan.neon.dist

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ parameters:
1818
## PHP 8.0 & GdImage
1919
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, GdImage\ given\.#'
2020
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, GdImage\|false given\.#'
21+
- '#^Parameter \#1 \$value of method PhpOffice\\PhpPresentation\\Shape\\Drawing\\Gd::setImageResource\(\) expects resource\|null, \(GdImage\|false\) given\.#'
2122
- '#^Parameter \#1 \$image of function imagesx expects GdImage, resource given\.#'
2223
- '#^Parameter \#1 \$image of function imagesy expects GdImage, resource given\.#'
2324
- '#^Parameter \#1 \$image of function imagealphablending expects GdImage, resource given\.#'

src/PhpPresentation/PhpPresentation.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ public function createSlide(): Slide
165165
/**
166166
* Add slide.
167167
*/
168-
public function addSlide(Slide $slide): Slide
168+
public function addSlide(Slide $slide, int $index = -1): Slide
169169
{
170-
$this->slideCollection[] = $slide;
170+
if ($index > -1) {
171+
array_splice($this->slideCollection, $index, 0, [$slide]);
172+
} else {
173+
$this->slideCollection[] = $slide;
174+
}
171175

172176
return $slide;
173177
}

src/PhpPresentation/Shape/RichText/TextElementInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
namespace PhpOffice\PhpPresentation\Shape\RichText;
2121

22+
use PhpOffice\PhpPresentation\Style\Font;
23+
2224
/**
2325
* Rich text element interface.
2426
*/
@@ -43,7 +45,7 @@ public function setText($pText = '');
4345
/**
4446
* Get font.
4547
*
46-
* @return \PhpOffice\PhpPresentation\Style\Font
48+
* @return Font
4749
*/
4850
public function getFont();
4951

tests/PhpPresentation/Tests/PhpPresentationTest.php

+62-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PhpOffice\PhpPresentation\Exception\OutOfBoundsException;
2525
use PhpOffice\PhpPresentation\PhpPresentation;
2626
use PhpOffice\PhpPresentation\PresentationProperties;
27+
use PhpOffice\PhpPresentation\Slide;
2728
use PHPUnit\Framework\TestCase;
2829

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

4445
self::assertEquals(new DocumentProperties(), $object->getDocumentProperties());
4546
self::assertEquals(new DocumentLayout(), $object->getLayout());
46-
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Slide', $object->getSlide());
47+
self::assertInstanceOf(Slide::class, $object->getSlide());
4748
self::assertCount(1, $object->getAllSlides());
4849
self::assertEquals(0, $object->getIndex($slide));
4950
self::assertEquals(1, $object->getSlideCount());
@@ -129,4 +130,64 @@ public function testSetActiveSlideIndexException(): void
129130
$object = new PhpPresentation();
130131
$object->setActiveSlideIndex(1);
131132
}
133+
134+
public function testAddSlideAtStart(): void
135+
{
136+
$presentation = new PhpPresentation();
137+
$presentation->removeSlideByIndex(0);
138+
$slide1 = new Slide($presentation);
139+
$slide1->setName('Slide 1');
140+
$slide2 = new Slide($presentation);
141+
$slide2->setName('Slide 2');
142+
$slide3 = new Slide($presentation);
143+
$slide3->setName('Slide 3');
144+
145+
$presentation->addSlide($slide1);
146+
$presentation->addSlide($slide2);
147+
$presentation->addSlide($slide3, 0);
148+
149+
self::assertEquals('Slide 3', $presentation->getSlide(0)->getName());
150+
self::assertEquals('Slide 1', $presentation->getSlide(1)->getName());
151+
self::assertEquals('Slide 2', $presentation->getSlide(2)->getName());
152+
}
153+
154+
public function testAddSlideAtMiddle(): void
155+
{
156+
$presentation = new PhpPresentation();
157+
$presentation->removeSlideByIndex(0);
158+
$slide1 = new Slide($presentation);
159+
$slide1->setName('Slide 1');
160+
$slide2 = new Slide($presentation);
161+
$slide2->setName('Slide 2');
162+
$slide3 = new Slide($presentation);
163+
$slide3->setName('Slide 3');
164+
165+
$presentation->addSlide($slide1);
166+
$presentation->addSlide($slide2);
167+
$presentation->addSlide($slide3, 1);
168+
169+
self::assertEquals('Slide 1', $presentation->getSlide(0)->getName());
170+
self::assertEquals('Slide 3', $presentation->getSlide(1)->getName());
171+
self::assertEquals('Slide 2', $presentation->getSlide(2)->getName());
172+
}
173+
174+
public function testAddSlideAtEnd(): void
175+
{
176+
$presentation = new PhpPresentation();
177+
$presentation->removeSlideByIndex(0);
178+
$slide1 = new Slide($presentation);
179+
$slide1->setName('Slide 1');
180+
$slide2 = new Slide($presentation);
181+
$slide2->setName('Slide 2');
182+
$slide3 = new Slide($presentation);
183+
$slide3->setName('Slide 3');
184+
185+
$presentation->addSlide($slide1);
186+
$presentation->addSlide($slide2);
187+
$presentation->addSlide($slide3);
188+
189+
self::assertEquals('Slide 1', $presentation->getSlide(0)->getName());
190+
self::assertEquals('Slide 2', $presentation->getSlide(1)->getName());
191+
self::assertEquals('Slide 3', $presentation->getSlide(2)->getName());
192+
}
132193
}

0 commit comments

Comments
 (0)