Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
111 / 111
100.00% covered (success)
100.00%
49 / 49
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
111 / 111
100.00% covered (success)
100.00%
49 / 49
59
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 setCellSpacing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellSpacing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFirstRow
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBgColor
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setBgColor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getBorderSize
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 setBorderSize
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getBorderColor
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 setBorderColor
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getBorderInsideHSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBorderInsideHSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBorderInsideHColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBorderInsideHColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBorderInsideVSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBorderInsideVSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBorderInsideVColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBorderInsideVColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellMarginTop
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCellMarginTop
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellMarginLeft
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCellMarginLeft
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellMarginRight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCellMarginRight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellMarginBottom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCellMarginBottom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCellMargin
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 setCellMargin
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 hasMargin
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getShading
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setShading
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAlignment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAlignment
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 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
 getUnit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUnit
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getLayout
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLayout
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTableOnlyProperty
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setTableOnlyProperty
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getPosition
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPosition
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getIndent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIndent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getColumnWidths
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setColumnWidths
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isBidiVisual
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBidiVisual
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Style;
20
21use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
22use PhpOffice\PhpWord\Settings;
23use PhpOffice\PhpWord\SimpleType\Jc;
24use PhpOffice\PhpWord\SimpleType\JcTable;
25use PhpOffice\PhpWord\SimpleType\TblWidth;
26use PhpOffice\PhpWord\Style;
27
28class Table extends Border
29{
30    //values for http://www.datypic.com/sc/ooxml/t-w_ST_TblLayoutType.html
31    /**
32     * AutoFit Table Layout.
33     *
34     * @var string
35     */
36    const LAYOUT_AUTO = 'autofit';
37    /**
38     * Fixed Width Table Layout.
39     *
40     * @var string
41     */
42    const LAYOUT_FIXED = 'fixed';
43
44    /**
45     * Is this a first row style?
46     *
47     * @var bool
48     */
49    private $isFirstRow = false;
50
51    /**
52     * Style for first row.
53     *
54     * @var Table
55     */
56    private $firstRowStyle;
57
58    /**
59     * Cell margin top.
60     *
61     * @var int
62     */
63    private $cellMarginTop;
64
65    /**
66     * Cell margin left.
67     *
68     * @var int
69     */
70    private $cellMarginLeft;
71
72    /**
73     * Cell margin right.
74     *
75     * @var int
76     */
77    private $cellMarginRight;
78
79    /**
80     * Cell margin bottom.
81     *
82     * @var int
83     */
84    private $cellMarginBottom;
85
86    /**
87     * Border size inside horizontal.
88     *
89     * @var int
90     */
91    private $borderInsideHSize;
92
93    /**
94     * Border color inside horizontal.
95     *
96     * @var string
97     */
98    private $borderInsideHColor;
99
100    /**
101     * Border size inside vertical.
102     *
103     * @var int
104     */
105    private $borderInsideVSize;
106
107    /**
108     * Border color inside vertical.
109     *
110     * @var string
111     */
112    private $borderInsideVColor;
113
114    /**
115     * Shading.
116     *
117     * @var Shading
118     */
119    private $shading;
120
121    /**
122     * @var string
123     */
124    private $alignment = '';
125
126    /**
127     * @var float|int Width value
128     */
129    private $width = 0;
130
131    /**
132     * @var string Width unit
133     */
134    private $unit = TblWidth::AUTO;
135
136    /**
137     * @var null|float|int cell spacing value
138     */
139    protected $cellSpacing;
140
141    /**
142     * @var string Table Layout
143     */
144    private $layout = self::LAYOUT_AUTO;
145
146    /**
147     * Position.
148     *
149     * @var ?TablePosition
150     */
151    private $position;
152
153    /** @var null|TblWidthComplexType */
154    private $indent;
155
156    /**
157     * The width of each column, computed based on the max cell width of each column.
158     *
159     * @var int[]
160     */
161    private $columnWidths;
162
163    /**
164     * Visually Right to Left Table.
165     *
166     * @see  http://www.datypic.com/sc/ooxml/e-w_bidiVisual-1.html
167     *
168     * @var ?bool
169     */
170    private $bidiVisual;
171
172    /**
173     * Create new table style.
174     *
175     * @param mixed $tableStyle
176     * @param mixed $firstRowStyle
177     */
178    public function __construct($tableStyle = null, $firstRowStyle = null)
179    {
180        // Clone first row from table style, but with certain properties disabled
181        if ($firstRowStyle !== null && is_array($firstRowStyle)) {
182            $this->firstRowStyle = clone $this;
183            $this->firstRowStyle->isFirstRow = true;
184            unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom, $this->firstRowStyle->cellSpacing);
185            $this->firstRowStyle->setStyleByArray($firstRowStyle);
186        }
187
188        if ($tableStyle !== null && is_array($tableStyle)) {
189            $this->setStyleByArray($tableStyle);
190        }
191    }
192
193    /**
194     * @param null|float|int $cellSpacing
195     */
196    public function setCellSpacing($cellSpacing = null): void
197    {
198        $this->cellSpacing = $cellSpacing;
199    }
200
201    /**
202     * @return null|float|int
203     */
204    public function getCellSpacing()
205    {
206        return $this->cellSpacing;
207    }
208
209    /**
210     * Set first row.
211     *
212     * @return Table
213     */
214    public function getFirstRow()
215    {
216        return $this->firstRowStyle;
217    }
218
219    /**
220     * Get background.
221     *
222     * @return ?string
223     */
224    public function getBgColor()
225    {
226        if ($this->shading !== null) {
227            return $this->shading->getFill();
228        }
229
230        return null;
231    }
232
233    /**
234     * Set background.
235     *
236     * @param string $value
237     *
238     * @return self
239     */
240    public function setBgColor($value = null)
241    {
242        $this->setShading(['fill' => $value]);
243
244        return $this;
245    }
246
247    /**
248     * Get TLRBHV Border Size.
249     *
250     * @return int[]
251     */
252    public function getBorderSize()
253    {
254        return [
255            $this->getBorderTopSize(),
256            $this->getBorderLeftSize(),
257            $this->getBorderRightSize(),
258            $this->getBorderBottomSize(),
259            $this->getBorderInsideHSize(),
260            $this->getBorderInsideVSize(),
261        ];
262    }
263
264    /**
265     * Set TLRBHV Border Size.
266     *
267     * @param int $value Border size in eighths of a point (1/8 point)
268     *
269     * @return self
270     */
271    public function setBorderSize($value = null)
272    {
273        $this->setBorderTopSize($value);
274        $this->setBorderLeftSize($value);
275        $this->setBorderRightSize($value);
276        $this->setBorderBottomSize($value);
277        $this->setBorderInsideHSize($value);
278        $this->setBorderInsideVSize($value);
279
280        return $this;
281    }
282
283    /**
284     * Get TLRBHV Border Color.
285     *
286     * @return string[]
287     */
288    public function getBorderColor()
289    {
290        return [
291            $this->getBorderTopColor(),
292            $this->getBorderLeftColor(),
293            $this->getBorderRightColor(),
294            $this->getBorderBottomColor(),
295            $this->getBorderInsideHColor(),
296            $this->getBorderInsideVColor(),
297        ];
298    }
299
300    /**
301     * Set TLRBHV Border Color.
302     *
303     * @param string $value
304     *
305     * @return self
306     */
307    public function setBorderColor($value = null)
308    {
309        $this->setBorderTopColor($value);
310        $this->setBorderLeftColor($value);
311        $this->setBorderRightColor($value);
312        $this->setBorderBottomColor($value);
313        $this->setBorderInsideHColor($value);
314        $this->setBorderInsideVColor($value);
315
316        return $this;
317    }
318
319    /**
320     * Get border size inside horizontal.
321     *
322     * @return int
323     */
324    public function getBorderInsideHSize()
325    {
326        return $this->getTableOnlyProperty('borderInsideHSize');
327    }
328
329    /**
330     * Set border size inside horizontal.
331     *
332     * @param int $value
333     *
334     * @return self
335     */
336    public function setBorderInsideHSize($value = null)
337    {
338        return $this->setTableOnlyProperty('borderInsideHSize', $value);
339    }
340
341    /**
342     * Get border color inside horizontal.
343     *
344     * @return string
345     */
346    public function getBorderInsideHColor()
347    {
348        return $this->getTableOnlyProperty('borderInsideHColor');
349    }
350
351    /**
352     * Set border color inside horizontal.
353     *
354     * @param string $value
355     *
356     * @return self
357     */
358    public function setBorderInsideHColor($value = null)
359    {
360        return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
361    }
362
363    /**
364     * Get border size inside vertical.
365     *
366     * @return int
367     */
368    public function getBorderInsideVSize()
369    {
370        return $this->getTableOnlyProperty('borderInsideVSize');
371    }
372
373    /**
374     * Set border size inside vertical.
375     *
376     * @param int $value
377     *
378     * @return self
379     */
380    public function setBorderInsideVSize($value = null)
381    {
382        return $this->setTableOnlyProperty('borderInsideVSize', $value);
383    }
384
385    /**
386     * Get border color inside vertical.
387     *
388     * @return string
389     */
390    public function getBorderInsideVColor()
391    {
392        return $this->getTableOnlyProperty('borderInsideVColor');
393    }
394
395    /**
396     * Set border color inside vertical.
397     *
398     * @param string $value
399     *
400     * @return self
401     */
402    public function setBorderInsideVColor($value = null)
403    {
404        return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
405    }
406
407    /**
408     * Get cell margin top.
409     *
410     * @return int
411     */
412    public function getCellMarginTop()
413    {
414        return $this->getTableOnlyProperty('cellMarginTop');
415    }
416
417    /**
418     * Set cell margin top.
419     *
420     * @param int $value
421     *
422     * @return self
423     */
424    public function setCellMarginTop($value = null)
425    {
426        return $this->setTableOnlyProperty('cellMarginTop', $value);
427    }
428
429    /**
430     * Get cell margin left.
431     *
432     * @return int
433     */
434    public function getCellMarginLeft()
435    {
436        return $this->getTableOnlyProperty('cellMarginLeft');
437    }
438
439    /**
440     * Set cell margin left.
441     *
442     * @param int $value
443     *
444     * @return self
445     */
446    public function setCellMarginLeft($value = null)
447    {
448        return $this->setTableOnlyProperty('cellMarginLeft', $value);
449    }
450
451    /**
452     * Get cell margin right.
453     *
454     * @return int
455     */
456    public function getCellMarginRight()
457    {
458        return $this->getTableOnlyProperty('cellMarginRight');
459    }
460
461    /**
462     * Set cell margin right.
463     *
464     * @param int $value
465     *
466     * @return self
467     */
468    public function setCellMarginRight($value = null)
469    {
470        return $this->setTableOnlyProperty('cellMarginRight', $value);
471    }
472
473    /**
474     * Get cell margin bottom.
475     *
476     * @return int
477     */
478    public function getCellMarginBottom()
479    {
480        return $this->getTableOnlyProperty('cellMarginBottom');
481    }
482
483    /**
484     * Set cell margin bottom.
485     *
486     * @param int $value
487     *
488     * @return self
489     */
490    public function setCellMarginBottom($value = null)
491    {
492        return $this->setTableOnlyProperty('cellMarginBottom', $value);
493    }
494
495    /**
496     * Get cell margin.
497     *
498     * @return int[]
499     */
500    public function getCellMargin()
501    {
502        return [
503            $this->cellMarginTop,
504            $this->cellMarginLeft,
505            $this->cellMarginRight,
506            $this->cellMarginBottom,
507        ];
508    }
509
510    /**
511     * Set TLRB cell margin.
512     *
513     * @param int $value Margin in twips
514     *
515     * @return self
516     */
517    public function setCellMargin($value = null)
518    {
519        $this->setCellMarginTop($value);
520        $this->setCellMarginLeft($value);
521        $this->setCellMarginRight($value);
522        $this->setCellMarginBottom($value);
523
524        return $this;
525    }
526
527    /**
528     * Check if any of the margin is not null.
529     *
530     * @return bool
531     */
532    public function hasMargin()
533    {
534        $margins = $this->getCellMargin();
535
536        return $margins !== array_filter($margins, 'is_null');
537    }
538
539    /**
540     * Get shading.
541     *
542     * @return Shading
543     */
544    public function getShading()
545    {
546        return $this->shading;
547    }
548
549    /**
550     * Set shading.
551     *
552     * @param mixed $value
553     *
554     * @return self
555     */
556    public function setShading($value = null)
557    {
558        $this->setObjectVal($value, 'Shading', $this->shading);
559
560        return $this;
561    }
562
563    /**
564     * @since 0.13.0
565     *
566     * @return string
567     */
568    public function getAlignment()
569    {
570        return $this->alignment;
571    }
572
573    /**
574     * @since 0.13.0
575     *
576     * @param string $value
577     *
578     * @return self
579     */
580    public function setAlignment($value)
581    {
582        if (JcTable::isValid($value) || Jc::isValid($value)) {
583            $this->alignment = $value;
584        }
585
586        return $this;
587    }
588
589    /**
590     * Get width.
591     *
592     * @return float|int
593     */
594    public function getWidth()
595    {
596        return $this->width;
597    }
598
599    /**
600     * Set width.
601     *
602     * @param float|int $value
603     *
604     * @return self
605     */
606    public function setWidth($value = null)
607    {
608        $this->width = $this->setNumericVal($value, $this->width);
609
610        return $this;
611    }
612
613    /**
614     * Get width unit.
615     *
616     * @return string
617     */
618    public function getUnit()
619    {
620        return $this->unit;
621    }
622
623    /**
624     * Set width unit.
625     *
626     * @param string $value
627     *
628     * @return self
629     */
630    public function setUnit($value = null)
631    {
632        TblWidth::validate($value);
633        $this->unit = $value;
634
635        return $this;
636    }
637
638    /**
639     * Get layout.
640     *
641     * @return string
642     */
643    public function getLayout()
644    {
645        return $this->layout;
646    }
647
648    /**
649     * Set layout.
650     *
651     * @param string $value
652     *
653     * @return self
654     */
655    public function setLayout($value = null)
656    {
657        $enum = [self::LAYOUT_AUTO, self::LAYOUT_FIXED];
658        $this->layout = $this->setEnumVal($value, $enum, $this->layout);
659
660        return $this;
661    }
662
663    /**
664     * Get table style only property by checking if it's a firstRow.
665     *
666     * This is necessary since firstRow style is cloned from table style but
667     * without certain properties activated, e.g. margins
668     *
669     * @param string $property
670     *
671     * @return null|int|string
672     */
673    private function getTableOnlyProperty($property)
674    {
675        if (false === $this->isFirstRow) {
676            return $this->$property;
677        }
678
679        return null;
680    }
681
682    /**
683     * Set table style only property by checking if it's a firstRow.
684     *
685     * This is necessary since firstRow style is cloned from table style but
686     * without certain properties activated, e.g. margins
687     *
688     * @param string $property
689     * @param int|string $value
690     * @param bool $isNumeric
691     *
692     * @return self
693     */
694    private function setTableOnlyProperty($property, $value, $isNumeric = true)
695    {
696        if (false === $this->isFirstRow) {
697            if (true === $isNumeric) {
698                $this->$property = $this->setNumericVal($value, $this->$property);
699            } else {
700                $this->$property = $value;
701            }
702        }
703
704        return $this;
705    }
706
707    /**
708     * Get position.
709     *
710     * @return ?TablePosition
711     */
712    public function getPosition()
713    {
714        return $this->position;
715    }
716
717    /**
718     * Set position.
719     *
720     * @param mixed $value
721     *
722     * @return self
723     */
724    public function setPosition($value = null)
725    {
726        $this->setObjectVal($value, 'TablePosition', $this->position);
727
728        return $this;
729    }
730
731    /**
732     * @return ?TblWidthComplexType
733     */
734    public function getIndent()
735    {
736        return $this->indent;
737    }
738
739    /**
740     * @return self
741     *
742     * @see http://www.datypic.com/sc/ooxml/e-w_tblInd-1.html
743     */
744    public function setIndent(TblWidthComplexType $indent)
745    {
746        $this->indent = $indent;
747
748        return $this;
749    }
750
751    /**
752     * Get the columnWidths.
753     *
754     * @return null|int[]
755     */
756    public function getColumnWidths()
757    {
758        return $this->columnWidths;
759    }
760
761    /**
762     * The column widths.
763     *
764     * @param int[] $value
765     */
766    public function setColumnWidths(?array $value = null): void
767    {
768        $this->columnWidths = $value;
769    }
770
771    /**
772     * Get bidiVisual.
773     *
774     * @return ?bool
775     */
776    public function isBidiVisual()
777    {
778        return $this->bidiVisual ?? Settings::isDefaultRtl();
779    }
780
781    /**
782     * Set bidiVisual.
783     *
784     * @param ?bool $bidi
785     *            Set to true to visually present table as Right to Left
786     *
787     * @return self
788     */
789    public function setBidiVisual($bidi)
790    {
791        $this->bidiVisual = $bidi;
792
793        return $this;
794    }
795}