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