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

Cell::createTextRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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