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