Passed
Pull Request — master (#4264)
by Owen
18:41 queued 08:04
created

Style::writeTableStyle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Ods\Cell;
4
5
use PhpOffice\PhpSpreadsheet\Helper\Dimension;
6
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
7
use PhpOffice\PhpSpreadsheet\Style\Alignment;
8
use PhpOffice\PhpSpreadsheet\Style\Border;
9
use PhpOffice\PhpSpreadsheet\Style\Borders;
10
use PhpOffice\PhpSpreadsheet\Style\Fill;
11
use PhpOffice\PhpSpreadsheet\Style\Font;
12
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle;
13
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
14
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension;
15
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
16
17
class Style
18
{
19
    public const CELL_STYLE_PREFIX = 'ce';
20
    public const COLUMN_STYLE_PREFIX = 'co';
21
    public const ROW_STYLE_PREFIX = 'ro';
22
    public const TABLE_STYLE_PREFIX = 'ta';
23
    public const INDENT_TO_INCHES = 0.1043; // undocumented, used trial and error
24
25
    private XMLWriter $writer;
26
27 38
    public function __construct(XMLWriter $writer)
28
    {
29 38
        $this->writer = $writer;
30
    }
31
32 38
    private function mapHorizontalAlignment(?string $horizontalAlignment): string
33
    {
34 38
        return match ($horizontalAlignment) {
35
            Alignment::HORIZONTAL_CENTER, Alignment::HORIZONTAL_CENTER_CONTINUOUS, Alignment::HORIZONTAL_DISTRIBUTED => 'center',
36
            Alignment::HORIZONTAL_RIGHT => 'end',
37
            Alignment::HORIZONTAL_FILL, Alignment::HORIZONTAL_JUSTIFY => 'justify',
38 38
            Alignment::HORIZONTAL_GENERAL, '', null => '',
39 38
            default => 'start',
40 38
        };
41
    }
42
43 38
    private function mapVerticalAlignment(string $verticalAlignment): string
44
    {
45 38
        return match ($verticalAlignment) {
46
            Alignment::VERTICAL_TOP => 'top',
47
            Alignment::VERTICAL_CENTER => 'middle',
48
            Alignment::VERTICAL_DISTRIBUTED, Alignment::VERTICAL_JUSTIFY => 'automatic',
49 38
            default => 'bottom',
50 38
        };
51
    }
52
53 38
    private function writeFillStyle(Fill $fill): void
54
    {
55 38
        switch ($fill->getFillType()) {
56
            case Fill::FILL_SOLID:
57 1
                $this->writer->writeAttribute('fo:background-color', sprintf(
58 1
                    '#%s',
59 1
                    strtolower($fill->getStartColor()->getRGB())
60 1
                ));
61
62 1
                break;
63
            case Fill::FILL_GRADIENT_LINEAR:
64
            case Fill::FILL_GRADIENT_PATH:
65
                /// TODO :: To be implemented
66
                break;
67
            case Fill::FILL_NONE:
68
            default:
69
        }
70
    }
71
72 38
    private function writeBordersStyle(Borders $borders): void
73
    {
74 38
        $this->writeBorderStyle('bottom', $borders->getBottom());
75 38
        $this->writeBorderStyle('left', $borders->getLeft());
76 38
        $this->writeBorderStyle('right', $borders->getRight());
77 38
        $this->writeBorderStyle('top', $borders->getTop());
78
    }
79
80 38
    private function writeBorderStyle(string $direction, Border $border): void
81
    {
82 38
        if ($border->getBorderStyle() === Border::BORDER_NONE) {
83 38
            return;
84
        }
85
86 1
        $this->writer->writeAttribute('fo:border-' . $direction, sprintf(
87 1
            '%s %s #%s',
88 1
            $this->mapBorderWidth($border),
89 1
            $this->mapBorderStyle($border),
90 1
            $border->getColor()->getRGB(),
91 1
        ));
92
    }
93
94 1
    private function mapBorderWidth(Border $border): string
95
    {
96 1
        switch ($border->getBorderStyle()) {
97
            case Border::BORDER_THIN:
98
            case Border::BORDER_DASHED:
99
            case Border::BORDER_DASHDOT:
100
            case Border::BORDER_DASHDOTDOT:
101
            case Border::BORDER_DOTTED:
102
            case Border::BORDER_HAIR:
103
                return '0.75pt';
104
            case Border::BORDER_MEDIUM:
105
            case Border::BORDER_MEDIUMDASHED:
106
            case Border::BORDER_MEDIUMDASHDOT:
107
            case Border::BORDER_MEDIUMDASHDOTDOT:
108
            case Border::BORDER_SLANTDASHDOT:
109
                return '1.75pt';
110
            case Border::BORDER_DOUBLE:
111
            case Border::BORDER_THICK:
112 1
                return '2.5pt';
113
        }
114
115
        return '1pt';
116
    }
117
118 1
    private function mapBorderStyle(Border $border): string
119
    {
120 1
        switch ($border->getBorderStyle()) {
121
            case Border::BORDER_DOTTED:
122
            case Border::BORDER_MEDIUMDASHDOTDOT:
123
                return Border::BORDER_DOTTED;
124
125
            case Border::BORDER_DASHED:
126
            case Border::BORDER_DASHDOT:
127
            case Border::BORDER_DASHDOTDOT:
128
            case Border::BORDER_MEDIUMDASHDOT:
129
            case Border::BORDER_MEDIUMDASHED:
130
            case Border::BORDER_SLANTDASHDOT:
131
                return Border::BORDER_DASHED;
132
133
            case Border::BORDER_DOUBLE:
134
                return Border::BORDER_DOUBLE;
135
136
            case Border::BORDER_HAIR:
137
            case Border::BORDER_MEDIUM:
138
            case Border::BORDER_THICK:
139
            case Border::BORDER_THIN:
140 1
                return 'solid';
141
        }
142
143
        return 'solid';
144
    }
145
146 38
    private function writeCellProperties(CellStyle $style): void
147
    {
148
        // Align
149 38
        $hAlign = $style->getAlignment()->getHorizontal();
150 38
        $hAlign = $this->mapHorizontalAlignment($hAlign);
151 38
        $vAlign = $style->getAlignment()->getVertical();
152 38
        $wrap = $style->getAlignment()->getWrapText();
153 38
        $indent = $style->getAlignment()->getIndent();
154
155 38
        $this->writer->startElement('style:table-cell-properties');
156 38
        if (!empty($vAlign) || $wrap) {
157 38
            if (!empty($vAlign)) {
158 38
                $vAlign = $this->mapVerticalAlignment($vAlign);
159 38
                $this->writer->writeAttribute('style:vertical-align', $vAlign);
160
            }
161 38
            if ($wrap) {
162 1
                $this->writer->writeAttribute('fo:wrap-option', 'wrap');
163
            }
164
        }
165 38
        $this->writer->writeAttribute('style:rotation-align', 'none');
166
167
        // Fill
168 38
        $this->writeFillStyle($style->getFill());
169
170
        // Border
171 38
        $this->writeBordersStyle($style->getBorders());
172
173 38
        $this->writer->endElement();
174
175 38
        if ($hAlign !== '' || !empty($indent)) {
176 1
            $this->writer
177 1
                ->startElement('style:paragraph-properties');
178 1
            if ($hAlign !== '') {
179
                $this->writer->writeAttribute('fo:text-align', $hAlign);
180
            }
181 1
            if (!empty($indent)) {
182 1
                $indentString = sprintf('%.4f', $indent * self::INDENT_TO_INCHES) . 'in';
183 1
                $this->writer->writeAttribute('fo:margin-left', $indentString);
184
            }
185 1
            $this->writer->endElement();
186
        }
187
    }
188
189 1
    protected function mapUnderlineStyle(Font $font): string
190
    {
191 1
        return match ($font->getUnderline()) {
192 1
            Font::UNDERLINE_DOUBLE, Font::UNDERLINE_DOUBLEACCOUNTING => 'double',
193 1
            Font::UNDERLINE_SINGLE, Font::UNDERLINE_SINGLEACCOUNTING => 'single',
194 1
            default => 'none',
195 1
        };
196
    }
197
198 38
    protected function writeTextProperties(CellStyle $style): void
199
    {
200
        // Font
201 38
        $this->writer->startElement('style:text-properties');
202
203 38
        $font = $style->getFont();
204
205 38
        if ($font->getBold()) {
206 2
            $this->writer->writeAttribute('fo:font-weight', 'bold');
207 2
            $this->writer->writeAttribute('style:font-weight-complex', 'bold');
208 2
            $this->writer->writeAttribute('style:font-weight-asian', 'bold');
209
        }
210
211 38
        if ($font->getItalic()) {
212 1
            $this->writer->writeAttribute('fo:font-style', 'italic');
213
        }
214
215 38
        $this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB()));
216
217 38
        if ($family = $font->getName()) {
218 38
            $this->writer->writeAttribute('fo:font-family', $family);
219
        }
220
221 38
        if ($size = $font->getSize()) {
222 38
            $this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
223
        }
224
225 38
        if ($font->getUnderline() && $font->getUnderline() !== Font::UNDERLINE_NONE) {
226 1
            $this->writer->writeAttribute('style:text-underline-style', 'solid');
227 1
            $this->writer->writeAttribute('style:text-underline-width', 'auto');
228 1
            $this->writer->writeAttribute('style:text-underline-color', 'font-color');
229
230 1
            $underline = $this->mapUnderlineStyle($font);
231 1
            $this->writer->writeAttribute('style:text-underline-type', $underline);
232
        }
233
234 38
        $this->writer->endElement(); // Close style:text-properties
235
    }
