Passed
Pull Request — master (#3876)
by Abdul Malik
15:08
created

Style::writeBorderStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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