Completed
Pull Request — develop (#455)
by
unknown
10:09
created

RichText::getInsetLeft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Shape;
19
20
use PhpOffice\PhpPresentation\AbstractShape;
21
use PhpOffice\PhpPresentation\ComparableInterface;
22
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
23
use PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface;
24
25
/**
26
 * \PhpOffice\PhpPresentation\Shape\RichText
27
 */
28
class RichText extends AbstractShape implements ComparableInterface
29
{
30
    /** Wrapping */
31
    const WRAP_NONE = 'none';
32
    const WRAP_SQUARE = 'square';
33
34
    /** Autofit */
35
    const AUTOFIT_DEFAULT = 'spAutoFit';
36
    const AUTOFIT_SHAPE = 'spAutoFit';
37
    const AUTOFIT_NOAUTOFIT = 'noAutofit';
38
    const AUTOFIT_NORMAL = 'normAutofit';
39
40
    /** Overflow */
41
    const OVERFLOW_CLIP = 'clip';
42
    const OVERFLOW_OVERFLOW = 'overflow';
43
44
    /**
45
     * Rich text paragraphs
46
     *
47
     * @var \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[]
48
     */
49
    private $richTextParagraphs;
50
51
    /**
52
     * Active paragraph
53
     *
54
     * @var int
55
     */
56
    private $activeParagraph = 0;
57
58
    /**
59
     * Text wrapping
60
     *
61
     * @var string
62
     */
63
    private $wrap = self::WRAP_SQUARE;
64
65
    /**
66
     * Autofit
67
     *
68
     * @var string
69
     */
70
    private $autoFit = self::AUTOFIT_DEFAULT;
71
72
    /**
73
     * Horizontal overflow
74
     *
75
     * @var string
76
     */
77
    private $horizontalOverflow = self::OVERFLOW_OVERFLOW;
78
79
    /**
80
     * Vertical overflow
81
     *
82
     * @var string
83
     */
84
    private $verticalOverflow = self::OVERFLOW_OVERFLOW;
85
86
    /**
87
     * Text upright?
88
     *
89
     * @var boolean
90
     */
91
    private $upright = false;
92
93
    /**
94
     * Vertical text?
95
     *
96
     * @var boolean
97
     */
98
    private $vertical = false;
99
100
    /**
101
     * Number of columns (1 - 16)
102
     *
103
     * @var int
104
     */
105
    private $columns = 1;
106
107
    /**
108
     * Bottom inset (in pixels)
109
     *
110
     * @var float
111
     */
112
    private $bottomInset = 4.8;
113
114
    /**
115
     * Left inset (in pixels)
116
     *
117
     * @var float
118
     */
119
    private $leftInset = 9.6;
120
121
    /**
122
     * Right inset (in pixels)
123
     *
124
     * @var float
125
     */
126
    private $rightInset = 9.6;
127
128
    /**
129
     * Top inset (in pixels)
130
     *
131
     * @var float
132
     */
133
    private $topInset = 4.8;
134
135
    /**
136
     * Horizontal Auto Shrink
137
     * @var boolean
138
     */
139
    private $autoShrinkHorizontal;
140
141
    /**
142
     * Vertical Auto Shrink
143
     * @var boolean
144
     */
145
    private $autoShrinkVertical;
146
    
147
    /**
148
     * The percentage of the original font size to which the text is scaled
149
     * @var float
150
     */
151
    private $fontScale;
152
    
153
    /**
154
     * The percentage of the reduction of the line spacing
155
     * @var float
156
     */
157
    private $lnSpcReduction;
158
159
160
    private $bodyPr;
161
162
    /**
163
     * Create a new \PhpOffice\PhpPresentation\Shape\RichText instance
164
     */
165 82
    public function __construct()
166
    {
167
        // Initialise variables
168 82
        $this->richTextParagraphs = array(
169 82
            new Paragraph()
170 82
        );
171 82
        $this->activeParagraph    = 0;
172
173
        // Initialize parent
174 82
        parent::__construct();
175 82
    }
176
177
    /**
178
     * Get active paragraph index
179
     *
180
     * @return int
181
     */
182 3
    public function getActiveParagraphIndex()
183
    {
184 3
        return $this->activeParagraph;
185
    }
186
187
    /**
188
     * Get active paragraph
189
     *
190
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
191
     */
192 39
    public function getActiveParagraph()
193
    {
194 39
        return $this->richTextParagraphs[$this->activeParagraph];
195
    }
196
197
    /**
198
     * Set active paragraph
199
     *
200
     * @param  int $index
201
     * @throws \Exception
202
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
203
     */
204 4
    public function setActiveParagraph($index = 0)
205
    {
206 4
        if ($index >= count($this->richTextParagraphs)) {
207 1
            throw new \Exception("Invalid paragraph count.");
208
        }
209
210 3
        $this->activeParagraph = $index;
211
212 3
        return $this->getActiveParagraph();
213
    }
214
215
    /**
216
     * Get paragraph
217
     *
218
     * @param  int $index
219
     * @throws \Exception
220
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
221
     */
222 3
    public function getParagraph($index = 0)
223
    {
224 3
        if ($index >= count($this->richTextParagraphs)) {
225 1
            throw new \Exception("Invalid paragraph count.");
226
        }
227
228 2
        return $this->richTextParagraphs[$index];
229
    }
230
231
    /**
232
     * Create paragraph
233
     *
234
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
235
     */
236 8
    public function createParagraph()
237
    {
238 8
        $numParagraphs = count($this->richTextParagraphs);
239 8
        if ($numParagraphs > 0) {
240 7
            $alignment   = clone $this->getActiveParagraph()->getAlignment();
241 7
            $font        = clone $this->getActiveParagraph()->getFont();
242 7
            $bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
243 7
        }
244
245 8
        $this->richTextParagraphs[] = new Paragraph();
246 8
        $this->activeParagraph      = count($this->richTextParagraphs) - 1;
247
248 8
        if (isset($alignment)) {
249 7
            $this->getActiveParagraph()->setAlignment($alignment);
250 7
        }
251 8
        if (isset($font)) {
252 7
            $this->getActiveParagraph()->setFont($font);
253 7
        }
254 8
        if (isset($bulletStyle)) {
255 7
            $this->getActiveParagraph()->setBulletStyle($bulletStyle);
256 7
        }
257 8
        return $this->getActiveParagraph();
258
    }
259
260
    /**
261
     * Add text
262
     *
263
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element
264
     * @throws \Exception
265
     * @return \PhpOffice\PhpPresentation\Shape\RichText
266
     */
267 1
    public function addText(TextElementInterface $pText = null)
268
    {
269 1
        $this->richTextParagraphs[$this->activeParagraph]->addText($pText);
270
271 1
        return $this;
272
    }
273
274
    /**
275
     * Create text (can not be formatted !)
276
     *
277
     * @param  string                                   $pText Text
278
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement
279
     * @throws \Exception
280
     */
281 2
    public function createText($pText = '')
282
    {
283 2
        return $this->richTextParagraphs[$this->activeParagraph]->createText($pText);
284
    }
285
286
    /**
287
     * Create break
288
     *
289
     * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement
290
     * @throws \Exception
291
     */
292 4
    public function createBreak()
293
    {
294 4
        return $this->richTextParagraphs[$this->activeParagraph]->createBreak();
295
    }
296
297
    /**
298
     * Create text run (can be formatted)
299
     *
300
     * @param  string                           $pText Text
301
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Run
302
     * @throws \Exception
303
     */
304 33
    public function createTextRun($pText = '')
305
    {
306 33
        return $this->richTextParagraphs[$this->activeParagraph]->createTextRun($pText);
307
    }
308
309
    /**
310
     * Get plain text
311
     *
312
     * @return string
313
     */
314 1
    public function getPlainText()
315
    {
316
        // Return value
317 1
        $returnValue = '';
318
319
        // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
320 1
        foreach ($this->richTextParagraphs as $p) {
321 1
            $returnValue .= $p->getPlainText();
322 1
        }
323
324
        // Return
325 1
        return $returnValue;
326
    }
327
328
    /**
329
     * Convert to string
330
     *
331
     * @return string
332
     */
333 1
    public function __toString()
334
    {
335 1
        return $this->getPlainText();
336
    }
337
338
    /**
339
     * Get paragraphs
340
     *
341
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[]
342
     */
343 47
    public function getParagraphs()
344
    {
345 47
        return $this->richTextParagraphs;
346
    }
347
348
    /**
349
     * Set paragraphs
350
     *
351
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs
352
     * @throws \Exception
353
     * @return \PhpOffice\PhpPresentation\Shape\RichText
354
     */
355 5
    public function setParagraphs($paragraphs = null)
356
    {
357 5
        if (!is_array($paragraphs)) {
358 1
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.");
359
        }
360
361 4
        $this->richTextParagraphs = $paragraphs;
362 4
        $this->activeParagraph    = count($this->richTextParagraphs) - 1;
363 4
        return $this;
364
    }
365
366
    /**
367
     * Get text wrapping
368
     *
369
     * @return string
370
     */
371 27
    public function getWrap()
372
    {
373 27
        return $this->wrap;
374
    }
375
376
    /**
377
     * Set text wrapping
378
     *
379
     * @param $value string
380
     * @return \PhpOffice\PhpPresentation\Shape\RichText
381
     */
382 2
    public function setWrap($value = self::WRAP_SQUARE)
383
    {
384 2
        $this->wrap = $value;
385
386 2
        return $this;
387
    }
388
389
    /**
390
     * Get autofit
391
     *
392
     * @return string
393
     */
394 27
    public function getAutoFit()
395
    {
396 27
        return $this->autoFit;
397
    }
398
399
    /**
400
     * Get pourcentage of fontScale
401
     *
402
     * @return float
403
     */
404 1
    public function getFontScale()
405
    {
406 1
        return $this->fontScale;
407
    }
408
409
    /**
410
     * Get pourcentage of the line space reduction
411
     *
412
     * @return float
413
     */
414 1
    public function getLineSpaceReduction()
415
    {
416 1
        return $this->lnSpcReduction;
417
    }
418
419
    /**
420
     * Set autofit
421
     *
422
     * @param $value string
423
     * @param $fontScale float
424
     * @param $lnSpcReduction float
425
     * @return \PhpOffice\PhpPresentation\Shape\RichText
426
     */
427 2
    public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null)
428
    {
429 2
        $this->autoFit = $value;
430
        
431 2
        if (!is_null($fontScale)) {
432 1
            $this->fontScale = $fontScale;
433 1
        }
434
        
435 2
        if (!is_null($lnSpcReduction)) {
436 1
            $this->lnSpcReduction = $lnSpcReduction;
437 1
        }
438
439 2
        return $this;
440
    }
441
442
    /**
443
     * Get horizontal overflow
444
     *
445
     * @return string
446
     */
447 27
    public function getHorizontalOverflow()
448
    {
449 27
        return $this->horizontalOverflow;
450
    }
451
452
    /**
453
     * Set horizontal overflow
454
     *
455
     * @param $value string
456
     * @return \PhpOffice\PhpPresentation\Shape\RichText
457
     */
458 1
    public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW)
