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