Completed
Pull Request — master (#597)
by
unknown
08:06
created

PhpPresentation::setAllMasterSlides()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 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
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
use PhpOffice\PhpPresentation\Slide;
21
use PhpOffice\PhpPresentation\Slide\Iterator;
22
use PhpOffice\PhpPresentation\Slide\SlideMaster;
23
24
/**
25
 * PhpPresentation
26
 */
27
class PhpPresentation
28
{
29
    /**
30
     * Document properties
31
     *
32
     * @var \PhpOffice\PhpPresentation\DocumentProperties
33
     */
34
    protected $documentProperties;
35
36
    /**
37
     * Presentation properties
38
     *
39
     * @var \PhpOffice\PhpPresentation\PresentationProperties
40
     */
41
    protected $presentationProps;
42
43
    /**
44
     * Document layout
45
     *
46
     * @var \PhpOffice\PhpPresentation\DocumentLayout
47
     */
48
    protected $layout;
49
50
    /**
51
     * Collection of Slide objects
52
     *
53
     * @var \PhpOffice\PhpPresentation\Slide[]
54
     */
55
    protected $slideCollection = array();
56
57
    /**
58
     * Active slide index
59
     *
60
     * @var int
61
     */
62
    protected $activeSlideIndex = 0;
63
64
    /**
65
     * Collection of Master Slides
66
     * @var \ArrayObject|\PhpOffice\PhpPresentation\Slide\SlideMaster[]
67
     */
68
    protected $slideMasters;
69
70
    /**
71
     * Create a new PhpPresentation with one Slide
72
     */
73 222
    public function __construct()
74
    {
75
        // Set empty Master & SlideLayout
76 222
        $this->createMasterSlide()->createSlideLayout();
77
78
        // Initialise slide collection and add one slide
79 222
        $this->createSlide();
80 222
        $this->setActiveSlideIndex();
81
82
        // Set initial document properties & layout
83 222
        $this->setDocumentProperties(new DocumentProperties());
84 222
        $this->setPresentationProperties(new PresentationProperties());
85 222
        $this->setLayout(new DocumentLayout());
86 222
    }
87
88
    /**
89
     * Get properties
90
     *
91
     * @return \PhpOffice\PhpPresentation\DocumentProperties
92
     * @deprecated for getDocumentProperties
93
     */
94 1
    public function getProperties()
95
    {
96 1
        return $this->getDocumentProperties();
97
    }
98
99
    /**
100
     * Set properties
101
     *
102
     * @param  \PhpOffice\PhpPresentation\DocumentProperties $value
103
     * @deprecated for setDocumentProperties
104
     * @return PhpPresentation
105
     */
106 1
    public function setProperties(DocumentProperties $value)
107
    {
108 1
        return $this->setDocumentProperties($value);
109
    }
110
111
    /**
112
     * Get properties
113
     *
114
     * @return \PhpOffice\PhpPresentation\DocumentProperties
115
     */
116 186
    public function getDocumentProperties()
117
    {
118 186
        return $this->documentProperties;
119
    }
120
121
    /**
122
     * Set properties
123
     *
124
     * @param  \PhpOffice\PhpPresentation\DocumentProperties $value
125
     * @return PhpPresentation
126
     */
127 222
    public function setDocumentProperties(DocumentProperties $value)
128
    {
129 222
        $this->documentProperties = $value;
130
131 222
        return $this;
132
    }
133
134
    /**
135
     * Get presentation properties
136
     *
137
     * @return \PhpOffice\PhpPresentation\PresentationProperties
138
     */
139 183
    public function getPresentationProperties()
140
    {
141 183
        return $this->presentationProps;
142
    }
143
144
    /**
145
     * Set presentation properties
146
     *
147
     * @param  \PhpOffice\PhpPresentation\PresentationProperties $value
148
     * @return PhpPresentation
149
     */
150 222
    public function setPresentationProperties(PresentationProperties $value)
151
    {
152 222
        $this->presentationProps = $value;
153 222
        return $this;
154
    }
155
156
    /**
157
     * Get layout
158
     *
159
     * @return \PhpOffice\PhpPresentation\DocumentLayout
160
     */
161 179
    public function getLayout()
162
    {
163 179
        return $this->layout;
164
    }
165
166
    /**
167
     * Set layout
168
     *
169
     * @param  \PhpOffice\PhpPresentation\DocumentLayout $value
170
     * @return PhpPresentation
171
     */
172 222
    public function setLayout(DocumentLayout $value)
173
    {
174 222
        $this->layout = $value;
175
176 222
        return $this;
177
    }
178
179
    /**
180
     * Get active slide
181
     *
182
     * @return \PhpOffice\PhpPresentation\Slide
183
     */
184 222
    public function getActiveSlide()
185
    {
186 222
        return $this->slideCollection[$this->activeSlideIndex];
187
    }
188
189
    /**
190
     * Create slide and add it to this presentation
191
     *
192
     * @return \PhpOffice\PhpPresentation\Slide
193
     * @throws \Exception
194
     */
195 222
    public function createSlide($prepend=false)
196
    {
197 222
        $newSlide = new Slide($this);
198 222
        if ($prepend) {
199
            $this->prependSlide($newSlide);
200
        } else {
201 222
            $this->addSlide($newSlide);
202
        }
203 222
        return $newSlide;
204
    }
205
    
206
    /**
207
     * Add slide
208
     *
209
     * @param  \PhpOffice\PhpPresentation\Slide $slide
210
     * @throws \Exception
211
     * @return \PhpOffice\PhpPresentation\Slide
212
     */
213
    public function prependSlide(Slide $slide = null)
214
    {
215
        array_unshift($this->slideCollection, $slide);
216
        return $slide;
217
    }
218
219
    /**
220
     * Add slide
221
     *
222
     * @param  \PhpOffice\PhpPresentation\Slide $slide
223
     * @throws \Exception
224
     * @return \PhpOffice\PhpPresentation\Slide
225
     */
226 222
    public function addSlide(Slide $slide = null)
227
    {
228 222
        $this->slideCollection[] = $slide;
229
230 222
        return $slide;
231
    }
232
233
    /**
234
     * Remove slide by index
235
     *
236
     * @param  int $index Slide index
237
     * @throws \Exception
238
     * @return PhpPresentation
239
     */
240 16
    public function removeSlideByIndex($index = 0)
241
    {
242 16
        if ($index > count($this->slideCollection) - 1) {
243 1
            throw new \Exception("Slide index is out of bounds.");
244
        }
245 15
        array_splice($this->slideCollection, $index, 1);
246
247 15
        return $this;
248
    }
249
250
    /**
251
     * Get slide by index
252
     *
253
     * @param  int $index Slide index
254
     * @return \PhpOffice\PhpPresentation\Slide
255
     * @throws \Exception
256
     */
257 193
    public function getSlide($index = 0)
258
    {
259 193
        if ($index > count($this->slideCollection) - 1) {
260 1
            throw new \Exception("Slide index is out of bounds.");
261
        }
262 192
        return $this->slideCollection[$index];
263
    }
264
265
    /**
266
     * Get all slides
267
     *
268
     * @return \PhpOffice\PhpPresentation\Slide[]
269
     */
270 184
    public function getAllSlides()
271
    {
272 184
        return $this->slideCollection;
273
    }
274
275
    /**
276
     * Get index for slide
277
     *
278
     * @param  \PhpOffice\PhpPresentation\Slide\AbstractSlide $slide
279
     * @return int
280
     * @throws \Exception
281
     */
282 115
    public function getIndex(Slide\AbstractSlide $slide)
283
    {
284 115
        $index = null;
285 115
        foreach ($this->slideCollection as $key => $value) {
286 115
            if ($value->getHashCode() == $slide->getHashCode()) {
287 115
                $index = $key;
288 115
                break;
289
            }
290
        }
291 115
        return $index;
292
    }
293
294
    /**
295
     * Get slide count
296
     *
297
     * @return int
298
     */
299 196
    public function getSlideCount()
300
    {
301 196
        return count($this->slideCollection);
302
    }
303
304
    /**
305
     * Get active slide index
306
     *
307
     * @return int Active slide index
308
     */
309 1
    public function getActiveSlideIndex()
310
    {
311 1
        return $this->activeSlideIndex;
312
    }
313
314
    /**
315
     * Set active slide index
316
     *
317
     * @param  int $index Active slide index
318
     * @throws \Exception
319
     * @return \PhpOffice\PhpPresentation\Slide
320
     */
321 222
    public function setActiveSlideIndex($index = 0)
322
    {
323 222
        if ($index > count($this->slideCollection) - 1) {
324 1
            throw new \Exception("Active slide index is out of bounds.");
325
        }
326 222
        $this->activeSlideIndex = $index;
327
328 222
        return $this->getActiveSlide();
329
    }
330
331
    /**
332
     * Add external slide
333
     *
334
     * @param  \PhpOffice\PhpPresentation\Slide $slide External slide to add
335
     * @throws \Exception
336
     * @return \PhpOffice\PhpPresentation\Slide
337
     */
338 1
    public function addExternalSlide(Slide $slide)
339
    {
340 1
        $slide->rebindParent($this);
341
342 1
        $this->addMasterSlide($slide->getSlideLayout()->getSlideMaster());
343
344 1
        return $this->addSlide($slide);
345
    }
346
347
    /**
348
     * Get slide iterator
349
     *
350
     * @return \PhpOffice\PhpPresentation\Slide\Iterator
351
     */
352 1
    public function getSlideIterator()
353
    {
354 1
        return new Iterator($this);
355
    }
356
357
    /**
358
     * Create a masterslide and add it to this presentation
359
     *
360
     * @return \PhpOffice\PhpPresentation\Slide\SlideMaster
361
     * @throws \Exception
362
     */
363 222
    public function createMasterSlide()
364
    {
365 222
        $newMasterSlide = new SlideMaster($this);
366 222
        $this->addMasterSlide($newMasterSlide);
367 222
        return $newMasterSlide;
368
    }
369
370
    /**
371
     * Add masterslide
372
     *
373
     * @param  \PhpOffice\PhpPresentation\Slide\SlideMaster $slide
374
     * @return \PhpOffice\PhpPresentation\Slide\SlideMaster
375
     * @throws \Exception
376
     */
377 222
    public function addMasterSlide(SlideMaster $slide = null)
378
    {
379 222
        $this->slideMasters[] = $slide;
380
381 222
        return $slide;
382
    }
383
384
    /**
385
     * Copy presentation (!= clone!)
386
     *
387
     * @return PhpPresentation
388
     * @throws \Exception
389
     */
390 1
    public function copy()
391
    {
392 1
        $copied = clone $this;
393
394 1
        $slideCount = count($this->slideCollection);
395 1
        for ($i = 0; $i < $slideCount; ++$i) {
396 1
            $this->slideCollection[$i] = $this->slideCollection[$i]->copy();
397 1
            $this->slideCollection[$i]->rebindParent($this);
398
        }
399
400 1
        return $copied;
401
    }
402
403
    /**
404
     * Mark a document as final
405
     * @param bool $state
406
     * @return PresentationProperties
407
     * @deprecated for getPresentationProperties()->markAsFinal()
408
     */
409 1
    public function markAsFinal($state = true)
410
    {
411 1
        return $this->getPresentationProperties()->markAsFinal($state);
412
    }
413
414
    /**
415
     * Return if this document is marked as final
416
     * @return bool
417
     * @deprecated for getPresentationProperties()->isMarkedAsFinal()
418
     */
419 1
    public function isMarkedAsFinal()
420
    {
421 1
        return $this->getPresentationProperties()->isMarkedAsFinal();
422
    }
423
424
    /**
425
     * Set the zoom of the document (in percentage)
426
     * @param float $zoom
427
     * @return PresentationProperties
428
     * @deprecated for getPresentationProperties()->setZoom()
429
     */
430 1
    public function setZoom($zoom = 1.0)
431
    {
432 1
        return $this->getPresentationProperties()->setZoom($zoom);
433
    }
434
435
    /**
436
     * Return the zoom (in percentage)
437
     * @return float
438
     * @deprecated for getPresentationProperties()->getZoom()
439
     */
440 1
    public function getZoom()
441
    {
442 1
        return $this->getPresentationProperties()->getZoom();
443
    }
444
445
    /**
446
     * @return \ArrayObject|Slide\SlideMaster[]
447
     */
448 222
    public function getAllMasterSlides()
449
    {
450 222
        return $this->slideMasters;
451
    }
452
453
    /**
454
     * @param \ArrayObject|Slide\SlideMaster[] $slideMasters
455
     * @return $this
456
     */
457 4
    public function setAllMasterSlides($slideMasters = array())
458
    {
459 4
        if ($slideMasters instanceof \ArrayObject || is_array($slideMasters)) {
460 4
            $this->slideMasters = $slideMasters;
461
        }
462 4
        return $this;
463
    }
464
}
465