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