Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 9ce7740

Browse files
authored
Refactor Style::mergeWith() and StyleHelper::getStyleSectionContent() (#346)
1 parent ef4a32e commit 9ce7740

File tree

2 files changed

+164
-54
lines changed

2 files changed

+164
-54
lines changed

src/Spout/Writer/ODS/Helper/StyleHelper.php

Lines changed: 121 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -214,64 +214,143 @@ public function getContentXmlAutomaticStylesSectionContent($numWorksheets)
214214
*/
215215
protected function getStyleSectionContent($style)
216216
{
217-
$defaultStyle = $this->getDefaultStyle();
218217
$styleIndex = $style->getId() + 1; // 1-based
219218

220219
$content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">';
221220

221+
$content .= $this->getTextPropertiesSectionContent($style);
222+
$content .= $this->getTableCellPropertiesSectionContent($style);
223+
224+
$content .= '</style:style>';
225+
226+
return $content;
227+
}
228+
229+
/**
230+
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
231+
*
232+
* @param \Box\Spout\Writer\Style\Style $style
233+
* @return string
234+
*/
235+
private function getTextPropertiesSectionContent($style)
236+
{
237+
$content = '';
238+
222239
if ($style->shouldApplyFont()) {
223-
$content .= '<style:text-properties';
224-
225-
$fontColor = $style->getFontColor();
226-
if ($fontColor !== $defaultStyle->getFontColor()) {
227-
$content .= ' fo:color="#' . $fontColor . '"';
228-
}
229-
230-
$fontName = $style->getFontName();
231-
if ($fontName !== $defaultStyle->getFontName()) {
232-
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
233-
}
234-
235-
$fontSize = $style->getFontSize();
236-
if ($fontSize !== $defaultStyle->getFontSize()) {
237-
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
238-
}
239-
240-
if ($style->isFontBold()) {
241-
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
242-
}
243-
if ($style->isFontItalic()) {
244-
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
245-
}
246-
if ($style->isFontUnderline()) {
247-
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
248-
}
249-
if ($style->isFontStrikethrough()) {
250-
$content .= ' style:text-line-through-style="solid"';
251-
}
252-
253-
$content .= '/>';
240+
$content .= $this->getFontSectionContent($style);
241+
}
242+
243+
return $content;
244+
}
245+
246+
/**
247+
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
248+
*
249+
* @param \Box\Spout\Writer\Style\Style $style
250+
* @return string
251+
*/
252+
private function getFontSectionContent($style)
253+
{
254+
$defaultStyle = $this->getDefaultStyle();
255+
256+
$content = '<style:text-properties';
257+
258+
$fontColor = $style->getFontColor();
259+
if ($fontColor !== $defaultStyle->getFontColor()) {
260+
$content .= ' fo:color="#' . $fontColor . '"';
254261
}
255262

263+
$fontName = $style->getFontName();
264+
if ($fontName !== $defaultStyle->getFontName()) {
265+
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
266+
}
267+
268+
$fontSize = $style->getFontSize();
269+
if ($fontSize !== $defaultStyle->getFontSize()) {
270+
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
271+
}
272+
273+
if ($style->isFontBold()) {
274+
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
275+
}
276+
if ($style->isFontItalic()) {
277+
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
278+
}
279+
if ($style->isFontUnderline()) {
280+
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
281+
}
282+
if ($style->isFontStrikethrough()) {
283+
$content .= ' style:text-line-through-style="solid"';
284+
}
285+
286+
$content .= '/>';
287+
288+
return $content;
289+
}
290+
291+
/**
292+
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
293+
*
294+
* @param \Box\Spout\Writer\Style\Style $style
295+
* @return string
296+
*/
297+
private function getTableCellPropertiesSectionContent($style)
298+
{
299+
$content = '';
300+
256301
if ($style->shouldWrapText()) {
257-
$content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
302+
$content .= $this->getWrapTextXMLContent();
258303
}
259304

260305
if ($style->shouldApplyBorder()) {
261-
$borderProperty = '<style:table-cell-properties %s />';
262-
$borders = array_map(function (BorderPart $borderPart) {
263-
return BorderHelper::serializeBorderPart($borderPart);
264-
}, $style->getBorder()->getParts());
265-
$content .= sprintf($borderProperty, implode(' ', $borders));
306+
$content .= $this->getBorderXMLContent($style);
266307
}
267308

268309
if ($style->shouldApplyBackgroundColor()) {
269-
$content .= sprintf('
270-
<style:table-cell-properties fo:background-color="#%s"/>', $style->getBackgroundColor());
310+
$content .= $this->getBackgroundColorXMLContent($style);
271311
}
272312

273-
$content .= '</style:style>';
274-
275313
return $content;
276314
}
315+
316+
/**
317+
* Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section
318+
*
319+
* @return string
320+
*/
321+
private function getWrapTextXMLContent()
322+
{
323+
return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
324+
}
325+
326+
/**
327+
* Returns the contents of the borders definition for the "<style:table-cell-properties>" section
328+
*
329+
* @param \Box\Spout\Writer\Style\Style $style
330+
* @return string
331+
*/
332+
private function getBorderXMLContent($style)
333+
{
334+
$borderProperty = '<style:table-cell-properties %s />';
335+
336+
$borders = array_map(function (BorderPart $borderPart) {
337+
return BorderHelper::serializeBorderPart($borderPart);
338+
}, $style->getBorder()->getParts());
339+
340+
return sprintf($borderProperty, implode(' ', $borders));
341+
}
342+
343+
/**
344+
* Returns the contents of the background color definition for the "<style:table-cell-properties>" section
345+
*
346+
* @param \Box\Spout\Writer\Style\Style $style
347+
* @return string
348+
*/
349+
private function getBackgroundColorXMLContent($style)
350+
{
351+
return sprintf(
352+
'<style:table-cell-properties fo:background-color="#%s"/>',
353+
$style->getBackgroundColor()
354+
);
355+
}
277356
}

src/Spout/Writer/Style/Style.php

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function getBorder()
106106

107107
/**
108108
* @param Border $border
109+
* @return Style
109110
*/
110111
public function setBorder(Border $border)
111112
{
@@ -359,37 +360,67 @@ public function mergeWith($baseStyle)
359360
{
360361
$mergedStyle = clone $this;
361362

363+
$this->mergeFontStyles($mergedStyle, $baseStyle);
364+
$this->mergeOtherFontProperties($mergedStyle, $baseStyle);
365+
$this->mergeCellProperties($mergedStyle, $baseStyle);
366+
367+
return $mergedStyle;
368+
}
369+
370+
/**
371+
* @param Style $styleToUpdate (passed as reference)
372+
* @param Style $baseStyle
373+
* @return void
374+
*/
375+
private function mergeFontStyles($styleToUpdate, $baseStyle)
376+
{
362377
if (!$this->hasSetFontBold && $baseStyle->isFontBold()) {
363-
$mergedStyle->setFontBold();
378+
$styleToUpdate->setFontBold();
364379
}
365380
if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) {
366-
$mergedStyle->setFontItalic();
381+
$styleToUpdate->setFontItalic();
367382
}
368383
if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) {
369-
$mergedStyle->setFontUnderline();
384+
$styleToUpdate->setFontUnderline();
370385
}
371386
if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) {
372-
$mergedStyle->setFontStrikethrough();
387+
$styleToUpdate->setFontStrikethrough();
373388
}
389+
}
390+
391+
/**
392+
* @param Style $styleToUpdate Style to update (passed as reference)
393+
* @param Style $baseStyle
394+
* @return void
395+
*/
396+
private function mergeOtherFontProperties($styleToUpdate, $baseStyle)
397+
{
374398
if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
375-
$mergedStyle->setFontSize($baseStyle->getFontSize());
399+
$styleToUpdate->setFontSize($baseStyle->getFontSize());
376400
}
377401
if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
378-
$mergedStyle->setFontColor($baseStyle->getFontColor());
402+
$styleToUpdate->setFontColor($baseStyle->getFontColor());
379403
}
380404
if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
381-
$mergedStyle->setFontName($baseStyle->getFontName());
405+
$styleToUpdate->setFontName($baseStyle->getFontName());
382406
}
407+
}
408+
409+
/**
410+
* @param Style $styleToUpdate Style to update (passed as reference)
411+
* @param Style $baseStyle
412+
* @return void
413+
*/
414+
private function mergeCellProperties($styleToUpdate, $baseStyle)
415+
{
383416
if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) {
384-
$mergedStyle->setShouldWrapText();
417+
$styleToUpdate->setShouldWrapText();
385418
}
386419
if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) {
387-
$mergedStyle->setBorder($baseStyle->getBorder());
420+
$styleToUpdate->setBorder($baseStyle->getBorder());
388421
}
389422
if (!$this->hasSetBackgroundColor && $baseStyle->shouldApplyBackgroundColor()) {
390-
$mergedStyle->setBackgroundColor($baseStyle->getBackgroundColor());
423+
$styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor());
391424
}
392-
393-
return $mergedStyle;
394425
}
395426
}

0 commit comments

Comments
 (0)