Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
9 / 9
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addRow
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 addCell
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getRows
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyle
100.00% covered (success)
100.00%
1 / 1
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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 countColumns
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 findFirstDefinedCellWidths
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
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\Element;
20
21use PhpOffice\PhpWord\Style\Table as TableStyle;
22
23/**
24 * Table element.
25 */
26class Table extends AbstractElement
27{
28    /**
29     * Table style.
30     *
31     * @var ?TableStyle
32     */
33    private $style;
34
35    /**
36     * Table rows.
37     *
38     * @var Row[]
39     */
40    private $rows = [];
41
42    /**
43     * Table width.
44     *
45     * @var ?int
46     */
47    private $width;
48
49    /**
50     * Create a new table.
51     *
52     * @param mixed $style
53     */
54    public function __construct($style = null)
55    {
56        $this->style = $this->setNewStyle(new TableStyle(), $style);
57    }
58
59    /**
60     * Add a row.
61     *
62     * @param int $height
63     * @param mixed $style
64     *
65     * @return Row
66     */
67    public function addRow($height = null, $style = null)
68    {
69        $row = new Row($height, $style);
70        $row->setParentContainer($this);
71        $this->rows[] = $row;
72
73        return $row;
74    }
75
76    /**
77     * Add a cell.
78     *
79     * @param int $width
80     * @param mixed $style
81     *
82     * @return Cell
83     */
84    public function addCell($width = null, $style = null)
85    {
86        $index = count($this->rows) - 1;
87        $row = $this->rows[$index];
88        $cell = $row->addCell($width, $style);
89
90        return $cell;
91    }
92
93    /**
94     * Get all rows.
95     *
96     * @return Row[]
97     */
98    public function getRows()
99    {
100        return $this->rows;
101    }
102
103    /**
104     * Get table style.
105     *
106     * @return null|string|TableStyle
107     */
108    public function getStyle()
109    {
110        return $this->style;
111    }
112
113    /**
114     * Get table width.
115     *
116     * @return ?int
117     */
118    public function getWidth()
119    {
120        return $this->width;
121    }
122
123    /**
124     * Set table width.
125     *
126     * @param int $width
127     */
128    public function setWidth($width): void
129    {
130        $this->width = $width;
131    }
132
133    /**
134     * Get column count.
135     *
136     * @return int
137     */
138    public function countColumns()
139    {
140        $columnCount = 0;
141
142        $rowCount = count($this->rows);
143        for ($i = 0; $i < $rowCount; ++$i) {
144            /** @var Row $row Type hint */
145            $row = $this->rows[$i];
146            $cellCount = count($row->getCells());
147            if ($columnCount < $cellCount) {
148                $columnCount = $cellCount;
149            }
150        }
151
152        return $columnCount;
153    }
154
155    /**
156     * The first declared cell width for each column.
157     *
158     * @return int[]
159     */
160    public function findFirstDefinedCellWidths()
161    {
162        $cellWidths = [];
163
164        foreach ($this->rows as $row) {
165            $cells = $row->getCells();
166            if (count($cells) <= count($cellWidths)) {
167                continue;
168            }
169            $cellWidths = [];
170            foreach ($cells as $cell) {
171                $cellWidths[] = $cell->getWidth();
172            }
173        }
174
175        return $cellWidths;
176    }
177}