Completed
Push — master ( 5fb76c...11bae5 )
by Mark
32s queued 28s
created

Style::writeColumnProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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