Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Cell
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
2 / 2
16
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
61 / 61
100.00% covered (success)
100.00%
1 / 1
15
 setWidth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Word2007\Style;
20
21use PhpOffice\PhpWord\Style\Cell as CellStyle;
22
23/**
24 * Cell style writer.
25 *
26 * @since 0.10.0
27 */
28class Cell extends AbstractStyle
29{
30    /**
31     * @var int Cell width
32     */
33    private $width;
34
35    /**
36     * Write style.
37     */
38    public function write(): void
39    {
40        $style = $this->getStyle();
41        if (!$style instanceof CellStyle) {
42            return;
43        }
44        $xmlWriter = $this->getXmlWriter();
45
46        $xmlWriter->startElement('w:tcPr');
47
48        // Width
49        if (null !== $this->width || null !== $style->getWidth()) {
50            $width = null === $this->width ? $style->getWidth() : $this->width;
51
52            $xmlWriter->startElement('w:tcW');
53            $xmlWriter->writeAttribute('w:w', $width);
54            $xmlWriter->writeAttribute('w:type', $style->getUnit());
55            $xmlWriter->endElement(); // w:tcW
56        }
57
58        $paddingTop = $style->getPaddingTop();
59        $paddingLeft = $style->getPaddingLeft();
60        $paddingBottom = $style->getPaddingBottom();
61        $paddingRight = $style->getPaddingRight();
62
63        if ($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null) {
64            $xmlWriter->startElement('w:tcMar');
65            if ($paddingTop !== null) {
66                $xmlWriter->startElement('w:top');
67                $xmlWriter->writeAttribute('w:w', $paddingTop);
68                $xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
69                $xmlWriter->endElement(); // w:top
70            }
71            if ($paddingLeft !== null) {
72                $xmlWriter->startElement('w:start');
73                $xmlWriter->writeAttribute('w:w', $paddingLeft);
74                $xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
75                $xmlWriter->endElement(); // w:start
76            }
77            if ($paddingBottom !== null) {
78                $xmlWriter->startElement('w:bottom');
79                $xmlWriter->writeAttribute('w:w', $paddingBottom);
80                $xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
81                $xmlWriter->endElement(); // w:bottom
82            }
83            if ($paddingRight !== null) {
84                $xmlWriter->startElement('w:end');
85                $xmlWriter->writeAttribute('w:w', $paddingRight);
86                $xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
87                $xmlWriter->endElement(); // w:end
88            }
89            $xmlWriter->endElement(); // w:tcMar
90        }
91
92        // Text direction
93        $textDir = $style->getTextDirection();
94        $xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);
95
96        // Vertical alignment
97        $vAlign = $style->getVAlign();
98        $xmlWriter->writeElementIf(null !== $vAlign, 'w:vAlign', 'w:val', $vAlign);
99
100        // Border
101        if ($style->hasBorder()) {
102            $xmlWriter->startElement('w:tcBorders');
103
104            $styleWriter = new MarginBorder($xmlWriter);
105            $styleWriter->setSizes($style->getBorderSize());
106            $styleWriter->setColors($style->getBorderColor());
107            $styleWriter->setStyles($style->getBorderStyle());
108            $styleWriter->setAttributes(['defaultColor' => CellStyle::DEFAULT_BORDER_COLOR]);
109            $styleWriter->write();
110
111            $xmlWriter->endElement();
112        }
113
114        // Shading
115        $shading = $style->getShading();
116        if (null !== $shading) {
117            $styleWriter = new Shading($xmlWriter, $shading);
118            $styleWriter->write();
119        }
120
121        // Colspan & rowspan
122        $gridSpan = $style->getGridSpan();
123        $vMerge = $style->getVMerge();
124        $xmlWriter->writeElementIf(null !== $gridSpan, 'w:gridSpan', 'w:val', $gridSpan);
125        $xmlWriter->writeElementIf(null !== $vMerge, 'w:vMerge', 'w:val', $vMerge);
126        $xmlWriter->writeElementIf($style->getNoWrap(), 'w:noWrap');
127
128        $xmlWriter->endElement(); // w:tcPr
129    }
130
131    /**
132     * Set width.
133     *
134     * @param int $value
135     */
136    public function setWidth($value = null): void
137    {
138        $this->width = $value;
139    }
140}