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