Completed
Pull Request — develop (#178)
by
unknown
31:08 queued 26:53
created

Slide::addShape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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\GeometryCalculator;
21
use PhpOffice\PhpPresentation\Shape\Chart;
22
use PhpOffice\PhpPresentation\Shape\Drawing;
23
use PhpOffice\PhpPresentation\Shape\Group;
24
use PhpOffice\PhpPresentation\Shape\Line;
25
use PhpOffice\PhpPresentation\Shape\RichText;
26
use PhpOffice\PhpPresentation\Shape\Table;
27
use PhpOffice\PhpPresentation\Slide\AbstractBackground;
28
use PhpOffice\PhpPresentation\Slide\Layout;
29
use PhpOffice\PhpPresentation\Slide\Note;
30
use PhpOffice\PhpPresentation\Slide\Transition;
31
32
/**
33
 * Slide class
34
 */
35
class Slide implements ComparableInterface, ShapeContainerInterface
36
{
37
    /**
38
     * Parent presentation
39
     *
40
     * @var PhpPresentation
41
     */
42
    private $parent;
43
44
    /**
45
     * Collection of shapes
46
     *
47
     * @var \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[]
48
     */
49
    private $shapeCollection = null;
50
51
    /**
52
     * Slide identifier
53
     *
54
     * @var string
55
     */
56
    private $identifier;
57
58
    /**
59
     * Slide layout
60
     *
61
     * @var string
62
     */
63
    private $slideLayout;
64
65
    /**
66
     * Slide master id
67
     *
68
     * @var integer
69
     */
70
    private $slideMasterId = 1;
71
72
    /**
73
     *
74
     * @var \PhpOffice\PhpPresentation\Slide\Note
75
     */
76
    private $slideNote;
77
    /**
78
     *
79
     * @var \PhpOffice\PhpPresentation\Slide\Transition
80
     */
81
    private $slideTransition;
82
  
83
    /**
84
     *
85
     * @var \PhpOffice\PhpPresentation\Slide\Animation
86
     */
87
    private $animations = array();
88
    
89
    /**
90
     * Hash index
91
     *
92
     * @var string
93
     */
94
    private $hashIndex;
95
96
    /**
97
     * Offset X
98
     *
99
     * @var int
100
     */
101
    protected $offsetX;
102
103
    /**
104
     * Offset Y
105
     *
106
     * @var int
107
     */
108
    protected $offsetY;
109
110
    /**
111
     * Extent X
112
     *
113
     * @var int
114
     */
115
    protected $extentX;
116
117
    /**
118
     * Extent Y
119
     *
120
     * @var int
121
     */
122
    protected $extentY;
123
124
    /**
125
     * Name of the title
126
     *
127
     * @var string
128
     */
129
    protected $name;
130
131
    /**
132
     * Background of the slide
133
     *
134
     * @var AbstractBackground
135
     */
136
    protected $background;
137 153
138
    /**
139
     * Create a new slide
140 153
     *
141
     * @param PhpPresentation $pParent
142 153
     */
143
    public function __construct(PhpPresentation $pParent = null)
144
    {
145 153
        // Set parent
146
        $this->parent = $pParent;
147
148 153
        $this->slideLayout = Slide\Layout::BLANK;
149 153
150
        // Shape collection
151
        $this->shapeCollection = new \ArrayObject();
152
153
        // Set identifier
154
        $this->identifier = md5(rand(0, 9999) . time());
155
    }
156 121
157
    /**
158 121
     * Get collection of shapes
159
     *
160
     * @return \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[]
161
     */
162
    public function getShapeCollection()
163
    {
164
        return $this->shapeCollection;
165
    }
166
    
167 106
    /**
168
     * Add shape to slide
169 106
     *
170
     * @param  \PhpOffice\PhpPresentation\AbstractShape $shape
171 106
     * @return \PhpOffice\PhpPresentation\AbstractShape
172
     */
173
    public function addShape(AbstractShape $shape)
174
    {
175
        $shape->setContainer($this);
176
177
        return $shape;
178
    }
179 40
180
    /**
181 40
     * Create rich text shape
182 40
     *
183
     * @return \PhpOffice\PhpPresentation\Shape\RichText
184 40
     */
185
    public function createRichTextShape()
186
    {
187
        $shape = new RichText();
188
        $this->addShape($shape);
189
190
        return $shape;
191
    }
192
193
    /**
194
     * Create line shape
195
     *
196 3
     * @param  int                      $fromX Starting point x offset
197
     * @param  int                      $fromY Starting point y offset
198 3
     * @param  int                      $toX   Ending point x offset
199 3
     * @param  int                      $toY   Ending point y offset
200
     * @return \PhpOffice\PhpPresentation\Shape\Line
201 3
     */
202
    public function createLineShape($fromX, $fromY, $toX, $toY)
203
    {
204
        $shape = new Line($fromX, $fromY, $toX, $toY);
205
        $this->addShape($shape);
206
207
        return $shape;
208
    }
209 34
210
    /**
211 34
     * Create chart shape
212 34
     *
213
     * @return \PhpOffice\PhpPresentation\Shape\Chart
214 34
     */
215
    public function createChartShape()
216
    {
217
        $shape = new Chart();
218
        $this->addShape($shape);
219
220
        return $shape;
221
    }
222 11
223
    /**
224 11
     * Create drawing shape
225 11
     *
226
     * @return \PhpOffice\PhpPresentation\Shape\Drawing
227 11
     */
228
    public function createDrawingShape()
229
    {
230
        $shape = new Drawing();
231
        $this->addShape($shape);
232
233
        return $shape;
234
    }
235
236 17
    /**
237
     * Create table shape
238 17
     *
239 17
     * @param  int                       $columns Number of columns
240
     * @return \PhpOffice\PhpPresentation\Shape\Table
241 17
     */
242
    public function createTableShape($columns = 1)
243
    {
244
        $shape = new Table($columns);
245
        $this->addShape($shape);
246
247
        return $shape;
248
    }
249 4
    
250
    /**
251 4
     * Creates a group within this slide
252 4
     *
253
     * @return \PhpOffice\PhpPresentation\Shape\Group
254 4
     */
255
    public function createGroup()
256
    {
257
        $shape = new Group();
258
        $this->addShape($shape);
259
        
260
        return $shape;
261
    }
262 61
263
    /**
264 61
     * Get parent
265
     *
266
     * @return PhpPresentation
267
     */
268
    public function getParent()
269
    {
270
        return $this->parent;
271
    }
272
273 2
    /**
274
     * Re-bind parent
275 2
     *
276 2
     * @param  \PhpOffice\PhpPresentation\PhpPresentation       $parent
277
     * @return \PhpOffice\PhpPresentation\Slide
278 2
     */
279
    public function rebindParent(PhpPresentation $parent)
280
    {
281
        $this->parent->removeSlideByIndex($this->parent->getIndex($this));
282
        $this->parent = $parent;
283
284
        return $this;
285
    }
286 61
287
    /**
288 61
     * Get slide layout
289
     *
290
     * @return string
291
     */
292
    public function getSlideLayout()
293
    {
294
        return $this->slideLayout;
295
    }
296
297 4
    /**
298
     * Set slide layout
299 4
     *
300
     * @param  string              $layout
301 4
     * @return \PhpOffice\PhpPresentation\Slide
302
     */
303
    public function setSlideLayout($layout = Layout::BLANK)
304
    {
305
        $this->slideLayout = $layout;
306
307
        return $this;
308
    }
309 64
310
    /**
311 64
     * Get slide master id
312
     *
313
     * @return int
314
     */
315
    public function getSlideMasterId()
316
    {
317
        return $this->slideMasterId;
318
    }
319
320 1
    /**
321
     * Set slide master id
322 1
     *
323
     * @param  int                 $masterId
324 1
     * @return \PhpOffice\PhpPresentation\Slide
325
     */
326
    public function setSlideMasterId($masterId = 1)
327
    {
328
        $this->slideMasterId = $masterId;
329
330
        return $this;
331
    }
332 85
333
    /**
334 85
     * Get hash code
335
     *
336
     * @return string Hash code
337
     */
338
    public function getHashCode()
339
    {
340
        return md5($this->identifier . __CLASS__);
341
    }
342
343
    /**
344
     * Get hash index
345 4
     *
346
     * Note that this index may vary during script execution! Only reliable moment is
347 4
     * while doing a write of a workbook and when changes are not allowed.
348
     *
349
     * @return string Hash index
350
     */
351
    public function getHashIndex()
352
    {
353
        return $this->hashIndex;
354
    }
355
356
    /**
357
     * Set hash index
358 4
     *
359
     * Note that this index may vary during script execution! Only reliable moment is
360 4
     * while doing a write of a workbook and when changes are not allowed.
361 4
     *
362
     * @param string $value Hash index
363
     */
364
    public function setHashIndex($value)
365
    {
366
        $this->hashIndex = $value;
367
    }
368 1
369
    /**
370 1
     * Copy slide (!= clone!)
371
     *
372 1
     * @return \PhpOffice\PhpPresentation\Slide
373
     */
374
    public function copy()
375
    {
376
        $copied = clone $this;
377
378
        return $copied;
379
    }
380 61
381
    /**
382 61
     * Get X Offset
383 61
     *
384 61
     * @return int
385 61
     */
386
    public function getOffsetX()
387 61
    {
388
        if ($this->offsetX === null) {
389
            $offsets = GeometryCalculator::calculateOffsets($this);
390
            $this->offsetX = $offsets[GeometryCalculator::X];
391
            $this->offsetY = $offsets[GeometryCalculator::Y];
392
        }
393
        return $this->offsetX;
394
    }
395 61
396
    /**
397 61
     * Get Y Offset
398 1
     *
399 1
     * @return int
400 1
     */
401
    public function getOffsetY()
402 61
    {
403
        if ($this->offsetY === null) {
404
            $offsets = GeometryCalculator::calculateOffsets($this);
405
            $this->offsetX = $offsets[GeometryCalculator::X];
406
            $this->offsetY = $offsets[GeometryCalculator::Y];
407
        }
408
        return $this->offsetY;
409
    }
410 61
411
    /**
412 61
     * Get X Extent
413 61
     *
414 61
     * @return int
415 61
     */
416
    public function getExtentX()
417 61
    {
418
        if ($this->extentX === null) {
419
            $extents = GeometryCalculator::calculateExtents($this);
420
            $this->extentX = $extents[GeometryCalculator::X];
421
            $this->extentY = $extents[GeometryCalculator::Y];
422
        }
423
        return $this->extentX;
424
    }
425 61
426
    /**
427 61
     * Get Y Extent
428 1
     *
429 1
     * @return int
430 1
     */
431
    public function getExtentY()
432 61
    {
433
        if ($this->extentY === null) {
434
            $extents = GeometryCalculator::calculateExtents($this);
435
            $this->extentX = $extents[GeometryCalculator::X];
436
            $this->extentY = $extents[GeometryCalculator::Y];
437
        }
438
        return $this->extentY;
439 101
    }
440
441 101
    /**
442 101
     *
443
     * @return \PhpOffice\PhpPresentation\Slide\Note
444 101
     */
445
    public function getNote()
446
    {
447
        if (is_null($this->slideNote)) {
448
            $this->setNote();
449
        }
450
        return $this->slideNote;
451
    }
452 102
453
    /**
454 102
     *
455 102
     * @param \PhpOffice\PhpPresentation\Slide\Note $note
456
     * @return \PhpOffice\PhpPresentation\Slide
457 102
     */
458
    public function setNote(Note $note = null)
459
    {
460
        $this->slideNote = (is_null($note) ? new Note() : $note);
461
        $this->slideNote->setParent($this);
462
463
        return $this;
464 102
    }
465
466 102
    /**
467
     *
468
     * @return \PhpOffice\PhpPresentation\Slide\Transition
469
     */
470
    public function getTransition()
471
    {
472
        return $this->slideTransition;
473
    }
474 3
475
    /**
476 3
     *
477
     * @param \PhpOffice\PhpPresentation\Slide\Transition $transition
478 3
     * @return \PhpOffice\PhpPresentation\Slide
479
     */
480
    public function setTransition(Transition $transition = null)
481
    {
482
        $this->slideTransition = $transition;
483
484
        return $this;
485 43
    }
486
487 43
    /**
488
     * Get the name of the slide
489
     * @return string
490
     */
491
    public function getName()
492
    {
493
        return $this->name;
494
    }
495 5
496
    /**
497 5
     * Set the name of the slide
498 5
     * @param string $name
499
     * @return $this
500
     */
501
    public function setName($name = null)
502
    {
503
        $this->name = $name;
504 102
        return $this;
505
    }
506 102
507
    /**
508
     * @return AbstractBackground
509
     */
510
    public function getBackground()
511
    {
512
        return $this->background;
513 4
    }
514
515 4
    /**
516 4
     * @param AbstractBackground $background
517
     * @return $this
518
     */
519
    public function setBackground(AbstractBackground $background = null)
520
    {
521
        $this->background = $background;
522
        return $this;
523
    }
524
    
525
    /**
526
     * Add an animation to the slide
527
     *
528
     * @param  \PhpOffice\PhpPresentation\Slide\Animation
529
     * @return \PhpOffice\PhpPresentation\Slide\Animation
530
     */
531
    public function addAnimation($animation)
532
    {
533
        $this->animations[] = $animation;
534
        return $animation;
535
    }
536
    
537
    /**
538
     * Get collection of animations
539
     *
540
     * @return \PhpOffice\PhpPresentation\Slide\Animation
541
     */
542
    
543
    public function getAnimations()
544
    {
545
        return $this->animations;
546
    }
547
}
548