236
237 6
    protected function writeColumnProperties(ColumnDimension $columnDimension): void
238
    {
239 6
        $this->writer->startElement('style:table-column-properties');
240 6
        $this->writer->writeAttribute(
241 6
            'style:column-width',
242 6
            round($columnDimension->getWidth(Dimension::UOM_CENTIMETERS), 3) . 'cm'
243 6
        );
244 6
        $this->writer->writeAttribute('fo:break-before', 'auto');
245
246
        // End
247 6
        $this->writer->endElement(); // Close style:table-column-properties
248
    }
249
250 6
    public function writeColumnStyles(ColumnDimension $columnDimension, int $sheetId): void
251
    {
252 6
        $this->writer->startElement('style:style');
253 6
        $this->writer->writeAttribute('style:family', 'table-column');
254 6
        $this->writer->writeAttribute(
255 6
            'style:name',
256 6
            sprintf('%s_%d_%d', self::COLUMN_STYLE_PREFIX, $sheetId, $columnDimension->getColumnNumeric())
257 6
        );
258
259 6
        $this->writeColumnProperties($columnDimension);
260
261
        // End
262 6
        $this->writer->endElement(); // Close style:style
263
    }
264
265 1
    protected function writeRowProperties(RowDimension $rowDimension): void
266
    {
267 1
        $this->writer->startElement('style:table-row-properties');
268 1
        $this->writer->writeAttribute(
269 1
            'style:row-height',
270 1
            round($rowDimension->getRowHeight(Dimension::UOM_CENTIMETERS), 3) . 'cm'
271 1
        );
272 1
        $this->writer->writeAttribute('style:use-optimal-row-height', 'false');
273 1
        $this->writer->writeAttribute('fo:break-before', 'auto');
274
275
        // End
276 1
        $this->writer->endElement(); // Close style:table-row-properties
277
    }
