Failed Conditions
Push — perf-tests ( 50942d...2fc93e )
by Adrien
14:53
created

StyleHelper::getStyleSectionContent()   F

Complexity

Conditions 10
Paths 258

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 49
rs 3.7894
cc 10
eloc 28
nc 258
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Box\Spout\Writer\ODS\Helper;
4
5
use Box\Spout\Writer\Common\Helper\AbstractStyleHelper;
6
7
/**
8
 * Class StyleHelper
9
 * This class provides helper functions to manage styles
10
 *
11
 * @package Box\Spout\Writer\ODS\Helper
12
 */
13
class StyleHelper extends AbstractStyleHelper
14
{
15
    /** @var string[] [FONT_NAME] => [] Map whose keys contain all the fonts used */
16
    protected $usedFontsSet = [];
17
18
    /**
19
     * Registers the given style as a used style.
20
     * Duplicate styles won't be registered more than once.
21
     *
22
     * @param \Box\Spout\Writer\Style\Style $style The style to be registered
23
     * @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID.
24
     */
25
    public function registerStyle($style)
26
    {
27
        $this->usedFontsSet[$style->getFontName()] = true;
28
        return parent::registerStyle($style);
29
    }
30
31
    /**
32
     * @return string[] List of used fonts name
33
     */
34
    protected function getUsedFonts()
35
    {
36
        return array_keys($this->usedFontsSet);
37
    }
38
39
    /**
40
     * Returns the content of the "styles.xml" file, given a list of styles.
41
     *
42
     * @param int $numWorksheets Number of worksheets created
43
     * @return string
44
     */
45
    public function getStylesXMLFileContent($numWorksheets)
46
    {
47
        $content = <<<EOD
48
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
49
<office:document-styles office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:msoxl="http://schemas.microsoft.com/office/excel/formula" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
50
EOD;
51
52
        $content .= $this->getFontFaceSectionContent();
53
        $content .= $this->getStylesSectionContent();
54
        $content .= $this->getAutomaticStylesSectionContent($numWorksheets);
55
        $content .= $this->getMasterStylesSectionContent($numWorksheets);
56
57
        $content .= <<<EOD
58
</office:document-styles>
59
EOD;
60
61
        return $content;
62
    }
63
64
    /**
65
     * Returns the content of the "<office:font-face-decls>" section, inside "styles.xml" file.
66
     *
67
     * @return string
68
     */
69
    protected function getFontFaceSectionContent()
70
    {
71
        $content = '<office:font-face-decls>';
72
        foreach ($this->getUsedFonts() as $fontName) {
73
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
74
        }
75
        $content .= '</office:font-face-decls>';
76
77
        return $content;
78
    }
79
80
    /**
81
     * Returns the content of the "<office:styles>" section, inside "styles.xml" file.
82
     *
83
     * @return string
84
     */
85
    protected function getStylesSectionContent()
86
    {
87
        $defaultStyle = $this->getDefaultStyle();
88
89
        return <<<EOD
90
<office:styles>
91
    <number:number-style style:name="N0">
92
        <number:number number:min-integer-digits="1"/>
93
    </number:number-style>
94
    <style:style style:data-style-name="N0" style:family="table-cell" style:name="Default">
95
        <style:table-cell-properties fo:background-color="transparent" style:vertical-align="automatic"/>
96
        <style:text-properties fo:color="#{$defaultStyle->getFontColor()}"
97
                               fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt"
98
                               style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/>
99
    </style:style>
100
</office:styles>
101
EOD;
102
    }
103
104
    /**
105
     * Returns the content of the "<office:automatic-styles>" section, inside "styles.xml" file.
106
     *
107
     * @param int $numWorksheets Number of worksheets created
108
     * @return string
109
     */
110
    protected function getAutomaticStylesSectionContent($numWorksheets)
111
    {
112
        $content = '<office:automatic-styles>';
113
114
        for ($i = 1; $i <= $numWorksheets; $i++) {
115
            $content .= <<<EOD
116
<style:page-layout style:name="pm$i">
117
    <style:page-layout-properties style:first-page-number="continue" style:print="objects charts drawings" style:table-centering="none"/>
118
    <style:header-style/>
119
    <style:footer-style/>
120
</style:page-layout>
121
EOD;
122
        }
123
124
        $content .= '</office:automatic-styles>';
125
126
        return $content;
127
    }
128
129
    /**
130
     * Returns the content of the "<office:master-styles>" section, inside "styles.xml" file.
131
     *
132
     * @param int $numWorksheets Number of worksheets created
133
     * @return string
134
     */
135
    protected function getMasterStylesSectionContent($numWorksheets)
136
    {
137
        $content = '<office:master-styles>';
138
139
        for ($i = 1; $i <= $numWorksheets; $i++) {
140
            $content .= <<<EOD
141
<style:master-page style:name="mp$i" style:page-layout-name="pm$i">
142
    <style:header/>
143
    <style:header-left style:display="false"/>
144
    <style:footer/>
145
    <style:footer-left style:display="false"/>
146
</style:master-page>
147
EOD;
148
        }
149
150
        $content .= '</office:master-styles>';
151
152
        return $content;
153
    }
154
155
156
    /**
157
     * Returns the contents of the "<office:font-face-decls>" section, inside "content.xml" file.
158
     *
159
     * @return string
160
     */
161
    public function getContentXmlFontFaceSectionContent()
162
    {
163
        $content = '<office:font-face-decls>';
164
        foreach ($this->getUsedFonts() as $fontName) {
165
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
166
        }
167
        $content .= '</office:font-face-decls>';
168
169
        return $content;
170
    }
171
172
    /**
173
     * Returns the contents of the "<office:automatic-styles>" section, inside "content.xml" file.
174
     *
175
     * @param int $numWorksheets Number of worksheets created
176
     * @return string
177
     */
178
    public function getContentXmlAutomaticStylesSectionContent($numWorksheets)
179
    {
180
        $content = '<office:automatic-styles>';
181
182
        foreach ($this->getRegisteredStyles() as $style) {
183
            $content .= $this->getStyleSectionContent($style);
184
        }
185
186
        $content .= <<<EOD
187
<style:style style:family="table-column" style:name="co1">
188
    <style:table-column-properties fo:break-before="auto"/>
189
</style:style>
190
<style:style style:family="table-row" style:name="ro1">
191
    <style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/>
192
</style:style>
193
EOD;
194
195
        for ($i = 1; $i <= $numWorksheets; $i++) {
196
            $content .= <<<EOD
197
<style:style style:family="table" style:master-page-name="mp$i" style:name="ta$i">
198
    <style:table-properties style:writing-mode="lr-tb" table:display="true"/>
199
</style:style>
200
EOD;
201
        }
202
203
        $content .= '</office:automatic-styles>';
204
205
        return $content;
206
    }
207
208
    /**
209
     * Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section
210
     *
211
     * @param \Box\Spout\Writer\Style\Style $style
212
     * @return string
213
     */
214
    protected function getStyleSectionContent($style)
215
    {
216
        $defaultStyle = $this->getDefaultStyle();
217
        $styleIndex = $style->getId() + 1; // 1-based
218
219
        $content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">';
220
221
        if ($style->shouldApplyFont()) {
222
            $content .= '<style:text-properties';
223
224
            $fontColor = $style->getFontColor();
225
            if ($fontColor !== $defaultStyle->getFontColor()) {
226
                $content .= ' fo:color="#' . $fontColor . '"';
227
            }
228
229
            $fontName = $style->getFontName();
230
            if ($fontName !== $defaultStyle->getFontName()) {
231
                $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
232
            }
233
234
            $fontSize = $style->getFontSize();
235
            if ($fontSize !== $defaultStyle->getFontSize()) {
236
                $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
237
            }
238
239
            if ($style->isFontBold()) {
240
                $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
241
            }
242
            if ($style->isFontItalic()) {
243
                $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
244
            }
245
            if ($style->isFontUnderline()) {
246
                $content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
247
            }
248
            if ($style->isFontStrikethrough()) {
249
                $content .= ' style:text-line-through-style="solid"';
250
            }
251
252
            $content .= '/>';
253
        }
254
255
        if ($style->shouldWrapText()) {
256
            $content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
257
        }
258
259
        $content .= '</style:style>';
260
261
        return $content;
262
    }
263
264
}
265