Completed
Pull Request — develop (#617)
by
unknown
18:35
created

Paragraph::setSpacingBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
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\Shape\RichText;
19
20
use PhpOffice\PhpPresentation\ComparableInterface;
21
use PhpOffice\PhpPresentation\Style\Alignment;
22
use PhpOffice\PhpPresentation\Style\Bullet;
23
use PhpOffice\PhpPresentation\Style\Font;
24
25
/**
26
 * \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
27
 */
28
class Paragraph implements ComparableInterface
29
{
30
    /**
31
     * Rich text elements
32
     *
33
     * @var \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[]
34
     */
35
    private $richTextElements;
36
37
    /**
38
     * Alignment
39
     *
40
     * @var \PhpOffice\PhpPresentation\Style\Alignment
41
     */
42
    private $alignment;
43
44
    /**
45
     * Font
46
     *
47
     * @var \PhpOffice\PhpPresentation\Style\Font
48
     */
49
    private $font;
50
51
    /**
52
     * Bullet style
53
     *
54
     * @var \PhpOffice\PhpPresentation\Style\Bullet
55
     */
56
    private $bulletStyle;
57
58
    /**
59
     * @var integer
60
     */
61
    private $lineSpacing = 100;
62
63
    /**
64
     * @var boolean
65
     */
66
    private $lineSpacingModeExact = false;
67
68
    /**
69
     * @var float
70
     */
71
    private $spacingBefore = 0;
72
73 300
    /**
74
     * @var float
75
     */
76 300
    private $spacingAfter = 0;
77 300
78 300
    /**
79 300
     * Hash index
80 300
     *
81
     * @var string
82
     */
83
    private $hashIndex;
84
85
    /**
86
     * Create a new \PhpOffice\PhpPresentation\Shape\RichText\Paragraph instance
87 235
     */
88
    public function __construct()
89 235
    {
90
        // Initialise variables
91
        $this->richTextElements = array();
92
        $this->alignment = new Alignment();
93
        $this->font = new Font();
94
        $this->bulletStyle = new Bullet();
95
    }
96
97
    /**
98 13
     * Get alignment
99
     *
100 13
     * @return \PhpOffice\PhpPresentation\Style\Alignment
101
     */
102 13
    public function getAlignment()
103
    {
104
        return $this->alignment;
105
    }
106
107
    /**
108
     * Set alignment
109
     *
110 236
     * @param  \PhpOffice\PhpPresentation\Style\Alignment $alignment
111
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
112 236
     */
113
    public function setAlignment(Alignment $alignment)
114
    {
115
        $this->alignment = $alignment;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Get font
122 13
     *
123
     * @return \PhpOffice\PhpPresentation\Style\Font
124 13
     */
125
    public function getFont()
126 13
    {
127
        return $this->font;
128
    }
129
130
    /**
131
     * Set font
132
     *
133
     * @param  \PhpOffice\PhpPresentation\Style\Font $pFont Font
134 60
     * @throws \Exception
135
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
136 60
     */
137
    public function setFont(Font $pFont = null)
138
    {
139
        $this->font = $pFont;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Get bullet style
146 13
     *
147
     * @return \PhpOffice\PhpPresentation\Style\Bullet
148 13
     */
149
    public function getBulletStyle()
150 13
    {
151
        return $this->bulletStyle;
152
    }
153
154
    /**
155
     * Set bullet style
156
     *
157
     * @param  \PhpOffice\PhpPresentation\Style\Bullet $style
158
     * @throws \Exception
159
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
160 4
     */
161
    public function setBulletStyle(Bullet $style = null)
162 4
    {
163 4
        $this->bulletStyle = $style;
164
165 4
        return $this;
166
    }
167
168
    /**
169
     * Create text (can not be formatted !)
170
     *
171
     * @param  string $pText Text
172
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement
173
     * @throws \Exception
174
     */
175 56
    public function createText($pText = '')
176
    {
177 56
        $objText = new TextElement($pText);
178
        $this->addText($objText);
179 56
180
        return $objText;
181
    }
182
183
    /**
184
     * Add text
185
     *
186
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element
187
     * @throws \Exception
188 12
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
189
     */
190 12
    public function addText(TextElementInterface $pText = null)
191 12
    {
192
        $this->richTextElements[] = $pText;
193 12
194
        return $this;
195
    }
196
197
    /**
198
     * Create break
199
     *
200
     * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement
201
     * @throws \Exception
202
     */
203 52
    public function createBreak()
204
    {
205 52
        $objText = new BreakElement();
206 52
        $this->addText($objText);
207 52
208
        return $objText;
209 52
    }
210
211
    /**
212
     * Create text run (can be formatted)
213
     *
214
     * @param  string $pText Text
215
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Run
216
     * @throws \Exception
217 1
     */
218
    public function createTextRun($pText = '')
219 1
    {
220
        $objText = new Run($pText);
221
        $objText->setFont(clone $this->font);
222
        $this->addText($objText);
223
224
        return $objText;
225
    }
226
227 3
    /**
228
     * Convert to string
229
     *
230 3
     * @return string
231
     */
232
    public function __toString()
233 3
    {
234 3
        return $this->getPlainText();
235 3
    }
236
237
    /**
238
     * Get plain text
239
     *
240 3
     * @return string
241
     */
242
    public function getPlainText()
243
    {
244
        // Return value
245
        $returnValue = '';
246
247
        // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface
248 64
        foreach ($this->richTextElements as $text) {
249
            if ($text instanceof TextElementInterface) {
250 64
                $returnValue .= $text->getText();
251
            }
252
        }
253
254
        // Return
255
        return $returnValue;
256
    }
257
258
    /**
259
     * Get Rich Text elements
260 6
     *
261
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[]
262 6
     */
263 1
    public function getRichTextElements()
264
    {
265 5
        return $this->richTextElements;
266 5
    }
267
268
    /**
269
     * Set Rich Text elements
270
     *
271
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[] $pElements Array of elements
272
     * @throws \Exception
273
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
274 20
     */
275
    public function setRichTextElements($pElements = null)
276 20
    {
277 20
        if (!is_array($pElements)) {
278 10
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[] array passed.");
279
        }
280
        $this->richTextElements = $pElements;
281 20
        return $this;
282
    }
283
284
    /**
285
     * Get hash code
286
     *
287
     * @return string Hash code
288
     */
289
    public function getHashCode()
290
    {
291
        $hashElements = '';
292 1
        foreach ($this->richTextElements as $element) {
293
            $hashElements .= $element->getHashCode();
294 1
        }
295
296
        return md5($hashElements . $this->font->getHashCode() . __CLASS__);
297
    }
298
299
    /**
300
     * Get hash index
301
     *
302
     * Note that this index may vary during script execution! Only reliable moment is
303
     * while doing a write of a workbook and when changes are not allowed.
304
     *
305 1
     * @return string Hash index
306
     */
307 1
    public function getHashIndex()
308 1
    {
309
        return $this->hashIndex;
310
    }
311
312
    /**
313 36
     * Set hash index
314
     *
315 36
     * Note that this index may vary during script execution! Only reliable moment is
316
     * while doing a write of a workbook and when changes are not allowed.
317
     *
318
     * @param string $value Hash index
319
     */
320
    public function setHashIndex($value)
321
    {
322 2
        $this->hashIndex = $value;
323
    }
324 2
325 2
    /**
326
     * @return int
327
     */
328
    public function getLineSpacing()
329
    {
330
        return $this->lineSpacing;
331
    }
332
333
    /**
334
     * @param int $lineSpacing
335
     * @return Paragraph
336
     */
337
    public function setLineSpacing($lineSpacing)
338
    {
339
        $this->lineSpacing = $lineSpacing;
340
        return $this;
341
    }
342
343
    /**
344
     * @return boolean
345
     */
346
    public function getLineSpacingModeExact()
347
    {
348
        return $this->lineSpacingModeExact;
349
    }
350
351
    /**
352
     * @param boolean $lineSpacingModeExact
353
     * @return Paragraph
354
     */
355
    public function setLineSpacingModeExact($lineSpacingModeExact)
356
    {
357
        $this->lineSpacingModeExact = $lineSpacingModeExact;
358
        return $this;
359
    }
360
361
    /**
362
     * @return float
363
     */
364
    public function getSpacingBefore()
365
    {
366
        return $this->spacingBefore;
367
    }
368
369
    /**
370
     * @param float $spacingBefore
371
     * @return Paragraph
372
     */
373
    public function setSpacingBefore($spacingBefore)
374
    {
375
        $this->spacingBefore = $spacingBefore;
376
        return $this;
377
    }
378
379
    /**
380
     * @return float
381
     */
382
    public function getSpacingAfter()
383
    {
384
        return $this->spacingAfter;
385
    }
386
387
    /**
388
     * @param float $spacingAfter
389
     * @return Paragraph
390
     */
391
    public function setSpacingAfter($spacingAfter)
392
    {
393
        $this->spacingAfter = $spacingAfter;
394
        return $this;
395
    }
396
}
397