459
    {
460 1
        $this->horizontalOverflow = $value;
461
462 1
        return $this;
463
    }
464
465
    /**
466
     * Get vertical overflow
467
     *
468
     * @return string
469
     */
470 27
    public function getVerticalOverflow()
471
    {
472 27
        return $this->verticalOverflow;
473
    }
474
475
    /**
476
     * Set vertical overflow
477
     *
478
     * @param $value string
479
     * @return \PhpOffice\PhpPresentation\Shape\RichText
480
     */
481 1
    public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW)
482
    {
483 1
        $this->verticalOverflow = $value;
484
485 1
        return $this;
486
    }
487
488
    /**
489
     * Get upright
490
     *
491
     * @return boolean
492
     */
493 27
    public function isUpright()
494
    {
495 27
        return $this->upright;
496
    }
497
498
    /**
499
     * Set vertical
500
     *
501
     * @param $value boolean
502
     * @return \PhpOffice\PhpPresentation\Shape\RichText
503
     */
504 2
    public function setUpright($value = false)
505
    {
506 2
        $this->upright = $value;
507
508 2
        return $this;
509
    }
510
511
    /**
512
     * Get vertical
513
     *
514
     * @return boolean
515
     */
516 27
    public function isVertical()
517
    {
518 27
        return $this->vertical;
519
    }
