Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
4 / 4
13
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 writeColumns
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 writeRow
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 writeCell
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
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\Writer\Word2007\Element;
19
20use PhpOffice\PhpWord\Element\Cell as CellElement;
21use PhpOffice\PhpWord\Element\Row as RowElement;
22use PhpOffice\PhpWord\Element\Table as TableElement;
23use PhpOffice\PhpWord\Shared\XMLWriter;
24use PhpOffice\PhpWord\Style\Cell as CellStyle;
25use PhpOffice\PhpWord\Style\Row as RowStyle;
26use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
27use PhpOffice\PhpWord\Writer\Word2007\Style\Row as RowStyleWriter;
28use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
29
30/**
31 * Table element writer.
32 *
33 * @since 0.10.0
34 */
35class Table extends AbstractElement
36{
37    /**
38     * Write element.
39     */
40    public function write(): void
41    {
42        $xmlWriter = $this->getXmlWriter();
43        $element = $this->getElement();
44        if (!$element instanceof TableElement) {
45            return;
46        }
47
48        $rows = $element->getRows();
49        $rowCount = count($rows);
50
51        if ($rowCount > 0) {
52            $xmlWriter->startElement('w:tbl');
53
54            // Write columns
55            $this->writeColumns($xmlWriter, $element);
56
57            // Write style
58            $styleWriter = new TableStyleWriter($xmlWriter, $element->getStyle());
59            $styleWriter->setWidth($element->getWidth());
60            $styleWriter->write();
61
62            // Write rows
63            for ($i = 0; $i < $rowCount; ++$i) {
64                $this->writeRow($xmlWriter, $rows[$i]);
65            }
66
67            $xmlWriter->endElement(); // w:tbl
68        }
69    }
70
71    /**
72     * Write column.
73     */
74    private function writeColumns(XMLWriter $xmlWriter, TableElement $element): void
75    {
76        $cellWidths = $element->findFirstDefinedCellWidths();
77
78        $xmlWriter->startElement('w:tblGrid');
79        foreach ($cellWidths as $width) {
80            $xmlWriter->startElement('w:gridCol');
81            if ($width !== null) {
82                $xmlWriter->writeAttribute('w:w', $width);
83                $xmlWriter->writeAttribute('w:type', 'dxa');
84            }
85            $xmlWriter->endElement();
86        }
87        $xmlWriter->endElement(); // w:tblGrid
88    }
89
90    /**
91     * Write row.
92     */
93    private function writeRow(XMLWriter $xmlWriter, RowElement $row): void
94    {
95        $xmlWriter->startElement('w:tr');
96
97        // Write style
98        $rowStyle = $row->getStyle();
99        if ($rowStyle instanceof RowStyle) {
100            $styleWriter = new RowStyleWriter($xmlWriter, $rowStyle);
101            $styleWriter->setHeight($row->getHeight());
102            $styleWriter->write();
103        }
104
105        // Write cells
106        $cells = $row->getCells();
107        if (count($cells) === 0) {
108            // issue 2505 - Word treats doc as corrupt if row without cell
109            $this->writeCell($xmlWriter, new CellElement());
110        } else {
111            foreach ($cells as $cell) {
112                $this->writeCell($xmlWriter, $cell);
113            }
114        }
115
116        $xmlWriter->endElement(); // w:tr
117    }
118
119    /**
120     * Write cell.
121     */
122    private function writeCell(XMLWriter $xmlWriter, CellElement $cell): void
123    {
124        $xmlWriter->startElement('w:tc');
125
126        // Write style
127        $cellStyle = $cell->getStyle();
128        if ($cellStyle instanceof CellStyle) {
129            $styleWriter = new CellStyleWriter($xmlWriter, $cellStyle);
130            $styleWriter->setWidth($cell->getWidth());
131            $styleWriter->write();
132        }
133
134        // Write content
135        $containerWriter = new Container($xmlWriter, $cell);
136        $containerWriter->write();
137
138        $xmlWriter->endElement(); // w:tc
139    }
140}