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