Failed Conditions
Pull Request — master (#738)
by
unknown
02:18
created

StyleManager::getFillsSectionContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Manager\Style;
4
5
use Box\Spout\Common\Entity\Style\Color;
6
use Box\Spout\Common\Entity\Style\Style;
7
use Box\Spout\Writer\XLSX\Helper\BorderHelper;
8
9
/**
10
 * Class StyleManager
11
 * Manages styles to be applied to a cell
12
 */
13
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
14
{
15
    /** @var StyleRegistry */
16
    protected $styleRegistry;
17
18
    /**
19
     * For empty cells, we can specify a style or not. If no style are specified,
20
     * then the software default will be applied. But sometimes, it may be useful
21
     * to override this default style, for instance if the cell should have a
22
     * background color different than the default one or some borders
23
     * (fonts property don't really matter here).
24
     *
25
     * @param int $styleId
26
     * @return bool Whether the cell should define a custom style
27
     */
28 11
    public function shouldApplyStyleOnEmptyCell($styleId)
29
    {
30 11
        $associatedFillId = $this->styleRegistry->getFillIdForStyleId($styleId);
31 11
        $hasStyleCustomFill = ($associatedFillId !== null && $associatedFillId !== 0);
32
33 11
        $associatedBorderId = $this->styleRegistry->getBorderIdForStyleId($styleId);
34 11
        $hasStyleCustomBorders = ($associatedBorderId !== null && $associatedBorderId !== 0);
35
36 11
        $associatedFormatId = $this->styleRegistry->getFormatIdForStyleId($styleId);
37 11
        $hasStyleCustomFormats = ($associatedFormatId !== null && $associatedFormatId !== 0);
38
39 11
        return ($hasStyleCustomFill || $hasStyleCustomBorders || $hasStyleCustomFormats);
40
    }
41
42
    /**
43
     * Returns the content of the "styles.xml" file, given a list of styles.
44
     *
45
     * @return string
46
     */
47 38
    public function getStylesXMLFileContent()
48
    {
49
        $content = <<<'EOD'
50 38
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
51
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
52
EOD;
53
54 38
        $content .= $this->getFormatsSectionContent();
55 38
        $content .= $this->getFontsSectionContent();
56 38
        $content .= $this->getFillsSectionContent();
57 38
        $content .= $this->getBordersSectionContent();
58 38
        $content .= $this->getCellStyleXfsSectionContent();
59 38
        $content .= $this->getCellXfsSectionContent();
60 38
        $content .= $this->getCellStylesSectionContent();
61
62
        $content .= <<<'EOD'
63 38
</styleSheet>
64
EOD;
65
66 38
        return $content;
67
    }
68
69
    /**
70
     * Returns the content of the "<numFmts>" section.
71
     *
72
     * @return string
73
     */
74 38
    protected function getFormatsSectionContent()
75
    {
76 38
        $tags = [];
77 38
        $registeredFormats = $this->styleRegistry->getRegisteredFormats();
78 38
        foreach ($registeredFormats as $styleId) {
79 1
            $numFmtId = $this->styleRegistry->getFormatIdForStyleId($styleId);
80
81
            //Built-in formats do not need to be declared, skip them
82 1
            if ($numFmtId < 164) {
83 1
                continue;
84
            }
85
86
            /** @var Style $style */
87 1
            $style = $this->styleRegistry->getStyleFromStyleId($styleId);
88 1
            $format = $style->getFormat();
89 1
            $tags[] = '<numFmt numFmtId="' . $numFmtId . '" formatCode="' . $format . '"/>';
90
        }
91 38
        $content = '<numFmts count="' . \count($tags) . '">';
92 38
        $content .= \implode('', $tags);
93 38
        $content .= '</numFmts>';
94
95 38
        return $content;
96
    }
97
98
    /**
99
     * Returns the content of the "<fonts>" section.
100
     *
101
     * @return string
102
     */
103 38
    protected function getFontsSectionContent()
104
    {
105 38
        $registeredStyles = $this->styleRegistry->getRegisteredStyles();
106
107 38
        $content = '<fonts count="' . \count($registeredStyles) . '">';
108
109
        /** @var Style $style */
110 38
        foreach ($registeredStyles as $style) {
111 38
            $content .= '<font>';
112
113 38
            $content .= '<sz val="' . $style->getFontSize() . '"/>';
114 38
            $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>';
115 38
            $content .= '<name val="' . $style->getFontName() . '"/>';
116
117 38
            if ($style->isFontBold()) {
118 8
                $content .= '<b/>';
119
            }
120 38
            if ($style->isFontItalic()) {
121 1
                $content .= '<i/>';
122
            }
123 38
            if ($style->isFontUnderline()) {
124 2
                $content .= '<u/>';
125
            }
126 38
            if ($style->isFontStrikethrough()) {
127 1
                $content .= '<strike/>';
128
            }
129
130 38
            $content .= '</font>';
131
        }
132
133 38
        $content .= '</fonts>';
134
135 38
        return $content;
136
    }
137
138
    /**
139
     * Returns the content of the "<fills>" section.
140
     *
141
     * @return string
142
     */
143 38
    protected function getFillsSectionContent()
144
    {
145 38
        $registeredFills = $this->styleRegistry->getRegisteredFills();
146
147
        // Excel reserves two default fills
148 38
        $fillsCount = \count($registeredFills) + 2;
149 38
        $content = \sprintf('<fills count="%d">', $fillsCount);
150
151 38
        $content .= '<fill><patternFill patternType="none"/></fill>';
152 38
        $content .= '<fill><patternFill patternType="gray125"/></fill>';
153
154
        // The other fills are actually registered by setting a background color
155 38
        foreach ($registeredFills as $styleId) {
156
            /** @var Style $style */
157 3
            $style = $this->styleRegistry->getStyleFromStyleId($styleId);
158
159 3
            $backgroundColor = $style->getBackgroundColor();
160 3
            $content .= \sprintf(
161 3
                '<fill><patternFill patternType="solid"><fgColor rgb="%s"/></patternFill></fill>',
162 3
                $backgroundColor
163
            );
164
        }
165
166 38
        $content .= '</fills>';
167
168 38
        return $content;
169
    }
170
171
    /**
172
     * Returns the content of the "<borders>" section.
173
     *
174
     * @return string
175
     */
176 38
    protected function getBordersSectionContent()
177
    {
178 38
        $registeredBorders = $this->styleRegistry->getRegisteredBorders();
179
180
        // There is one default border with index 0
181 38
        $borderCount = \count($registeredBorders) + 1;
182
183 38
        $content = '<borders count="' . $borderCount . '">';
184
185
        // Default border starting at index 0
186 38
        $content .= '<border><left/><right/><top/><bottom/></border>';
187
188 38
        foreach ($registeredBorders as $styleId) {
189
            /** @var \Box\Spout\Common\Entity\Style\Style $style */
190 4
            $style = $this->styleRegistry->getStyleFromStyleId($styleId);
191 4
            $border = $style->getBorder();
192 4
            $content .= '<border>';
193
194
            // @link https://github.com/box/spout/issues/271
195 4
            $sortOrder = ['left', 'right', 'top', 'bottom'];
196
197 4
            foreach ($sortOrder as $partName) {
198 4
                if ($border->hasPart($partName)) {
199
                    /** @var $part \Box\Spout\Common\Entity\Style\BorderPart */
200 4
                    $part = $border->getPart($partName);
201 4
                    $content .= BorderHelper::serializeBorderPart($part);
202
                }
203
            }
204
205 4
            $content .= '</border>';
206
        }
207
208 38
        $content .= '</borders>';
209
210 38
        return $content;
211
    }
212
213
    /**
214
     * Returns the content of the "<cellStyleXfs>" section.
215
     *
216
     * @return string
217
     */
218 38
    protected function getCellStyleXfsSectionContent()
219
    {
220
        return <<<'EOD'
221 38
<cellStyleXfs count="1">
222
    <xf borderId="0" fillId="0" fontId="0" numFmtId="0"/>
223
</cellStyleXfs>
224
EOD;
225
    }
226
227
    /**
228
     * Returns the content of the "<cellXfs>" section.
229
     *
230
     * @return string
231
     */
232 38
    protected function getCellXfsSectionContent()
233
    {
234 38
        $registeredStyles = $this->styleRegistry->getRegisteredStyles();
235
236 38
        $content = '<cellXfs count="' . \count($registeredStyles) . '">';
237
238 38
        foreach ($registeredStyles as $style) {
239 38
            $styleId = $style->getId();
240 38
            $fillId = $this->getFillIdForStyleId($styleId);
241 38
            $borderId = $this->getBorderIdForStyleId($styleId);
242 38
            $numFmtId = $this->getFormatIdForStyleId($styleId);
243
244 38
            $content .= '<xf numFmtId="' . $numFmtId . '" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
245
246 38
            if ($style->shouldApplyFont()) {
247 38
                $content .= ' applyFont="1"';
248
            }
249
250 38
            $content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
251
252 38
            if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
253 3
                $content .= ' applyAlignment="1">';
254 3
                $content .= '<alignment';
255 3
                if ($style->shouldApplyCellAlignment()) {
256 1
                    $content .= \sprintf(' horizontal="%s"', $style->getCellAlignment());
257
                }
258 3
                if ($style->shouldWrapText()) {
259 2
                    $content .= ' wrapText="1"';
260
                }
261 3
                if ($style->shouldShrinkToFit()) {
262
                    $content .= ' shrinkToFit="true"';
263
                }
264
265 3
                $content .= '/>';
266 3
                $content .= '</xf>';
267
            } else {
268 38
                $content .= '/>';
269
            }
270
        }
271
272 38
        $content .= '</cellXfs>';
273
274 38
        return $content;
275
    }
276
277
    /**
278
     * Returns the fill ID associated to the given style ID.
279
     * For the default style, we don't a fill.
280
     *
281
     * @param int $styleId
282
     * @return int
283
     */
284 38
    private function getFillIdForStyleId($styleId)
285
    {
286
        // For the default style (ID = 0), we don't want to override the fill.
287
        // Otherwise all cells of the spreadsheet will have a background color.
288 38
        $isDefaultStyle = ($styleId === 0);
289
290 38
        return $isDefaultStyle ? 0 : ($this->styleRegistry->getFillIdForStyleId($styleId) ?: 0);
291
    }
292
293
    /**
294
     * Returns the fill ID associated to the given style ID.
295
     * For the default style, we don't a border.
296
     *
297
     * @param int $styleId
298
     * @return int
299
     */
300 38
    private function getBorderIdForStyleId($styleId)
301
    {
302
        // For the default style (ID = 0), we don't want to override the border.
303
        // Otherwise all cells of the spreadsheet will have a border.
304 38
        $isDefaultStyle = ($styleId === 0);
305
306 38
        return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0);
307
    }
308
309
    /**
310
     * Returns the format ID associated to the given style ID.
311
     * For the default style use general format.
312
     *
313
     * @param int $styleId
314
     * @return int
315
     */
316 38
    private function getFormatIdForStyleId($styleId)
317
    {
318
        // For the default style (ID = 0), we don't want to override the format.
319
        // Otherwise all cells of the spreadsheet will have a format.
320 38
        $isDefaultStyle = ($styleId === 0);
321
322 38
        return $isDefaultStyle ? 0 : ($this->styleRegistry->getFormatIdForStyleId($styleId) ?: 0);
323
    }
324
325
    /**
326
     * Returns the content of the "<cellStyles>" section.
327
     *
328
     * @return string
329
     */
330 38
    protected function getCellStylesSectionContent()
331
    {
332
        return <<<'EOD'
333 38
<cellStyles count="1">
334
    <cellStyle builtinId="0" name="Normal" xfId="0"/>
335
</cellStyles>
336
EOD;
337
    }
338
}
339