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