Passed
Pull Request — master (#4263)
by Owen
30:17 queued 17:47
created

Style::writeColumnStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 37
    public function __construct(XMLWriter $writer)
28
    {
29 37
        $this->writer = $writer;
30
    }
31
32 37
    private function mapHorizontalAlignment(?string $horizontalAlignment): string
33
    {
34 37
        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 37
            Alignment::HORIZONTAL_GENERAL, '', null => '',
39 37
            default => 'start',
40 37
        };
41
    }
42
43 37
    private function mapVerticalAlignment(string $verticalAlignment): string
44
    {
45 37
        return match ($verticalAlignment) {
46
            Alignment::VERTICAL_TOP => 'top',
47
            Alignment::VERTICAL_CENTER => 'middle',
48
            Alignment::VERTICAL_DISTRIBUTED, Alignment::VERTICAL_JUSTIFY => 'automatic',
49 37
            default => 'bottom',
50 37
        };
51
    }
52
53 37
    private function writeFillStyle(Fill $fill): void
54
    {
55 37
        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 37
    private function writeBordersStyle(Borders $borders): void
73
    {
74 37
        $this->writeBorderStyle('bottom', $borders->getBottom());
75 37
        $this->writeBorderStyle('left', $borders->getLeft());
76 37
        $this->writeBorderStyle('right', $borders->getRight());
77 37
        $this->writeBorderStyle('top', $borders->getTop());
78
    }
79
80 37
    private function writeBorderStyle(string $direction, Border $border): void
81
    {
82 37
        if ($border->getBorderStyle() === Border::BORDER_NONE) {
83 37
            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 37
    private function writeCellProperties(CellStyle $style): void
147
    {
148
        // Align
149 37
        $hAlign = $style->getAlignment()->getHorizontal();
150 37
        $hAlign = $this->mapHorizontalAlignment($hAlign);
151 37
        $vAlign = $style->getAlignment()->getVertical();
152 37
        $wrap = $style->getAlignment()->getWrapText();
153 37
        $indent = $style->getAlignment()->getIndent();
154
155 37
        $this->writer->startElement('style:table-cell-properties');
156 37
        if (!empty($vAlign) || $wrap) {
157 37
            if (!empty($vAlign)) {
158 37
                $vAlign = $this->mapVerticalAlignment($vAlign);
159 37
                $this->writer->writeAttribute('style:vertical-align', $vAlign);
160
            }
161 37
            if ($wrap) {
162 1
                $this->writer->writeAttribute('fo:wrap-option', 'wrap');
163
            }
164
        }
165 37
        $this->writer->writeAttribute('style:rotation-align', 'none');
166
167
        // Fill
168 37
        $this->writeFillStyle($style->getFill());
169
170
        // Border
171 37
        $this->writeBordersStyle($style->getBorders());
172
173 37
        $this->writer->endElement();
174
175 37
        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 37
    protected function writeTextProperties(CellStyle $style): void
199
    {
200
        // Font
201 37
        $this->writer->startElement('style:text-properties');
202
203 37
        $font = $style->getFont();
204
205 37
        if ($font->getBold()) {
206 1
            $this->writer->writeAttribute('fo:font-weight', 'bold');
207 1
            $this->writer->writeAttribute('style:font-weight-complex', 'bold');
208 1
            $this->writer->writeAttribute('style:font-weight-asian', 'bold');
209
        }
210
211 37
        if ($font->getItalic()) {
212 1
            $this->writer->writeAttribute('fo:font-style', 'italic');
213
        }
214
215 37
        $this->writer->writeAttribute('fo:color', sprintf('#%s', $font->getColor()->getRGB()));
216
217 37
        if ($family = $font->getName()) {
218 37
            $this->writer->writeAttribute('fo:font-family', $family);
219
        }
220
221 37
        if ($size = $font->getSize()) {
222 37
            $this->writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
223
        }
224
225 37
        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 37
        $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
    protected function writeRowProperties(RowDimension $rowDimension): void
266
    {
267
        $this->writer->startElement('style:table-row-properties');
268
        $this->writer->writeAttribute(
269
            'style:row-height',
270
            round($rowDimension->getRowHeight(Dimension::UOM_CENTIMETERS), 3) . 'cm'
271
        );
272
        $this->writer->writeAttribute('style:use-optimal-row-height', 'false');
273
        $this->writer->writeAttribute('fo:break-before', 'auto');
274
275
        // End
276
        $this->writer->endElement(); // Close style:table-row-properties
277
    }
278
279
    public function writeRowStyles(RowDimension $rowDimension, int $sheetId): void
280
    {
281
        $this->writer->startElement('style:style');
282
        $this->writer->writeAttribute('style:family', 'table-row');
283
        $this->writer->writeAttribute(
284
            'style:name',
285
            sprintf('%s_%d_%d', self::ROW_STYLE_PREFIX, $sheetId, $rowDimension->getRowIndex())
286
        );
287
288
        $this->writeRowProperties($rowDimension);
289
290
        // End
291
        $this->writer->endElement(); // Close style:style
292
    }
293
294 37
    public function writeTableStyle(Worksheet $worksheet, int $sheetId): void
295
    {
296 37
        $this->writer->startElement('style:style');
297 37
        $this->writer->writeAttribute('style:family', 'table');
298 37
        $this->writer->writeAttribute(
299 37
            'style:name',
300 37
            sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId)
301 37
        );
302
303 37
        $this->writer->startElement('style:table-properties');
304
305 37
        $this->writer->writeAttribute(
306 37
            'table:display',
307 37
            $worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false'
308 37
        );
309
310 37
        $this->writer->endElement(); // Close style:table-properties
311 37
        $this->writer->endElement(); // Close style:style
312
    }
313
314 37
    public function write(CellStyle $style): void
315
    {
316 37
        $this->writer->startElement('style:style');
317 37
        $this->writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
318 37
        $this->writer->writeAttribute('style:family', 'table-cell');
319 37
        $this->writer->writeAttribute('style:parent-style-name', 'Default');
320
321
        // Alignment, fill colour, etc
322 37
        $this->writeCellProperties($style);
323
324
        // style:text-properties
325 37
        $this->writeTextProperties($style);
326
327
        // End
328 37
        $this->writer->endElement(); // Close style:style
329
    }
330
}
331