Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Cell
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
7
 setWidth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
18namespace PhpOffice\PhpWord\Writer\Word2007\Style;
19
20use PhpOffice\PhpWord\Style\Cell as CellStyle;
21
22/**
23 * Cell style writer.
24 *
25 * @since 0.10.0
26 */
27class Cell extends AbstractStyle
28{
29    /**
30     * @var int Cell width
31     */
32    private $width;
33
34    /**
35     * Write style.
36     */
37    public function write(): void
38    {
39        $style = $this->getStyle();
40        if (!$style instanceof CellStyle) {
41            return;
42        }
43        $xmlWriter = $this->getXmlWriter();
44
45        $xmlWriter->startElement('w:tcPr');
46
47        // Width
48        if (null !== $this->width || null !== $style->getWidth()) {
49            $width = null === $this->width ? $style->getWidth() : $this->width;
50
51            $xmlWriter->startElement('w:tcW');
52            $xmlWriter->writeAttribute('w:w', $width);
53            $xmlWriter->writeAttribute('w:type', $style->getUnit());
54            $xmlWriter->endElement(); // w:tcW
55        }
56
57        // Text direction
58        $textDir = $style->getTextDirection();
59        $xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);
60
61        // Vertical alignment
62        $vAlign = $style->getVAlign();
63        $xmlWriter->writeElementIf(null !== $vAlign, 'w:vAlign', 'w:val', $vAlign);
64
65        // Border
66        if ($style->hasBorder()) {
67            $xmlWriter->startElement('w:tcBorders');
68
69            $styleWriter = new MarginBorder($xmlWriter);
70            $styleWriter->setSizes($style->getBorderSize());
71            $styleWriter->setColors($style->getBorderColor());
72            $styleWriter->setStyles($style->getBorderStyle());
73            $styleWriter->setAttributes(['defaultColor' => CellStyle::DEFAULT_BORDER_COLOR]);
74            $styleWriter->write();
75
76            $xmlWriter->endElement();
77        }
78
79        // Shading
80        $shading = $style->getShading();
81        if (null !== $shading) {
82            $styleWriter = new Shading($xmlWriter, $shading);
83            $styleWriter->write();
84        }
85
86        // Colspan & rowspan
87        $gridSpan = $style->getGridSpan();
88        $vMerge = $style->getVMerge();
89        $xmlWriter->writeElementIf(null !== $gridSpan, 'w:gridSpan', 'w:val', $gridSpan);
90        $xmlWriter->writeElementIf(null !== $vMerge, 'w:vMerge', 'w:val', $vMerge);
91        $xmlWriter->writeElementIf($style->getNoWrap(), 'w:noWrap');
92
93        $xmlWriter->endElement(); // w:tcPr
94    }
95
96    /**
97     * Set width.
98     *
99     * @param int $value
100     */
101    public function setWidth($value = null): void
102    {
103        $this->width = $value;
104    }
105}