520
521
    /**
522
     * Set vertical
523
     *
524
     * @param $value boolean
525
     * @return \PhpOffice\PhpPresentation\Shape\RichText
526
     */
527 2
    public function setVertical($value = false)
528
    {
529 2
        $this->vertical = $value;
530
531 2
        return $this;
532
    }
533
534
    /**
535
     * Get columns
536
     *
537
     * @return int
538
     */
539 27
    public function getColumns()
540
    {
541 27
        return $this->columns;
542
    }
543
544
    /**
545
     * Set columns
546
     *
547
     * @param $value int
548
     * @throws \Exception
549
     * @return \PhpOffice\PhpPresentation\Shape\RichText
550
     */
551 2
    public function setColumns($value = 1)
552
    {
553 2
        if ($value > 16 || $value < 1) {
554 1
            throw new \Exception('Number of columns should be 1-16');
555
        }
556
557 1
        $this->columns = $value;
558
559 1
        return $this;
560
    }
561
562
    /**
563
     * Get bottom inset
564
     *
565
     * @return float
566
     */
567 27
    public function getInsetBottom()
568
    {
569 27
        return $this->bottomInset;
570
    }
571
572
    /**
573
     * Set bottom inset
574
     *
575
     * @param $value float
576
     * @return \PhpOffice\PhpPresentation\Shape\RichText
577
     */
