Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
72 / 72 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Table | |
100.00% |
72 / 72 |
|
100.00% |
10 / 10 |
21 | |
100.00% |
1 / 1 |
| write | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| writeStyle | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |||
| writeLayout | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| writeMargin | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| writeBorder | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| writeTblWidth | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| writeFirstRow | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| writeShading | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| setWidth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| writeIndent | |
100.00% |
4 / 4 |
|
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\Style; |
| 20 | |
| 21 | use PhpOffice\PhpWord\Shared\XMLWriter; |
| 22 | use PhpOffice\PhpWord\SimpleType\TblWidth; |
| 23 | use PhpOffice\PhpWord\Style\Table as TableStyle; |
| 24 | use PhpOffice\PhpWord\Writer\Word2007\Element\TableAlignment; |
| 25 | |
| 26 | /** |
| 27 | * Table style writer. |
| 28 | * |
| 29 | * @since 0.10.0 |
| 30 | */ |
| 31 | class Table extends AbstractStyle |
| 32 | { |
| 33 | /** |
| 34 | * @var int Table width |
| 35 | */ |
| 36 | private $width; |
| 37 | |
| 38 | /** |
| 39 | * Write style. |
| 40 | */ |
| 41 | public function write(): void |
| 42 | { |
| 43 | $style = $this->getStyle(); |
| 44 | $xmlWriter = $this->getXmlWriter(); |
| 45 | |
| 46 | if ($style instanceof TableStyle) { |
| 47 | $this->writeStyle($xmlWriter, $style); |
| 48 | } elseif (is_string($style)) { |
| 49 | $xmlWriter->startElement('w:tblPr'); |
| 50 | $xmlWriter->startElement('w:tblStyle'); |
| 51 | $xmlWriter->writeAttribute('w:val', $style); |
| 52 | $xmlWriter->endElement(); |
| 53 | if (null !== $this->width) { |
| 54 | $this->writeTblWidth($xmlWriter, 'w:tblW', TblWidth::PERCENT, $this->width); |
| 55 | } |
| 56 | $xmlWriter->endElement(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Write full style. |
| 62 | */ |
| 63 | private function writeStyle(XMLWriter $xmlWriter, TableStyle $style): void |
| 64 | { |
| 65 | // w:tblPr |
| 66 | $xmlWriter->startElement('w:tblPr'); |
| 67 | |
| 68 | // Table alignment |
| 69 | if ('' !== $style->getAlignment()) { |
| 70 | $tableAlignment = new TableAlignment($style->getAlignment()); |
| 71 | $xmlWriter->startElement($tableAlignment->getName()); |
| 72 | foreach ($tableAlignment->getAttributes() as $attributeName => $attributeValue) { |
| 73 | $xmlWriter->writeAttribute($attributeName, $attributeValue); |
| 74 | } |
| 75 | $xmlWriter->endElement(); |
| 76 | } |
| 77 | |
| 78 | $this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth()); |
| 79 | $this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing()); |
| 80 | $this->writeIndent($xmlWriter, $style); |
| 81 | $this->writeLayout($xmlWriter, $style->getLayout()); |
| 82 | |
| 83 | // Position |
| 84 | $styleWriter = new TablePosition($xmlWriter, $style->getPosition()); |
| 85 | $styleWriter->write(); |
| 86 | |
| 87 | //Right to left |
| 88 | $xmlWriter->writeElementIf($style->isBidiVisual() !== null, 'w:bidiVisual', 'w:val', $this->writeOnOf($style->isBidiVisual())); |
| 89 | |
| 90 | $this->writeMargin($xmlWriter, $style); |
| 91 | $this->writeBorder($xmlWriter, $style); |
| 92 | |
| 93 | $xmlWriter->endElement(); // w:tblPr |
| 94 | |
| 95 | $this->writeShading($xmlWriter, $style); |
| 96 | |
| 97 | // First row style |
| 98 | $firstRow = $style->getFirstRow(); |
| 99 | if ($firstRow instanceof TableStyle) { |
| 100 | $this->writeFirstRow($xmlWriter, $firstRow); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Enable/Disable automatic resizing of the table. |
| 106 | * |
| 107 | * @param string $layout autofit / fixed |
| 108 | */ |
| 109 | private function writeLayout(XMLWriter $xmlWriter, $layout): void |
| 110 | { |
| 111 | $xmlWriter->startElement('w:tblLayout'); |
| 112 | $xmlWriter->writeAttribute('w:type', $layout); |
| 113 | $xmlWriter->endElement(); // w:tblLayout |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Write margin. |
| 118 | */ |
| 119 | private function writeMargin(XMLWriter $xmlWriter, TableStyle $style): void |
| 120 | { |
| 121 | if ($style->hasMargin()) { |
| 122 | $xmlWriter->startElement('w:tblCellMar'); |
| 123 | |
| 124 | $styleWriter = new MarginBorder($xmlWriter); |
| 125 | $styleWriter->setSizes($style->getCellMargin()); |
| 126 | $styleWriter->write(); |
| 127 | |
| 128 | $xmlWriter->endElement(); // w:tblCellMar |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Write border. |
| 134 | */ |
| 135 | private function writeBorder(XMLWriter $xmlWriter, TableStyle $style): void |
| 136 | { |
| 137 | if ($style->hasBorder()) { |
| 138 | $xmlWriter->startElement('w:tblBorders'); |
| 139 | |
| 140 | $styleWriter = new MarginBorder($xmlWriter); |
| 141 | $styleWriter->setSizes($style->getBorderSize()); |
| 142 | $styleWriter->setColors($style->getBorderColor()); |
| 143 | $styleWriter->write(); |
| 144 | |
| 145 | $xmlWriter->endElement(); // w:tblBorders |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Writes a table width. |
| 151 | * |
| 152 | * @param string $elementName |
| 153 | * @param string $unit |
| 154 | * @param null|float|int $width |
| 155 | */ |
| 156 | private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null): void |
| 157 | { |
| 158 | if (null === $width) { |
| 159 | return; |
| 160 | } |
| 161 | $xmlWriter->startElement($elementName); |
| 162 | $xmlWriter->writeAttribute('w:w', $width); |
| 163 | $xmlWriter->writeAttribute('w:type', $unit); |
| 164 | $xmlWriter->endElement(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Write row style. |
| 169 | */ |
| 170 | private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style): void |
| 171 | { |
| 172 | $xmlWriter->startElement('w:tblStylePr'); |
| 173 | $xmlWriter->writeAttribute('w:type', 'firstRow'); |
| 174 | $xmlWriter->startElement('w:tcPr'); |
| 175 | |
| 176 | $this->writeBorder($xmlWriter, $style); |
| 177 | $this->writeShading($xmlWriter, $style); |
| 178 | |
| 179 | $xmlWriter->endElement(); // w:tcPr |
| 180 | $xmlWriter->endElement(); // w:tblStylePr |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Write shading. |
| 185 | */ |
| 186 | private function writeShading(XMLWriter $xmlWriter, TableStyle $style): void |
| 187 | { |
| 188 | if (null !== $style->getShading()) { |
| 189 | $xmlWriter->startElement('w:tcPr'); |
| 190 | |
| 191 | $styleWriter = new Shading($xmlWriter, $style->getShading()); |
| 192 | $styleWriter->write(); |
| 193 | |
| 194 | $xmlWriter->endElement(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Set width. |
| 200 | * |
| 201 | * @param int $value |
| 202 | */ |
| 203 | public function setWidth($value = null): void |
| 204 | { |
| 205 | $this->width = $value; |
| 206 | } |
| 207 | |
| 208 | private function writeIndent(XMLWriter $xmlWriter, TableStyle $style): void |
| 209 | { |
| 210 | $indent = $style->getIndent(); |
| 211 | |
| 212 | if ($indent === null) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | $this->writeTblWidth($xmlWriter, 'w:tblInd', $indent->getType(), $indent->getValue()); |
| 217 | } |
| 218 | } |