Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 writeColumns
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 writeRow
100.00% covered (success)
100.00%
8 / 8
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\ODText\Element;
19
20use PhpOffice\PhpWord\Element\Row as RowElement;
21use PhpOffice\PhpWord\Element\Table as TableElement;
22use PhpOffice\PhpWord\Shared\XMLWriter;
23
24/**
25 * Table element writer.
26 *
27 * @since 0.10.0
28 */
29class Table extends AbstractElement
30{
31    /**
32     * Write element.
33     */
34    public function write(): void
35    {
36        $xmlWriter = $this->getXmlWriter();
37        $element = $this->getElement();
38        if (!$element instanceof \PhpOffice\PhpWord\Element\Table) {
39            return;
40        }
41        $rows = $element->getRows();
42        $rowCount = count($rows);
43
44        if ($rowCount > 0) {
45            $xmlWriter->startElement('table:table');
46            $xmlWriter->writeAttribute('table:name', $element->getElementId());
47            $xmlWriter->writeAttribute('table:style-name', $element->getElementId());
48
49            // Write columns
50            $this->writeColumns($xmlWriter, $element);
51
52            // Write rows
53            foreach ($rows as $row) {
54                $this->writeRow($xmlWriter, $row);
55            }
56            $xmlWriter->endElement(); // table:table
57        }
58    }
59
60    /**
61     * Write column.
62     */
63    private function writeColumns(XMLWriter $xmlWriter, TableElement $element): void
64    {
65        $colCount = $element->countColumns();
66
67        for ($i = 0; $i < $colCount; ++$i) {
68            $xmlWriter->startElement('table:table-column');
69            $xmlWriter->writeAttribute('table:style-name', $element->getElementId() . '.' . $i);
70            $xmlWriter->endElement();
71        }
72    }
73
74    /**
75     * Write row.
76     */
77    private function writeRow(XMLWriter $xmlWriter, RowElement $row): void
78    {
79        $xmlWriter->startElement('table:table-row');
80        /** @var \PhpOffice\PhpWord\Element\Row $row Type hint */
81        foreach ($row->getCells() as $cell) {
82            $xmlWriter->startElement('table:table-cell');
83            $xmlWriter->writeAttribute('office:value-type', 'string');
84
85            $containerWriter = new Container($xmlWriter, $cell);
86            $containerWriter->write();
87
88            $xmlWriter->endElement(); // table:table-cell
89        }
90        $xmlWriter->endElement(); // table:table-row
91    }
92}