Completed
Pull Request — develop (#335)
by Franck
08:54
created

Cell::getHashIndex()   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\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 39
    public function __construct()
91
    {
92
        // Initialise variables
93 39
        $this->richTextParagraphs = array(
94 39
            new Paragraph()
95 39
        );
96 39
        $this->activeParagraph    = 0;
97
98
        // Set fill
99 39
        $this->fill = new Fill();
100
101
        // Set borders
102 39
        $this->borders = new Borders();
103 39
    }
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
     */
164 1
    public function createParagraph()
165
    {
166 1
        $this->richTextParagraphs[] = new Paragraph();
167
168 1
        if (count($this->richTextParagraphs) > 1) {
169 1
            $alignment = clone $this->getActiveParagraph()->getAlignment();
170 1
            $font = clone $this->getActiveParagraph()->getFont();
171 1
            $bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
172
173 1
            $this->activeParagraph = count($this->richTextParagraphs) - 1;
174
175 1
            $this->getActiveParagraph()->setAlignment($alignment);
176 1
            $this->getActiveParagraph()->setFont($font);
177 1
            $this->getActiveParagraph()->setBulletStyle($bulletStyle);
178 1
        } else {
179
            $this->activeParagraph = count($this->richTextParagraphs) - 1;
180
        }
181
182 1
        return $this->getActiveParagraph();
183
    }
184
185
    /**
186
     * Add text
187
     *
188
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element
189
     * @throws \Exception
190
     * @return \PhpOffice\PhpPresentation\Shape\RichText
191
     */
192 2
    public function addText(TextElementInterface $pText = null)
193
    {
194 2
        $this->richTextParagraphs[$this->activeParagraph]->addText($pText);
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * Create text (can not be formatted !)
201
     *
202
     * @param  string                                   $pText Text
203
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement
204
     * @throws \Exception
205
     */
206 1
    public function createText($pText = '')
207
    {
208 1
        return $this->richTextParagraphs[$this->activeParagraph]->createText($pText);
209
    }
210
211
    /**
212
     * Create break
213
     *
214
     * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement
215
     * @throws \Exception
216
     */
217 2
    public function createBreak()
218
    {
219 2
        return $this->richTextParagraphs[$this->activeParagraph]->createBreak();
220
    }
221
222
    /**
223
     * Create text run (can be formatted)
224
     *
225
     * @param  string                           $pText Text
226
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Run
227
     * @throws \Exception
228
     */
229 12
    public function createTextRun($pText = '')
230
    {
231 12
        return $this->richTextParagraphs[$this->activeParagraph]->createTextRun($pText);
232
    }
233
234
    /**
235
     * Get plain text
236
     *
237
     * @return string
238
     */
239 1
    public function getPlainText()
240
    {
241
        // Return value
242 1
        $returnValue = '';
243
244
        // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
245 1
        foreach ($this->richTextParagraphs as $p) {
246 1
            $returnValue .= $p->getPlainText();
247 1
        }
248
249
        // Return
250 1
        return $returnValue;
251
    }
252
253
    /**
254
     * Convert to string
255
     *
256
     * @return string
257
     */
258 1
    public function __toString()
259
    {
260 1
        return $this->getPlainText();
261
    }
262
263
    /**
264
     * Get paragraphs
265
     *
266
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[]
267
     */
268 19
    public function getParagraphs()
269
    {
270 19
        return $this->richTextParagraphs;
271
    }
272
273
    /**
274
     * Set paragraphs
275
     *
276
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs
277
     * @throws \Exception
278
     * @return \PhpOffice\PhpPresentation\Shape\RichText
279
     */
280 3
    public function setParagraphs($paragraphs = null)
281
    {
282 3
        if (is_array($paragraphs)) {
283 2
            $this->richTextParagraphs = $paragraphs;
284 2
            $this->activeParagraph    = count($this->richTextParagraphs) - 1;
285 2
        } else {
286 1
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.");
287
        }
288
289 2
        return $this;
290
    }
291
292
    /**
293
     * Get fill
294
     *
295
     * @return \PhpOffice\PhpPresentation\Style\Fill
296
     */
297 18
    public function getFill()
298
    {
299 18
        return $this->fill;
300
    }
301
302
    /**
303
     * Set fill
304
     *
305
     * @param  \PhpOffice\PhpPresentation\Style\Fill     $fill
306
     * @return \PhpOffice\PhpPresentation\Shape\RichText
307
     */
308 5
    public function setFill(Fill $fill)
309
    {
310 5
        $this->fill = $fill;
311
312 5
        return $this;
313
    }
314
315
    /**
316
     * Get borders
317
     *
318
     * @return \PhpOffice\PhpPresentation\Style\Borders
319
     */
320 18
    public function getBorders()
321
    {
322 18
        return $this->borders;
323
    }
324
325
    /**
326
     * Set borders
327
     *
328
     * @param  \PhpOffice\PhpPresentation\Style\Borders  $borders
329
     * @return \PhpOffice\PhpPresentation\Shape\RichText
330
     */
331 1
    public function setBorders(Borders $borders)
332
    {
333 1
        $this->borders = $borders;
334
335 1
        return $this;
336
    }
337
338
    /**
339
     * Get width
340
     *
341
     * @return int
342
     */
343 11
    public function getWidth()
344
    {
345 11
        return $this->width;
346
    }
347
348
    /**
349
     * Set width
350
     *
351
     * @param  int                          $value
352
     * @return \PhpOffice\PhpPresentation\Shape\RichText
353
     */
354 1
    public function setWidth($value = 0)
355
    {
356 1
        $this->width = $value;
357
358 1
        return $this;
359
    }
360
361
    /**
362
     * Get colSpan
363
     *
364
     * @return int
365
     */
366 17
    public function getColSpan()
367
    {
368 17
        return $this->colSpan;
369
    }
370
371
    /**
372
     * Set colSpan
373
     *
374
     * @param  int                          $value
375
     * @return \PhpOffice\PhpPresentation\Shape\RichText
376
     */
377 3
    public function setColSpan($value = 0)
378
    {
379 3
        $this->colSpan = $value;
380
381 3
        return $this;
382
    }
383
384
    /**
385
     * Get rowSpan
386
     *
387
     * @return int
388
     */
389 11
    public function getRowSpan()
390
    {
391 11
        return $this->rowSpan;
392
    }
393
394
    /**
395
     * Set rowSpan
396
     *
397
     * @param  int                          $value
398
     * @return \PhpOffice\PhpPresentation\Shape\RichText
399
     */
400 2
    public function setRowSpan($value = 0)
401
    {
402 2
        $this->rowSpan = $value;
403
404 2
        return $this;
405
    }
406
407
    /**
408
     * Get hash code
409
     *
410
     * @return string Hash code
411
     */
412 1
    public function getHashCode()
413
    {
414 1
        $hashElements = '';
415 1
        foreach ($this->richTextParagraphs as $element) {
416 1
            $hashElements .= $element->getHashCode();
417 1
        }
418
419 1
        return md5($hashElements . $this->fill->getHashCode() . $this->borders->getHashCode() . $this->width . __CLASS__);
420
    }
421
422
    /**
423
     * Get hash index
424
     *
425
     * Note that this index may vary during script execution! Only reliable moment is
426
     * while doing a write of a workbook and when changes are not allowed.
427
     *
428
     * @return string Hash index
429
     */
430 1
    public function getHashIndex()
431
    {
432 1
        return $this->hashIndex;
433
    }
434
435
    /**
436
     * Set hash index
437
     *
438
     * Note that this index may vary during script execution! Only reliable moment is
439
     * while doing a write of a workbook and when changes are not allowed.
440
     *
441
     * @param string $value Hash index
442
     */
443 1
    public function setHashIndex($value)
444
    {
445 1
        $this->hashIndex = $value;
446 1
    }
447
}
448