Completed
Push — master ( ee4360...ba4c10 )
by Stefan
02:31
created

StyleHelper::buildStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author neun
4
 * @since  2016-07-03
5
 */
6
7
namespace OneSheet;
8
9
/**
10
 * Class StyleHelper to build new style strings for style.xml. Every style
11
 * has it's own font, fill, cellXfs since it's that much simpler to handle
12
 * and less prone to error.
13
 * The first 2 styles (index 0 and 1) are "reserved", because no matter
14
 * what ... the second <fill> always ended up as grey125.
15
 *
16
 * @package OneSheet
17
 */
18
class StyleHelper
19
{
20
    /**
21
     * Increase style index with every built style.
22
     *
23
     * @var int
24
     */
25
    private static $styleIndex = 1;
26
27
    /**
28
     * Concatenated <font> strings.
29
     *
30
     * @var string
31
     */
32
    private static $fontXml = '<font><sz val="11"/><color rgb="000000"/><name val="Calibri"/></font><font><sz val="11"/><color rgb="000000"/><name val="Calibri"/></font>';
33
34
    /**
35
     * Concatenated <fill> strings
36
     *
37
     * @var string
38
     */
39
    private static $fillXml = '<fill><patternFill patternType="none"/></fill><fill><patternFill patternType="grey125"/></fill>';
40
41
    /**
42
     * Build a single new style based on given params and return it's id.
43
     * Every newly built style will result in a completely new style set.
44
     *
45
     * @param Style $style
46
     * @return string
47
     */
48 2
    public static function buildStyle(Style $style)
49
    {
50 2
        self::$styleIndex++;
51 2
        self::$fontXml .= $style->getFontXml();
52 2
        self::$fillXml .= $style->getFillXml();
53
54 2
        return StyleHelper::$styleIndex;
55
    }
56
57
    /**
58
     * Return entire <fills> part for style.xml.
59
     *
60
     * @return string
61
     */
62 1
    public static function getFillsXml()
63
    {
64 1
        return '<fills count="' . (self::$styleIndex+1) . '">' . self::$fillXml . '</fills>';
65
    }
66
67
    /**
68
     * Return entire <fonts> part for style.xml.
69
     *
70
     * @return string
71
     */
72 1
    public static function getFontsXml()
73
    {
74 1
        return '<fonts count="' . (self::$styleIndex+1) . '">' . self::$fontXml . '</fonts>';
75
    }
76
77
    /**
78
     * Return entire cellXfs part for style.xml.
79
     *
80
     * @return string
81
     */
82 1
    public static function getCellXfsXml()
83
    {
84 1
        $xml = '<cellXfs count="' . (self::$styleIndex+1) . '">';
85
86 1
        for ($i = 0; $i <= self::$styleIndex; $i++) {
87 1
            $xml .= '<xf numFmtId="0" fontId="' . $i . '" fillId="' . $i . '" borderId="0" xfId="0" applyFont="1"/>';
88 1
        }
89
90 1
        return $xml . '</cellXfs>';
91
    }
92
}
93