Completed
Pull Request — develop (#565)
by
unknown
07:15
created

Paragraph::setRichTextElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
     * Hash index
65
     *
66
     * @var string
67
     */
68
    private $hashIndex;
69
70
    /**
71
     * Create a new \PhpOffice\PhpPresentation\Shape\RichText\Paragraph instance
72
     */
73 300
    public function __construct()
74
    {
75
        // Initialise variables
76 300
        $this->richTextElements = array();
77 300
        $this->alignment = new Alignment();
78 300
        $this->font = new Font();
79 300
        $this->bulletStyle = new Bullet();
80 300
    }
81
82
    /**
83
     * Get alignment
84
     *
85
     * @return \PhpOffice\PhpPresentation\Style\Alignment
86
     */
87 235
    public function getAlignment()
88
    {
89 235
        return $this->alignment;
90
    }
91
92
    /**
93
     * Set alignment
94
     *
95
     * @param  \PhpOffice\PhpPresentation\Style\Alignment $alignment
96
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
97
     */
98 13
    public function setAlignment(Alignment $alignment)
99
    {
100 13
        $this->alignment = $alignment;
101
102 13
        return $this;
103
    }
104
105
    /**
106
     * Get font
107
     *
108
     * @return \PhpOffice\PhpPresentation\Style\Font
109
     */
110 236
    public function getFont()
111
    {
112 236
        return $this->font;
113
    }
114
115
    /**
116
     * Set font
117
     *
118
     * @param  \PhpOffice\PhpPresentation\Style\Font $pFont Font
119
     * @throws \Exception
120
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
121
     */
122 13
    public function setFont(Font $pFont = null)
123
    {
124 13
        $this->font = $pFont;
125
126 13
        return $this;
127
    }
128
129
    /**
130
     * Get bullet style
131
     *
132
     * @return \PhpOffice\PhpPresentation\Style\Bullet
133
     */
134 60
    public function getBulletStyle()
135
    {
136 60
        return $this->bulletStyle;
137
    }
138
139
    /**
140
     * Set bullet style
141
     *
142
     * @param  \PhpOffice\PhpPresentation\Style\Bullet $style
143
     * @throws \Exception
144
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
145
     */
146 13
    public function setBulletStyle(Bullet $style = null)
147
    {
148 13
        $this->bulletStyle = $style;
149
150 13
        return $this;
151
    }
152
153
    /**
154
     * Create text (can not be formatted !)
155
     *
156
     * @param  string $pText Text
157
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement
158
     * @throws \Exception
159
     */
160 4
    public function createText($pText = '')
161
    {
162 4
        $objText = new TextElement($pText);
163 4
        $this->addText($objText);
164
165 4
        return $objText;
166
    }
167
168
    /**
169
     * Add text
170
     *
171
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element
172
     * @throws \Exception
173
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
174
     */
175 56
    public function addText(TextElementInterface $pText = null)
176
    {
177 56
        $this->richTextElements[] = $pText;
178
179 56
        return $this;
180
    }
181
182
    /**
183
     * Create break
184
     *
185
     * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement
186
     * @throws \Exception
187
     */
188 12
    public function createBreak()
189
    {
190 12
        $objText = new BreakElement();
191 12
        $this->addText($objText);
192
193 12
        return $objText;
194
    }
195
196
    /**
197
     * Create text run (can be formatted)
198
     *
199
     * @param  string $pText Text
200
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Run
201
     * @throws \Exception
202
     */
203 52
    public function createTextRun($pText = '')
204
    {
205 52
        $objText = new Run($pText);
206 52
        $objText->setFont(clone $this->font);
207 52
        $this->addText($objText);
208
209 52
        return $objText;
210
    }
211
212
    /**
213
     * Convert to string
214
     *
215
     * @return string
216
     */
217 1
    public function __toString()
218
    {
219 1
        return $this->getPlainText();
220
    }
221
222
    /**
223
     * Get plain text
224
     *
225
     * @return string
226
     */
227 3
    public function getPlainText()
228
    {
229
        // Return value
230 3
        $returnValue = '';
231
232
        // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface
233 3
        foreach ($this->richTextElements as $text) {
234 3
            if ($text instanceof TextElementInterface) {
235 3
                $returnValue .= $text->getText();
236
            }
237
        }
238
239
        // Return
240 3
        return $returnValue;
241
    }
242
243
    /**
244
     * Get Rich Text elements
245
     *
246
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[]
247
     */
248 64
    public function getRichTextElements()
249
    {
250 64
        return $this->richTextElements;
251
    }
252
253
    /**
254
     * Set Rich Text elements
255
     *
256
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[] $pElements Array of elements
257
     * @throws \Exception
258
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
259
     */
260 6
    public function setRichTextElements(array $pElements = null)
261
    {
262 6
        if (!is_array($pElements)) {
263 1
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[] array passed.");
264
        }
265 5
        $this->richTextElements = $pElements;
266 5
        return $this;
267
    }
268
269
    /**
270
     * Get hash code
271
     *
272
     * @return string Hash code
273
     */
274 20
    public function getHashCode()
275
    {
276 20
        $hashElements = '';
277 20
        foreach ($this->richTextElements as $element) {
278 10
            $hashElements .= $element->getHashCode();
279
        }
280
281 20
        return md5($hashElements . $this->font->getHashCode() . __CLASS__);
282
    }
283
284
    /**
285
     * Get hash index
286
     *
287
     * Note that this index may vary during script execution! Only reliable moment is
288
     * while doing a write of a workbook and when changes are not allowed.
289
     *
290
     * @return string Hash index
291
     */
292 1
    public function getHashIndex()
293
    {
294 1
        return $this->hashIndex;
295
    }
296
297
    /**
298
     * Set hash index
299
     *
300
     * Note that this index may vary during script execution! Only reliable moment is
301
     * while doing a write of a workbook and when changes are not allowed.
302
     *
303
     * @param string $value Hash index
304
     */
305 1
    public function setHashIndex($value)
306
    {
307 1
        $this->hashIndex = $value;
308 1
    }
309
310
    /**
311
     * @return int
312
     */
313 36
    public function getLineSpacing()
314
    {
315 36
        return $this->lineSpacing;
316
    }
317
318
    /**
319
     * @param int $lineSpacing
320
     * @return Paragraph
321
     */
322 2
    public function setLineSpacing($lineSpacing)
323
    {
324 2
        $this->lineSpacing = $lineSpacing;
325 2
        return $this;
326
    }
327
}
328