Failed Conditions
Pull Request — master (#209)
by
unknown
02:31
created

StyleHelper::getCellXfsSectionContent()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.025

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 30
ccs 18
cts 20
cp 0.9
rs 8.439
cc 5
eloc 17
nc 9
nop 0
crap 5.025
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Helper;
4
5
use Box\Spout\Writer\Common\Helper\AbstractStyleHelper;
6
use Box\Spout\Writer\Style\Color;
7
8
/**
9
 * Class StyleHelper
10
 * This class provides helper functions to manage styles
11
 *
12
 * @package Box\Spout\Writer\XLSX\Helper
13
 */
14
class StyleHelper extends AbstractStyleHelper
15
{
16
    /**
17
     * Returns the content of the "styles.xml" file, given a list of styles.
18
     *
19
     * @return string
20
     */
21 63
    public function getStylesXMLFileContent()
22
    {
23
        $content = <<<EOD
24
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
25
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
26 63
EOD;
27
28 63
        $content .= $this->getNumberFormatSectionContent();
29 63
        $content .= $this->getFontsSectionContent();
30 63
        $content .= $this->getFillsSectionContent();
31 63
        $content .= $this->getBordersSectionContent();
32 63
        $content .= $this->getCellStyleXfsSectionContent();
33 63
        $content .= $this->getCellXfsSectionContent();
34 63
        $content .= $this->getCellStylesSectionContent();
35
36
        $content .= <<<EOD
37
</styleSheet>
38 63
EOD;
39
40 63
        return $content;
41
    }
42
43 63
    protected function getNumberFormatSectionContent() {
44 63
        $formats = array();
45 63
        $numberFormatCount = 0;
46
        // This is the limit excel holds for the default number formats
47 63
        $baseNumberFormatId = 163;
48
49 63
        foreach($this->getRegisteredStyles() as $style) {
50
            /* If this evals to false we should skip it since it isnt used */
51 63
            if ($style->shouldApplyNumberFormat()) {
52
                $numberFormatCount++;
53
                $style->setNumberFormatId($baseNumberFormatId + $numberFormatCount);
54
                $formats[] = '<numFmt numFmtId="'.$style->getNumberFormatId().'" formatCode="'.$style->getNumberFormat().'"/>';
55
            }
56 63
        }
57
58 63
        if ($numberFormatCount == 0){
59 63
            return '';
60
        }
61
62
        $content = '<numFmts count="'.$numberFormatCount.'">';
63
        $content .= implode('', $formats);
64
        $content .= '</numFmts>';
65
        return $content;
66
    }
67
68
    /**
69
     * Returns the content of the "<fonts>" section.
70
     *
71
     * @return string
72
     */
73 63
    protected function getFontsSectionContent()
74
    {
75 63
        $content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">';
76
77
        /** @var \Box\Spout\Writer\Style\Style $style */
78 63
        foreach ($this->getRegisteredStyles() as $style) {
79 63
            $content .= '<font>';
80
81 63
            $content .= '<sz val="' . $style->getFontSize() . '"/>';
82 63
            $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>';
83 63
            $content .= '<name val="' . $style->getFontName() . '"/>';
84
85 63
            if ($style->isFontBold()) {
86 9
                $content .= '<b/>';
87 9
            }
88 63
            if ($style->isFontItalic()) {
89 3
                $content .= '<i/>';
90 3
            }
91 63
            if ($style->isFontUnderline()) {
92 3
                $content .= '<u/>';
93 3
            }
94 63
            if ($style->isFontStrikethrough()) {
95 3
                $content .= '<strike/>';
96 3
            }
97
98 63
            $content .= '</font>';
99 63
        }
100
101 63
        $content .= '</fonts>';
102
103 63
        return $content;
104
    }
105
106
    /**
107
     * Returns the content of the "<fills>" section.
108
     *
109
     * @return string
110
     */
111 63
    protected function getFillsSectionContent()
112
    {
113
        return <<<EOD
114
<fills count="1">
115
    <fill>
116
        <patternFill patternType="none"/>
117
    </fill>
118
</fills>
119 63
EOD;
120
    }
121
122
    /**
123
     * Returns the content of the "<borders>" section.
124
     *
125
     * @return string
126
     */
127 63
    protected function getBordersSectionContent()
128
    {
129
        return <<<EOD
130
<borders count="1">
131
    <border>
132
        <left/>
133
        <right/>
134
        <top/>
135
        <bottom/>
136
        <diagonal/>
137
    </border>
138
</borders>
139 63
EOD;
140
    }
141
142
    /**
143
     * Returns the content of the "<cellStyleXfs>" section.
144
     *
145
     * @return string
146
     */
147 63
    protected function getCellStyleXfsSectionContent()
148
    {
149
        return <<<EOD
150
<cellStyleXfs count="1">
151
    <xf borderId="0" fillId="0" fontId="0" numFmtId="0"/>
152
</cellStyleXfs>
153 63
EOD;
154
    }
155
156
    /**
157
     * Returns the content of the "<cellXfs>" section.
158
     *
159
     * @return string
160
     */
161 63
    protected function getCellXfsSectionContent()
162
    {
163 63
        $registeredStyles = $this->getRegisteredStyles();
164
165 63
        $content = '<cellXfs count="' . count($registeredStyles) . '">';
166
167 63
        foreach ($registeredStyles as $style) {
168 63
            $content .= '<xf numFmtId="'.$style->getNumberFormatId().'" fontId="' . $style->getId() . '" fillId="0" borderId="0" xfId="0"';
169
170 63
            if ($style->shouldApplyNumberFormat()) {
171
                $content .= ' applyNumberFormat="1"';
172
            }
173
174 63
            if ($style->shouldApplyFont()) {
175 63
                $content .= ' applyFont="1"';
176 63
            }
177
178 63
            if ($style->shouldWrapText()) {
179 6
                $content .= ' applyAlignment="1">';
180 6
                $content .= '<alignment wrapText="1"/>';
181 6
                $content .= '</xf>';
182 6
            } else {
183 63
                $content .= '/>';
184
            }
185 63
        }
186
187 63
        $content .= '</cellXfs>';
188
189 63
        return $content;
190
    }
191
192
    /**
193
     * Returns the content of the "<cellStyles>" section.
194
     *
195
     * @return string
196
     */
197 63
    protected function getCellStylesSectionContent()
198
    {
199
        return <<<EOD
200
<cellStyles count="1">
201
    <cellStyle builtinId="0" name="Normal" xfId="0"/>
202
</cellStyles>
203 63
EOD;
204
    }
205
}
206