Merge 887d6ef033 into 8459666841
This commit is contained in:
11 files changed
+229
-24
No files matched your search
@@ -3,7 +3,7 @@
|
||||
namespace Box\Spout\Common\Entity\Style;
|
||||
|
||||
/**
|
||||
* Class Alignment
|
||||
* Class CellAlignment
|
||||
* This class provides constants to work with text alignment.
|
||||
*/
|
||||
abstract class CellAlignment
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Common\Entity\Style;
|
||||
|
||||
/**
|
||||
* Class CellVerticalAlignment
|
||||
* This class provides constants to work with text vertical alignment.
|
||||
*/
|
||||
abstract class CellVerticalAlignment
|
||||
{
|
||||
public const AUTO = 'auto';
|
||||
public const BASELINE = 'baseline';
|
||||
public const BOTTOM = 'bottom';
|
||||
public const CENTER = 'center';
|
||||
public const DISTRIBUTED = 'distributed';
|
||||
public const JUSTIFY = 'justify';
|
||||
public const TOP = 'top';
|
||||
|
||||
private static $VALID_ALIGNMENTS = [
|
||||
self::AUTO => 1,
|
||||
self::BASELINE => 1,
|
||||
self::BOTTOM => 1,
|
||||
self::CENTER => 1,
|
||||
self::DISTRIBUTED => 1,
|
||||
self::JUSTIFY => 1,
|
||||
self::TOP => 1,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $cellVerticalAlignment
|
||||
*
|
||||
* @return bool Whether the given cell vertical alignment is valid
|
||||
*/
|
||||
public static function isValid($cellVerticalAlignment)
|
||||
{
|
||||
return isset(self::$VALID_ALIGNMENTS[$cellVerticalAlignment]);
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,13 @@ class Style
|
||||
/** @var bool Whether the cell alignment property was set */
|
||||
private $hasSetCellAlignment = false;
|
||||
|
||||
/** @var bool Whether specific cell vertical alignment should be applied */
|
||||
private $shouldApplyCellVerticalAlignment = false;
|
||||
/** @var string Cell vertical alignment */
|
||||
private $cellVerticalAlignment;
|
||||
/** @var bool Whether the cell vertical alignment property was set */
|
||||
private $hasSetCellVerticalAlignment = false;
|
||||
|
||||
/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
|
||||
private $shouldWrapText = false;
|
||||
/** @var bool Whether the wrap text property was set */
|
||||
@@ -354,6 +361,14 @@ class Style
|
||||
return $this->cellAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCellVerticalAlignment()
|
||||
{
|
||||
return $this->cellVerticalAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cellAlignment The cell alignment
|
||||
*
|
||||
@@ -369,6 +384,21 @@ class Style
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cellVerticalAlignment The cell vertical alignment
|
||||
*
|
||||
* @return Style
|
||||
*/
|
||||
public function setCellVerticalAlignment($cellVerticalAlignment)
|
||||
{
|
||||
$this->cellVerticalAlignment = $cellVerticalAlignment;
|
||||
$this->hasSetCellVerticalAlignment = true;
|
||||
$this->shouldApplyCellVerticalAlignment = true;
|
||||
$this->isEmpty = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@@ -377,6 +407,14 @@ class Style
|
||||
return $this->hasSetCellAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetCellVerticalAlignment()
|
||||
{
|
||||
return $this->hasSetCellVerticalAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Whether specific cell alignment should be applied
|
||||
*/
|
||||
@@ -385,6 +423,14 @@ class Style
|
||||
return $this->shouldApplyCellAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Whether specific cell alignment should be applied
|
||||
*/
|
||||
public function shouldApplyCellVerticalAlignment()
|
||||
{
|
||||
return $this->shouldApplyCellVerticalAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\Common\Creator\Style;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\Border;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Box\Spout\Common\Entity\Style\CellVerticalAlignment;
|
||||
use Box\Spout\Common\Entity\Style\Style;
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
|
||||
@@ -143,6 +144,25 @@ class StyleBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cell vertical alignment.
|
||||
*
|
||||
* @param string $cellVerticalAlignment The cell vertical alignment
|
||||
*
|
||||
* @throws InvalidArgumentException If the given cell vertical alignment is not valid
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setCellVerticalAlignment($cellVerticalAlignment)
|
||||
{
|
||||
if (!CellVerticalAlignment::isValid($cellVerticalAlignment)) {
|
||||
throw new InvalidArgumentException('Invalid cell vertical alignment value');
|
||||
}
|
||||
|
||||
$this->style->setCellVerticalAlignment($cellVerticalAlignment);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a border
|
||||
*
|
||||
|
||||
@@ -82,12 +82,15 @@ class StyleMerger
|
||||
*/
|
||||
private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
|
||||
{
|
||||
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
|
||||
$styleToUpdate->setShouldWrapText();
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS\Manager\Style;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\BorderPart;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Box\Spout\Common\Entity\Style\CellVerticalAlignment;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\ODS\Helper\BorderHelper;
|
||||
|
||||
@@ -277,12 +278,13 @@ EOD;
|
||||
*/
|
||||
private function getParagraphPropertiesSectionContent($style)
|
||||
{
|
||||
if (!$style->shouldApplyCellAlignment()) {
|
||||
if (!$style->shouldApplyCellAlignment() && !$style->shouldApplyCellVerticalAlignment()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '<style:paragraph-properties '
|
||||
. $this->getCellAlignmentSectionContent($style)
|
||||
. $this->getCellVerticalAlignmentSectionContent($style)
|
||||
. '/>';
|
||||
}
|
||||
|
||||
@@ -301,6 +303,21 @@ EOD;
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the cell vertical alignment definition for the "<style:paragraph-properties>" section
|
||||
*
|
||||
* @param \Box\Spout\Common\Entity\Style\Style $style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getCellVerticalAlignmentSectionContent($style)
|
||||
{
|
||||
return \sprintf(
|
||||
' fo:vertical-align="%s" ',
|
||||
$this->transformCellVerticalAlignment($style->getCellVerticalAlignment())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Even though "left" and "right" alignments are part of the spec, and interpreted
|
||||
* respectively as "start" and "end", using the recommended values increase compatibility
|
||||
@@ -319,6 +336,21 @@ EOD;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spec uses 'middle' rather than 'center'
|
||||
* http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1420236_253892949
|
||||
*
|
||||
* @param string $cellAlignment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function transformCellVerticalAlignment($cellVerticalAlignment)
|
||||
{
|
||||
return ($cellVerticalAlignment === CellVerticalAlignment::CENTER)
|
||||
? 'middle'
|
||||
: $cellVerticalAlignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
|
||||
*
|
||||
@@ -329,8 +361,8 @@ EOD;
|
||||
{
|
||||
$content = '<style:table-cell-properties ';
|
||||
|
||||
if ($style->shouldWrapText()) {
|
||||
$content .= $this->getWrapTextXMLContent();
|
||||
if ($style->hasSetWrapText()) {
|
||||
$content .= $this->getWrapTextXMLContent($style->shouldWrapText());
|
||||
}
|
||||
|
||||
if ($style->shouldApplyBorder()) {
|
||||
@@ -349,11 +381,12 @@ EOD;
|
||||
/**
|
||||
* Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section
|
||||
*
|
||||
* @param boolean $shouldWrapText
|
||||
* @return string
|
||||
*/
|
||||
private function getWrapTextXMLContent()
|
||||
private function getWrapTextXMLContent($shouldWrapText)
|
||||
{
|
||||
return ' fo:wrap-option="wrap" style:vertical-align="automatic" ';
|
||||
return ' fo:wrap-option="' . ($shouldWrapText ? '' : 'no-') . 'wrap" style:vertical-align="automatic" ';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -250,14 +250,17 @@ EOD;
|
||||
|
||||
$content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
|
||||
|
||||
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
|
||||
if ($style->shouldApplyCellAlignment() || $style->shouldApplyCellVerticalAlignment() || $style->hasSetWrapText()) {
|
||||
$content .= ' applyAlignment="1">';
|
||||
$content .= '<alignment';
|
||||
if ($style->shouldApplyCellAlignment()) {
|
||||
$content .= \sprintf(' horizontal="%s"', $style->getCellAlignment());
|
||||
}
|
||||
if ($style->shouldWrapText()) {
|
||||
$content .= ' wrapText="1"';
|
||||
if ($style->shouldApplyCellVerticalAlignment()) {
|
||||
$content .= \sprintf(' vertical="%s"', $style->getCellVerticalAlignment());
|
||||
}
|
||||
if ($style->hasSetWrapText()) {
|
||||
$content .= ' wrapText="' . ($style->shouldWrapText() ? '1' : '0') . '"';
|
||||
}
|
||||
$content .= '/>';
|
||||
$content .= '</xf>';
|
||||
|
||||
Reference in new issue
Block a user