Merge branch 'master' of github.com:box/spout into add-phpstan
Conflicts: .travis.yml CONTRIBUTING.md composer.json src/Spout/Common/Entity/Cell.php tests/Spout/Writer/ODS/WriterTest.php
This commit is contained in:
commit
540667870a
49 files changed
+529
-152
No files matched your search
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class RegisteredStyle
|
||||
* Allow to know if this style must replace actual row style.
|
||||
*/
|
||||
class RegisteredStyle
|
||||
{
|
||||
/**
|
||||
* @var Style
|
||||
*/
|
||||
private $style;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isMatchingRowStyle;
|
||||
|
||||
public function __construct(Style $style, bool $isMatchingRowStyle)
|
||||
{
|
||||
$this->style = $style;
|
||||
$this->isMatchingRowStyle = $isMatchingRowStyle;
|
||||
}
|
||||
|
||||
public function getStyle() : Style
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function isMatchingRowStyle() : bool
|
||||
{
|
||||
return $this->isMatchingRowStyle;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager\Style;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class PossiblyUpdatedStyle
|
||||
* Indicates if style is updated.
|
||||
* It allow to know if style registration must be done.
|
||||
*/
|
||||
class PossiblyUpdatedStyle
|
||||
{
|
||||
private $style;
|
||||
private $isUpdated;
|
||||
|
||||
public function __construct(Style $style, bool $isUpdated)
|
||||
{
|
||||
$this->style = $style;
|
||||
$this->isUpdated = $isUpdated;
|
||||
}
|
||||
|
||||
public function getStyle() : Style
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function isUpdated() : bool
|
||||
{
|
||||
return $this->isUpdated;
|
||||
}
|
||||
}
|
||||
@@ -50,13 +50,11 @@ class StyleManager implements StyleManagerInterface
|
||||
* Typically, set "wrap text" if a cell contains a new line.
|
||||
*
|
||||
* @param Cell $cell
|
||||
* @return Style
|
||||
* @return PossiblyUpdatedStyle The eventually updated style
|
||||
*/
|
||||
public function applyExtraStylesIfNeeded(Cell $cell)
|
||||
public function applyExtraStylesIfNeeded(Cell $cell) : PossiblyUpdatedStyle
|
||||
{
|
||||
$updatedStyle = $this->applyWrapTextIfCellContainsNewLine($cell);
|
||||
|
||||
return $updatedStyle;
|
||||
return $this->applyWrapTextIfCellContainsNewLine($cell);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,21 +67,19 @@ class StyleManager implements StyleManagerInterface
|
||||
* on the Windows version of Excel...
|
||||
*
|
||||
* @param Cell $cell The cell the style should be applied to
|
||||
* @return \Box\Spout\Common\Entity\Style\Style The eventually updated style
|
||||
* @return PossiblyUpdatedStyle The eventually updated style
|
||||
*/
|
||||
protected function applyWrapTextIfCellContainsNewLine(Cell $cell)
|
||||
protected function applyWrapTextIfCellContainsNewLine(Cell $cell) : PossiblyUpdatedStyle
|
||||
{
|
||||
$cellStyle = $cell->getStyle();
|
||||
|
||||
// if the "wrap text" option is already set, no-op
|
||||
if ($cellStyle->hasSetWrapText()) {
|
||||
return $cellStyle;
|
||||
}
|
||||
|
||||
if ($cell->isString() && \strpos($cell->getValue(), "\n") !== false) {
|
||||
if (!$cellStyle->hasSetWrapText() && $cell->isString() && \strpos($cell->getValue(), "\n") !== false) {
|
||||
$cellStyle->setShouldWrapText();
|
||||
|
||||
return new PossiblyUpdatedStyle($cellStyle, true);
|
||||
}
|
||||
|
||||
return $cellStyle;
|
||||
return new PossiblyUpdatedStyle($cellStyle, false);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ interface StyleManagerInterface
|
||||
* Typically, set "wrap text" if a cell contains a new line.
|
||||
*
|
||||
* @param Cell $cell
|
||||
* @return Style The updated style
|
||||
* @return PossiblyUpdatedStyle The eventually updated style
|
||||
*/
|
||||
public function applyExtraStylesIfNeeded(Cell $cell);
|
||||
public function applyExtraStylesIfNeeded(Cell $cell) : PossiblyUpdatedStyle;
|
||||
}
|
||||
@@ -36,9 +36,9 @@ class StyleRegistry
|
||||
{
|
||||
$serializedStyle = $this->serialize($style);
|
||||
|
||||
if (!$this->hasStyleAlreadyBeenRegistered($style)) {
|
||||
if (!$this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle)) {
|
||||
$nextStyleId = \count($this->serializedStyleToStyleIdMappingTable);
|
||||
$style->setId($nextStyleId);
|
||||
$style->markAsRegistered($nextStyleId);
|
||||
|
||||
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
|
||||
$this->styleIdToStyleMappingTable[$nextStyleId] = $style;
|
||||
@@ -48,15 +48,13 @@ class StyleRegistry
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given style has already been registered.
|
||||
* Returns whether the serialized style has already been registered.
|
||||
*
|
||||
* @param Style $style
|
||||
* @param string $serializedStyle The serialized style
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasStyleAlreadyBeenRegistered(Style $style)
|
||||
protected function hasSerializedStyleAlreadyBeenRegistered(string $serializedStyle)
|
||||
{
|
||||
$serializedStyle = $this->serialize($style);
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]);
|
||||
}
|
||||
@@ -101,13 +99,13 @@ class StyleRegistry
|
||||
*/
|
||||
public function serialize(Style $style)
|
||||
{
|
||||
// In order to be able to properly compare style, set static ID value
|
||||
// In order to be able to properly compare style, set static ID value and reset registration
|
||||
$currentId = $style->getId();
|
||||
$style->setId(0);
|
||||
$style->unmarkAsRegistered();
|
||||
|
||||
$serializedStyle = \serialize($style);
|
||||
|
||||
$style->setId($currentId);
|
||||
$style->markAsRegistered($currentId);
|
||||
|
||||
return $serializedStyle;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user