Completed
Pull Request — master (#200)
by Hura
11:35
created

StyleHelper::getStylesSectionContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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