Completed
Push — master ( ef4a32...9ce774 )
by Adrien
03:04
created

StyleHelper::getBorderXMLContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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
     */
26 129
    public function registerStyle($style)
27
    {
28 129
        $this->usedFontsSet[$style->getFontName()] = true;
29 129
        return parent::registerStyle($style);
30
    }
31
32
    /**
33
     * @return string[] List of used fonts name
34
     */
35 90
    protected function getUsedFonts()
36
    {
37 90
        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
     */
46 90
    public function getStylesXMLFileContent($numWorksheets)
47
    {
48
        $content = <<<EOD
49
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
50
<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 90
EOD;
52
53 90
        $content .= $this->getFontFaceSectionContent();
54 90
        $content .= $this->getStylesSectionContent();
55 90
        $content .= $this->getAutomaticStylesSectionContent($numWorksheets);
56 90
        $content .= $this->getMasterStylesSectionContent($numWorksheets);
57
58
        $content .= <<<EOD
59
</office:document-styles>
60 90
EOD;
61
62 90
        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
     */
70 90
    protected function getFontFaceSectionContent()
71
    {
72 90
        $content = '<office:font-face-decls>';
73 90
        foreach ($this->getUsedFonts() as $fontName) {
74 90
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
75 90
        }
76 90
        $content .= '</office:font-face-decls>';
77
78 90
        return $content;
79
    }
80
81
    /**
82
     * Returns the content of the "<office:styles>" section, inside "styles.xml" file.
83
     *
84
     * @return string
85
     */
86 90
    protected function getStylesSectionContent()
87
    {
88 90
        $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
        <style:table-cell-properties fo:background-color="transparent" style:vertical-align="automatic"/>
97 90
        <style:text-properties fo:color="#{$defaultStyle->getFontColor()}"
98 90
                               fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt"
99 90
                               style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/>
100
    </style:style>
101 90
</office:styles>
102 90
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
     */
111 90
    protected function getAutomaticStylesSectionContent($numWorksheets)
112
    {
113 90
        $content = '<office:automatic-styles>';
114
115 90
        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
    <style:footer-style/>
121 90
</style:page-layout>
122 90
EOD;
123 90
        }
124
125 90
        $content .= '</office:automatic-styles>';
126
127 90
        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
     */
136 90
    protected function getMasterStylesSectionContent($numWorksheets)
137
    {
138 90
        $content = '<office:master-styles>';
139
140 90
        for ($i = 1; $i <= $numWorksheets; $i++) {
141
            $content .= <<<EOD
142 90
<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
    <style:footer-left style:display="false"/>
147 90
</style:master-page>
148 90
EOD;
149 90
        }
150
151 90
        $content .= '</office:master-styles>';
152
153 90
        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
     */
162 90
    public function getContentXmlFontFaceSectionContent()
163
    {
164 90
        $content = '<office:font-face-decls>';
165 90
        foreach ($this->getUsedFonts() as $fontName) {
166 90
            $content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
167 90
        }
168 90
        $content .= '</office:font-face-decls>';
169
170 90
        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
     */
179 90
    public function getContentXmlAutomaticStylesSectionContent($numWorksheets)
180
    {
181 90
        $content = '<office:automatic-styles>';
182
183 90
        foreach ($this->getRegisteredStyles() as $style) {
184 90
            $content .= $this->getStyleSectionContent($style);
185 90
        }
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
</style:style>
194 90
EOD;
195
196 90
        for ($i = 1; $i <= $numWorksheets; $i++) {
197
            $content .= <<<EOD
198 90
<style:style style:family="table" style:master-page-name="mp$i" style:name="ta$i">
199
    <style:table-properties style:writing-mode="lr-tb" table:display="true"/>
200 90
</style:style>
201 90
EOD;
202 90
        }
203
204 90
        $content .= '</office:automatic-styles>';
205
206 90
        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
     */
215 90
    protected function getStyleSectionContent($style)
216
    {
217 90
        $styleIndex = $style->getId() + 1; // 1-based
218
219 90
        $content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">';
220
221 90
        $content .= $this->getTextPropertiesSectionContent($style);
222 90
        $content .= $this->getTableCellPropertiesSectionContent($style);
223
224 90
        $content .= '</style:style>';
225
226 90
        return $content;
227
    }
228
229
    /**
230
     * Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
231
     *
232
     * @param \Box\Spout\Writer\Style\Style $style
233
     * @return string
234
     */
235 90
    private function getTextPropertiesSectionContent($style)
236
    {
237 90
        $content = '';
238
239 90
        if ($style->shouldApplyFont()) {
240 12
            $content .= $this->getFontSectionContent($style);
241 12
        }
242
243 90
        return $content;
244
    }
245
246
    /**
247
     * Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
248
     *
249
     * @param \Box\Spout\Writer\Style\Style $style
250
     * @return string
251
     */
252 12
    private function getFontSectionContent($style)
253
    {
254 12
        $defaultStyle = $this->getDefaultStyle();
255
256 12
        $content = '<style:text-properties';
257
258 12
        $fontColor = $style->getFontColor();
259 12
        if ($fontColor !== $defaultStyle->getFontColor()) {
260 3
            $content .= ' fo:color="#' . $fontColor . '"';
261 3
        }
262
263 12
        $fontName = $style->getFontName();
264 12
        if ($fontName !== $defaultStyle->getFontName()) {
265 3
            $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
266 3
        }
267
268 12
        $fontSize = $style->getFontSize();
269 12
        if ($fontSize !== $defaultStyle->getFontSize()) {
270 6
            $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
271 6
        }
272
273 12
        if ($style->isFontBold()) {
274 9
            $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
275 9
        }
276 12
        if ($style->isFontItalic()) {
277 3
            $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
278 3
        }
279 12
        if ($style->isFontUnderline()) {
280 3
            $content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
281 3
        }
282 12
        if ($style->isFontStrikethrough()) {
283 3
            $content .= ' style:text-line-through-style="solid"';
284 3
        }
285
286 12
        $content .= '/>';
287
288 12
        return $content;
289
    }
290
291
    /**
292
     * Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
293
     *
294
     * @param \Box\Spout\Writer\Style\Style $style
295
     * @return string
296
     */
297 90
    private function getTableCellPropertiesSectionContent($style)
298
    {
299 90
        $content = '';
300
301 90
        if ($style->shouldWrapText()) {
302 9
            $content .= $this->getWrapTextXMLContent();
303 9
        }
304
305 90
        if ($style->shouldApplyBorder()) {
306 3
            $content .= $this->getBorderXMLContent($style);
307 3
        }
308
309 90
        if ($style->shouldApplyBackgroundColor()) {
310 6
            $content .= $this->getBackgroundColorXMLContent($style);
311 6
        }
312
313 90
        return $content;
314
    }
315
316
    /**
317
     * Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section
318
     *
319
     * @return string
320
     */
321 9
    private function getWrapTextXMLContent()
322
    {
323 9
        return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
324
    }
325
326
    /**
327
     * Returns the contents of the borders definition for the "<style:table-cell-properties>" section
328
     *
329
     * @param \Box\Spout\Writer\Style\Style $style
330
     * @return string
331
     */
332 3
    private function getBorderXMLContent($style)
333
    {
334 3
        $borderProperty = '<style:table-cell-properties %s />';
335
336 3
        $borders = array_map(function (BorderPart $borderPart) {
337 3
            return BorderHelper::serializeBorderPart($borderPart);
338 3
        }, $style->getBorder()->getParts());
339
340 3
        return sprintf($borderProperty, implode(' ', $borders));
341
    }
342
343
    /**
344
     * Returns the contents of the background color definition for the "<style:table-cell-properties>" section
345
     *
346
     * @param \Box\Spout\Writer\Style\Style $style
347
     * @return string
348
     */
349 6
    private function getBackgroundColorXMLContent($style)
350
    {
351 6
        return sprintf(
352 6
            '<style:table-cell-properties fo:background-color="#%s"/>',
353 6
            $style->getBackgroundColor()
354 6
        );
355
    }
356
}
357