578 5
    public function setInsetBottom($value = 4.8)
579
    {
580 5
        $this->bottomInset = $value;
581
582 5
        return $this;
583
    }
584
585
    /**
586
     * Get left inset
587
     *
588
     * @return float
589
     */
590 27
    public function getInsetLeft()
591
    {
592 27
        return $this->leftInset;
593
    }
594
595
    /**
596
     * Set left inset
597
     *
598
     * @param $value float
599
     * @return \PhpOffice\PhpPresentation\Shape\RichText
600
     */
601 5
    public function setInsetLeft($value = 9.6)
602
    {
603 5
        $this->leftInset = $value;
604
605 5
        return $this;
606
    }
607
608
    /**
609
     * Get right inset
610
     *
611
     * @return float
612
     */
613 27
    public function getInsetRight()
614
    {
615 27
        return $this->rightInset;
616
    }
617
618
    /**
619
     * Set left inset
620
     *
621
     * @param $value float
622
     * @return \PhpOffice\PhpPresentation\Shape\RichText
623
     */
624 5
    public function setInsetRight($value = 9.6)
625
    {
626 5
        $this->rightInset = $value;
627
628 5
        return $this;
629
    }
630
631
    /**
632
     * Get top inset
633
     *
634
     * @return float
635
     */
636 27
    public function getInsetTop()
637
    {
638 27
        return $this->topInset;
639
    }
640
641
    /**
642
     * Set top inset
643
     *
644
     * @param $value float
645
     * @return \PhpOffice\PhpPresentation\Shape\RichText
646
     */
647 5
    public function setInsetTop($value = 4.8)
648
    {
649 5
        $this->topInset = $value;
650
651 5
        return $this;
652
    }
653
654
    /**
655
     * Set horizontal auto shrink
656
     * @param bool $value
657
     */
658 2
    public function setAutoShrinkHorizontal($value = null)
659
    {
660 2
        if (is_bool($value)) {
661 2
            $this->autoShrinkHorizontal = $value;
662 2
        }
663 2
        return $this;
664
    }
665
    
666
    /**
667
     * Get horizontal auto shrink
668
     * @return bool
669
     */
670 15
    public function hasAutoShrinkHorizontal()
671
    {
672 15
        return $this->autoShrinkHorizontal;
673
    }
674
    
675
    /**
676
     * Set vertical auto shrink
677
     * @param bool $value
678
     */
679 2
    public function setAutoShrinkVertical($value = null)
680
    {
681 2
        if (is_bool($value)) {
682 2
            $this->autoShrinkVertical = $value;
683 2
        }
684 2
        return $this;
685
    }
686
    
687
    /**
688
     * Set vertical auto shrink
689
     * @return bool
690
     */
691 15
    public function hasAutoShrinkVertical()
692
    {
693 15
        return $this->autoShrinkVertical;
694
    }
695
    
696
    /**
697
     * Get hash code
698
     *
699
     * @return string Hash code
700
     */
701 2
    public function getHashCode()
702
    {
703 2
        $hashElements = '';
704 2
        foreach ($this->richTextParagraphs as $element) {
705 2
            $hashElements .= $element->getHashCode();
706 2
        }
707
708 2
        return md5($hashElements . $this->wrap . $this->autoFit . $this->horizontalOverflow . $this->verticalOverflow . ($this->upright ? '1' : '0') . ($this->vertical ? '1' : '0') . $this->columns . $this->bottomInset . $this->leftInset . $this->rightInset . $this->topInset . parent::getHashCode() . __CLASS__);
709
    }
710
711
    public function setBodyPr(\DOMElement $element)
712
    {
713
        $this->bodyPr = $element;
714
    }
715
716
    public function getBodyPr()
717
    {
718
        return $this->bodyPr;
719
    }
720
721
    public function setListStyle(\DOMElement $element)
722
    {
723
        $this->listStyle = $element;
0 ignored issues
show
Bug introduced by
The property listStyle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
724
    }
725
726 26
    public function getListStyle()
727
    {
728 26
        return $this->listStyle;
729
    }
730
}
731