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