278
279 1
    public function writeRowStyles(RowDimension $rowDimension, int $sheetId): void
280
    {
281 1
        $this->writer->startElement('style:style');
282 1
        $this->writer->writeAttribute('style:family', 'table-row');
283 1
        $this->writer->writeAttribute(
284 1
            'style:name',
285 1
            sprintf('%s_%d_%d', self::ROW_STYLE_PREFIX, $sheetId, $rowDimension->getRowIndex())
286 1
        );
287
288 1
        $this->writeRowProperties($rowDimension);
289
290
        // End
291 1
        $this->writer->endElement(); // Close style:style
292
    }
293
294 38
    public function writeTableStyle(Worksheet $worksheet, int $sheetId): void
295
    {
296 38
        $this->writer->startElement('style:style');
297 38
        $this->writer->writeAttribute('style:family', 'table');
298 38
        $this->writer->writeAttribute(
299 38
            'style:name',
300 38
            sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId)
301 38
        );
302
303 38
        $this->writer->startElement('style:table-properties');
304
305 38
        $this->writer->writeAttribute(
306 38
            'table:display',
307 38
            $worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false'
308 38
        );
309
310 38
        $this->writer->endElement(); // Close style:table-properties
311 38
        $this->writer->endElement(); // Close style:style
312
    }
313
314 38
    public function write(CellStyle $style): void
315
    {
316 38
        $this->writer->startElement('style:style');
317 38
        $this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
318 38
        $this->writer->writeAttribute('style:family', 'table-cell');
319 38
        $this->writer->writeAttribute('style:parent-style-name', 'Default');
320
321
        // Alignment, fill colour, etc
322 38
        $this->writeCellProperties($style);
323
324
        // style:text-properties
325 38
        $this->writeTextProperties($style);
326
327
        // End
328 38
        $this->writer->endElement(); // Close style:style
329
    }
330
}
331