Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
36 / 37
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Table
97.30% covered (success)
97.30%
36 / 37
0.00% covered (danger)
0.00%
0 / 1
10
0.00% covered (danger)
0.00%
0 / 1
 write
97.30% covered (success)
97.30%
36 / 37
0.00% covered (danger)
0.00%
0 / 1
10
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
19namespace PhpOffice\PhpWord\Writer\ODText\Style;
20
21/**
22 * Table style writer.
23 *
24 * @since 0.11.0
25 */
26class Table extends AbstractStyle
27{
28    /**
29     * Write style.
30     */
31    public function write(): void
32    {
33        /** @var \PhpOffice\PhpWord\Style\Table $style Type hint */
34        $style = $this->getStyle();
35        if (!$style instanceof \PhpOffice\PhpWord\Style\Table) {
36            return;
37        }
38        $xmlWriter = $this->getXmlWriter();
39
40        $xmlWriter->startElement('style:style');
41        $xmlWriter->writeAttribute('style:name', $style->getStyleName());
42        $xmlWriter->writeAttribute('style:family', 'table');
43        $xmlWriter->startElement('style:table-properties');
44        //$xmlWriter->writeAttribute('style:width', 'table');
45        $xmlWriter->writeAttribute('style:rel-width', 100);
46        $xmlWriter->writeAttribute('table:align', $style->getAlignment() ?: 'center');
47        if ($style->getBgColor() !== null) {
48            $xmlWriter->writeAttribute('fo:background-color', '#' . ltrim($style->getBgColor(), '#'));
49        }
50        if ($style->getWidth() > 0) {
51            $xmlWriter->writeAttribute('style:width', number_format($style->getWidth() / 1440, 3, '.', '') . 'in');
52        }
53        $margins = [
54            'top' => $style->getMarginTop(),
55            'bottom' => $style->getMarginBottom(),
56            'left' => $style->getMarginLeft(),
57            'right' => $style->getMarginRight(),
58        ];
59        foreach ($margins as $side => $value) {
60            if ($value !== null && $value !== \PhpOffice\PhpWord\Style\Border::DEFAULT_MARGIN) {
61                $xmlWriter->writeAttribute('fo:margin-' . $side, number_format($value / 1440, 3, '.', '') . 'in');
62            }
63        }
64        $xmlWriter->writeAttributeIf($style->isBidiVisual(), 'style:writing-mode', 'rl-tb');
65        $xmlWriter->endElement(); // style:table-properties
66        $xmlWriter->endElement(); // style:style
67
68        $cellWidths = $style->getColumnWidths();
69        $countCellWidths = $cellWidths === null ? 0 : count($cellWidths);
70
71        for ($i = 0; $i < $countCellWidths; ++$i) {
72            $width = $cellWidths[$i];
73            $xmlWriter->startElement('style:style');
74            $xmlWriter->writeAttribute('style:name', $style->getStyleName() . '.' . $i);
75            $xmlWriter->writeAttribute('style:family', 'table-column');
76            $xmlWriter->startElement('style:table-column-properties');
77            $xmlWriter->writeAttribute('style:column-width', number_format($width * 0.0017638889, 2, '.', '') . 'cm');
78            $xmlWriter->endElement(); // style:table-column-properties
79            $xmlWriter->endElement(); // style:style
80        }
81    }
82}