Completed
Push — develop ( 5764b5...f5141e )
by Franck
11:46 queued 11:36
created

Slide::getExtentX()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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