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