Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.97% covered (success)
96.97%
64 / 66
95.83% covered (success)
95.83%
23 / 24
CRAP
0.00% covered (danger)
0.00%
0 / 1
PhpPresentation
96.97% covered (success)
96.97%
64 / 66
95.83% covered (success)
95.83%
23 / 24
34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getDocumentProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDocumentProperties
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPresentationProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPresentationProperties
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLayout
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLayout
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActiveSlide
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createSlide
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addSlide
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 removeSlideByIndex
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSlide
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getAllSlides
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIndex
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
 getSlideCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveSlideIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setActiveSlideIndex
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addExternalSlide
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSlideIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMasterSlide
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addMasterSlide
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 copy
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getAllMasterSlides
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAllMasterSlides
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation;
21
22use ArrayObject;
23use PhpOffice\PhpPresentation\Exception\OutOfBoundsException;
24use PhpOffice\PhpPresentation\Slide\Iterator;
25use PhpOffice\PhpPresentation\Slide\SlideMaster;
26
27/**
28 * PhpPresentation.
29 */
30class PhpPresentation
31{
32    /**
33     * Document properties.
34     *
35     * @var DocumentProperties
36     */
37    protected $documentProperties;
38
39    /**
40     * Presentation properties.
41     *
42     * @var PresentationProperties
43     */
44    protected $presentationProps;
45
46    /**
47     * Document layout.
48     *
49     * @var DocumentLayout
50     */
51    protected $layout;
52
53    /**
54     * Collection of Slide objects.
55     *
56     * @var array<int, Slide>
57     */
58    protected $slideCollection = [];
59
60    /**
61     * Active slide index.
62     *
63     * @var int
64     */
65    protected $activeSlideIndex = 0;
66
67    /**
68     * Collection of Master Slides.
69     *
70     * @var array<int, SlideMaster>|ArrayObject<int, SlideMaster>
71     */
72    protected $slideMasters;
73
74    /**
75     * Create a new PhpPresentation with one Slide.
76     */
77    public function __construct()
78    {
79        // Set empty Master & SlideLayout
80        $this->createMasterSlide()->createSlideLayout();
81
82        // Initialise slide collection and add one slide
83        $this->createSlide();
84        $this->setActiveSlideIndex();
85
86        // Set initial document properties & layout
87        $this->setDocumentProperties(new DocumentProperties());
88        $this->setPresentationProperties(new PresentationProperties());
89        $this->setLayout(new DocumentLayout());
90    }
91
92    /**
93     * Get properties.
94     */
95    public function getDocumentProperties(): DocumentProperties
96    {
97        return $this->documentProperties;
98    }
99
100    /**
101     * Set properties.
102     */
103    public function setDocumentProperties(DocumentProperties $value): self
104    {
105        $this->documentProperties = $value;
106
107        return $this;
108    }
109
110    /**
111     * Get presentation properties.
112     */
113    public function getPresentationProperties(): PresentationProperties
114    {
115        return $this->presentationProps;
116    }
117
118    /**
119     * Set presentation properties.
120     */
121    public function setPresentationProperties(PresentationProperties $value): self
122    {
123        $this->presentationProps = $value;
124
125        return $this;
126    }
127
128    /**
129     * Get layout.
130     */
131    public function getLayout(): DocumentLayout
132    {
133        return $this->layout;
134    }
135
136    /**
137     * Set layout.
138     */
139    public function setLayout(DocumentLayout $value): self
140    {
141        $this->layout = $value;
142
143        return $this;
144    }
145
146    /**
147     * Get active slide.
148     */
149    public function getActiveSlide(): Slide
150    {
151        return $this->slideCollection[$this->activeSlideIndex];
152    }
153
154    /**
155     * Create slide and add it to this presentation.
156     */
157    public function createSlide(): Slide
158    {
159        $newSlide = new Slide($this);
160        $this->addSlide($newSlide);
161
162        return $newSlide;
163    }
164
165    /**
166     * Add slide.
167     */
168    public function addSlide(Slide $slide, int $index = -1): Slide
169    {
170        if ($index > -1) {
171            array_splice($this->slideCollection, $index, 0, [$slide]);
172        } else {
173            $this->slideCollection[] = $slide;
174        }
175
176        return $slide;
177    }
178
179    /**
180     * Remove slide by index.
181     *
182     * @param int $index Slide index
183     */
184    public function removeSlideByIndex(int $index = 0): self
185    {
186        if ($index > count($this->slideCollection) - 1) {
187            throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index);
188        }
189        array_splice($this->slideCollection, $index, 1);
190
191        return $this;
192    }
193
194    /**
195     * Get slide by index.
196     *
197     * @param int $index Slide index
198     */
199    public function getSlide(int $index = 0): Slide
200    {
201        if ($index > count($this->slideCollection) - 1) {
202            throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index);
203        }
204
205        return $this->slideCollection[$index];
206    }
207
208    /**
209     * Get all slides.
210     *
211     * @return array<int, Slide>
212     */
213    public function getAllSlides(): array
214    {
215        return $this->slideCollection;
216    }
217
218    /**
219     * Get index for slide.
220     */
221    public function getIndex(Slide\AbstractSlide $slide): ?int
222    {
223        if (empty($this->slideCollection)) {
224            return null;
225        }
226        foreach ($this->slideCollection as $key => $value) {
227            if ($value->getHashCode() == $slide->getHashCode()) {
228                return $key;
229            }
230        }
231
232        return null;
233    }
234
235    /**
236     * Get slide count.
237     */
238    public function getSlideCount(): int
239    {
240        return count($this->slideCollection);
241    }
242
243    /**
244     * Get active slide index.
245     *
246     * @return int Active slide index
247     */
248    public function getActiveSlideIndex(): int
249    {
250        return $this->activeSlideIndex;
251    }
252
253    /**
254     * Set active slide index.
255     *
256     * @param int $index Active slide index
257     */
258    public function setActiveSlideIndex(int $index = 0): Slide
259    {
260        if ($index > count($this->slideCollection) - 1) {
261            throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index);
262        }
263        $this->activeSlideIndex = $index;
264
265        return $this->getActiveSlide();
266    }
267
268    /**
269     * Add external slide.
270     *
271     * @param Slide $slide External slide to add
272     */
273    public function addExternalSlide(Slide $slide): Slide
274    {
275        $slide->rebindParent($this);
276
277        $this->addMasterSlide($slide->getSlideLayout()->getSlideMaster());
278
279        return $this->addSlide($slide);
280    }
281
282    /**
283     * Get slide iterator.
284     */
285    public function getSlideIterator(): Iterator
286    {
287        return new Iterator($this);
288    }
289
290    /**
291     * Create a masterslide and add it to this presentation.
292     */
293    public function createMasterSlide(): SlideMaster
294    {
295        $newMasterSlide = new SlideMaster($this);
296        $this->addMasterSlide($newMasterSlide);
297
298        return $newMasterSlide;
299    }
300
301    /**
302     * Add masterslide.
303     */
304    public function addMasterSlide(SlideMaster $slide): SlideMaster
305    {
306        $this->slideMasters[] = $slide;
307
308        return $slide;
309    }
310
311    /**
312     * Copy presentation (!= clone!).
313     */
314    public function copy(): self
315    {
316        $copied = clone $this;
317
318        $slideCount = count($this->slideCollection);
319
320        // Because the rebindParent() method on AbstractSlide removes the slide
321        // from the parent (current $this which we're cloning) presentation, we
322        // save the collection. This way, after the copying has finished, we can
323        // return the slides to the original presentation.
324        $oldSlideCollection = $this->slideCollection;
325        $newSlideCollection = [];
326
327        for ($i = 0; $i < $slideCount; ++$i) {
328            $newSlideCollection[$i] = $oldSlideCollection[$i]->copy();
329            $newSlideCollection[$i]->rebindParent($copied);
330        }
331
332        // Give the copied presentation a copied slide collection which the
333        // copied slides have been rebind to the copied presentation.
334        $copied->slideCollection = $newSlideCollection;
335
336        // Return the original slides to the original presentation.
337        $this->slideCollection = $oldSlideCollection;
338
339        return $copied;
340    }
341
342    /**
343     * @return array<int, Slide\SlideMaster>|ArrayObject<int, Slide\SlideMaster>
344     */
345    public function getAllMasterSlides()
346    {
347        return $this->slideMasters;
348    }
349
350    /**
351     * @param array<int, Slide\SlideMaster>|ArrayObject<int, Slide\SlideMaster> $slideMasters
352     */
353    public function setAllMasterSlides($slideMasters = []): self
354    {
355        if ($slideMasters instanceof ArrayObject || is_array($slideMasters)) {
356            $this->slideMasters = $slideMasters;
357        }
358
359        return $this;
360    }
361}