Jon Nott 887d6ef033 Add setCellVerticalAlignment() & allow setShouldWrapText() to be explicitly set as false
- add Common\Entity\Style\CellVerticalAlignment
- duplicate get/set/hasSet/shouldApply methods for CellAlignment in Common\Entity\Style\Style for CellVerticalAlignment instead, plus corresponding properties
- add setCellVerticalAlignment method to Common\Creator\Style\StyleBuilder
- add vertical alignment to StyleMerger:: mergeCellProperties()
- adjust wrapText logic in mergeCellProperties() to fix issue https://github.com/box/spout/issues/829
- apply vertical cell styling for both XLSX and ODS, via corresponding StyleManager classes
- transform vertical alignment  ‘center’ to ‘middle’ for ODS
- fix logic around wrapText such that the choice whether to include wrapping styles depends on hasSetWrapText() being true, and then use shouldWrapText() thereafter to either set wrapping or no wrapping (for XLSX, wrapText=“1” or wrapText=“0”, for ODS, wrap-option=wrap or wrap-option=no-wrap). previously there was no way to set wrapping to be OFF, only to set it to be ON.
- add new tests to ensure shouldWrapText(false) results in the correct negated wrapText (XLSX) / wrap-option (ODS) styles
- add new tests to StyleBuilderTest for vertical alignment
- add vertical alignment to documentation.md
2022-02-12 19:37:05 +00:00

105 lines
4.0 KiB
PHP

<?php
namespace Box\Spout\Writer\Common\Manager\Style;
use Box\Spout\Common\Entity\Style\Style;
/**
* Class StyleMerger
* Takes care of merging styles together
*/
class StyleMerger
{
/**
* Merges the current style with the given style, using the given style as a base. This means that:
* - if current style and base style both have property A set, use current style property's value
* - if current style has property A set but base style does not, use current style property's value
* - if base style has property A set but current style does not, use base style property's value
*
* @NOTE: This function returns a new style.
*
* @param Style $style
* @param Style $baseStyle
* @return Style New style corresponding to the merge of the 2 styles
*/
public function merge(Style $style, Style $baseStyle)
{
$mergedStyle = clone $style;
$this->mergeFontStyles($mergedStyle, $style, $baseStyle);
$this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle);
$this->mergeCellProperties($mergedStyle, $style, $baseStyle);
return $mergedStyle;
}
/**
* @param Style $styleToUpdate (passed as reference)
* @param Style $style
* @param Style $baseStyle
* @return void
*/
private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle)
{
if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) {
$styleToUpdate->setFontBold();
}
if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) {
$styleToUpdate->setFontItalic();
}
if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) {
$styleToUpdate->setFontUnderline();
}
if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) {
$styleToUpdate->setFontStrikethrough();
}
}
/**
* @param Style $styleToUpdate Style to update (passed as reference)
* @param Style $style
* @param Style $baseStyle
* @return void
*/
private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
{
if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) {
$styleToUpdate->setFontSize($baseStyle->getFontSize());
}
if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) {
$styleToUpdate->setFontColor($baseStyle->getFontColor());
}
if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) {
$styleToUpdate->setFontName($baseStyle->getFontName());
}
}
/**
* @param Style $styleToUpdate Style to update (passed as reference)
* @param Style $style
* @param Style $baseStyle
* @return void
*/
private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
{
if (!$style->hasSetWrapText() && $baseStyle->hasSetWrapText()) {
$styleToUpdate->setShouldWrapText($baseStyle->shouldWrapText());
}
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
}
if (!$style->hasSetCellVerticalAlignment() && $baseStyle->shouldApplyCellVerticalAlignment()) {
$styleToUpdate->setCellVerticalAlignment($baseStyle->getCellVerticalAlignment());
}
if ($style->getBorder() === null && $baseStyle->shouldApplyBorder()) {
$styleToUpdate->setBorder($baseStyle->getBorder());
}
if ($style->getFormat() === null && $baseStyle->shouldApplyFormat()) {
$styleToUpdate->setFormat($baseStyle->getFormat());
}
if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) {
$styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor());
}
}
}