Completed
Pull Request — master (#211)
by Hura
16:07
created

getContentXmlAutomaticStylesSectionContent()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.8571
ccs 13
cts 13
cp 1
cc 3
eloc 13
nc 4
nop 1
crap 3
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 114
    public function registerStyle($style)
26
    {
27 114
        $this->usedFontsSet[$style->getFontName()] = true;
28 114
        return parent::registerStyle($style);
29
    }
30
31
    /**
32
     * @return string[] List of used fonts name
33
     */
34 72
    protected function getUsedFonts()
35
    {
36 72
        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 72
    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 72
EOD;
51
52 72
        $content .= $this->getFontFaceSectionContent();
53 72
        $content .= $this->getStylesSectionContent();
54 72
        $content .= $this->getAutomaticStylesSectionContent($numWorksheets);
55 72
        $content .= $this->getMasterStylesSectionContent($numWorksheets);
56
57
        $content .= <<<EOD
58
</office:document-styles>
59 72
EOD;
60
61 72
        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 72
    protected function getFontFaceSectionContent()
70
    {
71 72
        $content = '<office:font-face-decls>';
72 72
        foreach ($this->getUsedFonts() as $fontName) {
73 72
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
74 72
        }
75 72
        $content .= '</office:font-face-decls>';
76
77 72
        return $content;
78
    }
79
80
    /**
81
     * Returns the content of the "<office:styles>" section, inside "styles.xml" file.
82
     *
83
     * @return string
84
     */
85 72
    protected function getStylesSectionContent()
86
    {
87 72
        $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 72
        <style:text-properties fo:color="#{$defaultStyle->getFontColor()}"
97 72
                               fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt"
98 72
                               style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/>
99
    </style:style>
100 72
</office:styles>
101 72
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 72
    protected function getAutomaticStylesSectionContent($numWorksheets)
111
    {
112 72
        $content = '<office:automatic-styles>';
113
114 72
        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 72
</style:page-layout>
121 72
EOD;
122 72
        }
123
124 72
        $content .= '</office:automatic-styles>';
125
126 72
        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 72
    protected function getMasterStylesSectionContent($numWorksheets)
136
    {
137 72
        $content = '<office:master-styles>';
138
139 72
        for ($i = 1; $i <= $numWorksheets; $i++) {
140
            $content .= <<<EOD
141 72
<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 72
</style:master-page>
147 72
EOD;
148 72
        }
149
150 72
        $content .= '</office:master-styles>';
151
152 72
        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 72
    public function getContentXmlFontFaceSectionContent()
162
    {
163 72
        $content = '<office:font-face-decls>';
164 72
        foreach ($this->getUsedFonts() as $fontName) {
165 72
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
166 72
        }
167 72
        $content .= '</office:font-face-decls>';
168
169 72
        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 72
    public function getContentXmlAutomaticStylesSectionContent($numWorksheets)
179
    {
180 72
        $content = '<office:automatic-styles>';
181
182 72
        foreach ($this->getRegisteredStyles() as $style) {
183 72
            $content .= $this->getStyleSectionContent($style);
184 72
        }
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 72
EOD;
194
195 72
        for ($i = 1; $i <= $numWorksheets; $i++) {
196
            $content .= <<<EOD
197 72
<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 72
</style:style>
200 72
EOD;
201 72
        }
202
203 72
        $content .= '</office:automatic-styles>';
204
205 72
        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 72
    protected function getStyleSectionContent($style)
215
    {
216 72
        $defaultStyle = $this->getDefaultStyle();
217 72
        $styleIndex = $style->getId() + 1; // 1-based
218
219 72
        $content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">';
220
221 72
        if ($style->shouldApplyFont()) {
222 9
            $content .= '<style:text-properties';
223
224 9
            $fontColor = $style->getFontColor();
225 9
            if ($fontColor !== $defaultStyle->getFontColor()) {
226 3
                $content .= ' fo:color="#' . $fontColor . '"';
227 3
            }
228
229 9
            $fontName = $style->getFontName();
230 9
            if ($fontName !== $defaultStyle->getFontName()) {
231 3
                $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
232 3
            }
233
234 9
            $fontSize = $style->getFontSize();
235 9
            if ($fontSize !== $defaultStyle->getFontSize()) {
236 6
                $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
237 6
            }
238
239 9
            if ($style->isFontBold()) {
240 9
                $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
241 9
            }
242 9
            if ($style->isFontItalic()) {
243 3
                $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
244 3
            }
245 9
            if ($style->isFontUnderline()) {
246 3
                $content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
247 3
            }
248 9
            if ($style->isFontStrikethrough()) {
249 3
                $content .= ' style:text-line-through-style="solid"';
250 3
            }
251
252 9
            $content .= '/>';
253 9
        }
254
255 72
        if ($style->shouldWrapText()) {
256 9
            $content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
257 9
        }
258
259 72
        if ($style->shouldApplyBackgroundColor()) {
260
            $content .= sprintf('
261 72
                <style:table-cell-properties fo:background-color="#%s"/>', $style->getBackgroundColor());
262
        }
263
264
        $content .= '</style:style>';
265
266
        return $content;
267
    }
268
}
269