Failed Conditions
Pull Request — develop_3.0 (#434)
by Hura
03:16
created

StyleManager::getTextPropertiesSectionContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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