Passed
Push — master ( dcb10e...d87ef3 )
by Adrien
12:23
created

Style::writeTextProperties()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8

Importance

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