|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Manager\Style; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Style\Style; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class StyleMerger |
|
9
|
|
|
* Takes care of merging styles together |
|
10
|
|
|
*/ |
|
11
|
|
|
class StyleMerger |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Merges the current style with the given style, using the given style as a base. This means that: |
|
15
|
|
|
* - if current style and base style both have property A set, use current style property's value |
|
16
|
|
|
* - if current style has property A set but base style does not, use current style property's value |
|
17
|
|
|
* - if base style has property A set but current style does not, use base style property's value |
|
18
|
|
|
* |
|
19
|
|
|
* @NOTE: This function returns a new style. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Style $style |
|
22
|
|
|
* @param Style $baseStyle |
|
23
|
|
|
* @return Style New style corresponding to the merge of the 2 styles |
|
24
|
|
|
*/ |
|
25
|
77 |
|
public function merge(Style $style, Style $baseStyle) |
|
26
|
|
|
{ |
|
27
|
77 |
|
$mergedStyle = clone $style; |
|
28
|
|
|
|
|
29
|
77 |
|
$this->mergeFontStyles($mergedStyle, $style, $baseStyle); |
|
30
|
77 |
|
$this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle); |
|
31
|
77 |
|
$this->mergeCellProperties($mergedStyle, $style, $baseStyle); |
|
32
|
|
|
|
|
33
|
77 |
|
return $mergedStyle; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Style $styleToUpdate (passed as reference) |
|
38
|
|
|
* @param Style $style |
|
39
|
|
|
* @param Style $baseStyle |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
77 |
|
private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle) |
|
43
|
|
|
{ |
|
44
|
77 |
|
if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) { |
|
45
|
3 |
|
$styleToUpdate->setFontBold(); |
|
46
|
|
|
} |
|
47
|
77 |
|
if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) { |
|
48
|
1 |
|
$styleToUpdate->setFontItalic(); |
|
49
|
|
|
} |
|
50
|
77 |
|
if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) { |
|
51
|
1 |
|
$styleToUpdate->setFontUnderline(); |
|
52
|
|
|
} |
|
53
|
77 |
|
if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) { |
|
54
|
1 |
|
$styleToUpdate->setFontStrikethrough(); |
|
55
|
|
|
} |
|
56
|
77 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Style $styleToUpdate Style to update (passed as reference) |
|
60
|
|
|
* @param Style $style |
|
61
|
|
|
* @param Style $baseStyle |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
77 |
|
private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle) |
|
65
|
|
|
{ |
|
66
|
77 |
|
if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) { |
|
67
|
37 |
|
$styleToUpdate->setFontSize($baseStyle->getFontSize()); |
|
68
|
|
|
} |
|
69
|
77 |
|
if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) { |
|
70
|
1 |
|
$styleToUpdate->setFontColor($baseStyle->getFontColor()); |
|
71
|
|
|
} |
|
72
|
77 |
|
if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) { |
|
73
|
34 |
|
$styleToUpdate->setFontName($baseStyle->getFontName()); |
|
74
|
|
|
} |
|
75
|
77 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Style $styleToUpdate Style to update (passed as reference) |
|
79
|
|
|
* @param Style $style |
|
80
|
|
|
* @param Style $baseStyle |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
77 |
|
private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle) |
|
84
|
|
|
{ |
|
85
|
77 |
|
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) { |
|
86
|
1 |
|
$styleToUpdate->setShouldWrapText(); |
|
87
|
|
|
} |
|
88
|
77 |
|
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) { |
|
89
|
|
|
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment()); |
|
90
|
|
|
} |
|
91
|
77 |
|
if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) { |
|
92
|
1 |
|
$styleToUpdate->setBorder($baseStyle->getBorder()); |
|
93
|
|
|
} |
|
94
|
77 |
|
if (!$style->getFormat() && $baseStyle->shouldApplyFormat()) { |
|
95
|
1 |
|
$styleToUpdate->setFormat($baseStyle->getFormat()); |
|
96
|
|
|
} |
|
97
|
77 |
|
if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) { |
|
98
|
1 |
|
$styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor()); |
|
99
|
|
|
} |
|
100
|
77 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|