Completed
Pull Request — develop_3.0 (#434)
by Hura
04:17
created

StyleManager::merge()   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 2
crap 1
1
<?php
2
3
namespace Box\Spout\Writer\Common\Manager;
4
5
use Box\Spout\Writer\Common\Entity\Style\Style;
6
7
/**
8
 * Class StyleManager
9
 * Manages styles to be applied to a cell
10
 *
11
 * @package Box\Spout\Writer\Common\Manager\Style
12
 */
13
class StyleManager
14
{
15
    /**
16
     * Serializes the style for future comparison with other styles.
17
     * The ID is excluded from the comparison, as we only care about
18
     * actual style properties.
19
     *
20
     * @param Style $style
21
     * @return string The serialized style
22
     */
23 101
    public function serialize(Style $style)
24
    {
25
        // In order to be able to properly compare style, set static ID value
26 101
        $currentId = $style->getId();
27 101
        $style->setId(0);
28
29 101
        $serializedStyle = serialize($style);
30
31 101
        $style->setId($currentId);
32
33 101
        return $serializedStyle;
34
    }
35
36
    /**
37
     * Merges the current style with the given style, using the given style as a base. This means that:
38
     *   - if current style and base style both have property A set, use current style property's value
39
     *   - if current style has property A set but base style does not, use current style property's value
40
     *   - if base style has property A set but current style does not, use base style property's value
41
     *
42
     * @NOTE: This function returns a new style.
43
     *
44
     * @param Style $style
45
     * @param Style $baseStyle
46
     * @return Style New style corresponding to the merge of the 2 styles
47
     */
48 72
    public function merge(Style $style, Style $baseStyle)
49
    {
50 72
        $mergedStyle = clone $style;
51
52 72
        $this->mergeFontStyles($mergedStyle, $style, $baseStyle);
53 72
        $this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle);
54 72
        $this->mergeCellProperties($mergedStyle, $style, $baseStyle);
55
56 72
        return $mergedStyle;
57
    }
58
59
    /**
60
     * @param Style $styleToUpdate (passed as reference)
61
     * @param Style $style
62
     * @param Style $baseStyle
63
     * @return void
64
     */
65 72
    private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle)
66
    {
67 72
        if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) {
68 10
            $styleToUpdate->setFontBold();
69
        }
70 72
        if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) {
71 3
            $styleToUpdate->setFontItalic();
72
        }
73 72
        if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) {
74 3
            $styleToUpdate->setFontUnderline();
75
        }
76 72
        if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) {
77 3
            $styleToUpdate->setFontStrikethrough();
78
        }
79 72
    }
80
81
    /**
82
     * @param Style $styleToUpdate Style to update (passed as reference)
83
     * @param Style $style
84
     * @param Style $baseStyle
85
     * @return void
86
     */
87 72
    private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
88
    {
89 72
        if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) {
90 37
            $styleToUpdate->setFontSize($baseStyle->getFontSize());
91
        }
92 72
        if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) {
93 2
            $styleToUpdate->setFontColor($baseStyle->getFontColor());
94
        }
95 72
        if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) {
96 33
            $styleToUpdate->setFontName($baseStyle->getFontName());
97
        }
98 72
    }
99
100
    /**
101
     * @param Style $styleToUpdate Style to update (passed as reference)
102
     * @param Style $style
103
     * @param Style $baseStyle
104
     * @return void
105
     */
106 72
    private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
107
    {
108 72
        if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
109 3
            $styleToUpdate->setShouldWrapText();
110
        }
111 72
        if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) {
112 6
            $styleToUpdate->setBorder($baseStyle->getBorder());
113
        }
114 72
        if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) {
115 5
            $styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor());
116
        }
117 72
    }
118
}
119