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