From 3a4b9bff73afe99338cc15cfcaa58b4615f2a0dc Mon Sep 17 00:00:00 2001 From: madflow Date: Sun, 12 Feb 2017 11:22:46 +0100 Subject: [PATCH] removed comment parameter, streamlined cell detection, more tests #182 --- src/Spout/Writer/Common/Cell.php | 74 ++++++++++++++------ src/Spout/Writer/ODS/Internal/Worksheet.php | 26 +++---- src/Spout/Writer/XLSX/Internal/Worksheet.php | 22 +++--- tests/Spout/Writer/ODS/WriterTest.php | 21 ++++++ tests/Spout/Writer/XLSX/WriterTest.php | 33 +++++++++ 5 files changed, 130 insertions(+), 46 deletions(-) diff --git a/src/Spout/Writer/Common/Cell.php b/src/Spout/Writer/Common/Cell.php index d443dbb..186838f 100644 --- a/src/Spout/Writer/Common/Cell.php +++ b/src/Spout/Writer/Common/Cell.php @@ -43,20 +43,19 @@ class Cell protected $value = null; /** - * Comment of this cell - * @var null | string + * The cell type + * @var null */ - protected $comment = null; + protected $type = null; /** * Cell constructor. * @param $value mixed * @param $comment string */ - public function __construct($value, $comment = null) + public function __construct($value) { $this->setValue($value); - $this->setComment($comment); } /** @@ -65,6 +64,7 @@ class Cell public function setValue($value) { $this->value = $value; + $this->type = $this->detectType($value); } /** @@ -76,34 +76,24 @@ class Cell } /** - * @param $comment string + * @return mixed|null */ - public function setComment($comment) + public function getType() { - $this->comment = $comment; - } - - /** - * @return null|string - */ - public function getComment() - { - return $this->comment; + return $this->type; } /** * Get the current value type * @return int */ - public function getType() + protected function detectType($value) { - $value = $this->getValue(); - - if(CellHelper::isBoolean($value)) { + if (CellHelper::isBoolean($value)) { return self::CELL_TYPE_BOOLEAN; } elseif (CellHelper::isEmpty($value)) { return self::CELL_TYPE_BLANK; - } elseif(CellHelper::isNumeric($this->getValue())) { + } elseif (CellHelper::isNumeric($this->getValue())) { return self::CELL_TYPE_NUMERIC; } elseif (CellHelper::isNonEmptyString($value)) { return self::CELL_TYPE_STRING; @@ -112,6 +102,46 @@ class Cell } } + /** + * @return bool + */ + public function isBoolean() + { + return $this->type === self::CELL_TYPE_BOOLEAN; + } + + /** + * @return bool + */ + public function isBlank() + { + return $this->type === self::CELL_TYPE_BLANK; + } + + /** + * @return bool + */ + public function isNumeric() + { + return $this->type === self::CELL_TYPE_NUMERIC; + } + + /** + * @return bool + */ + public function isString() + { + return $this->type === self::CELL_TYPE_STRING; + } + + /** + * @return bool + */ + public function isError() + { + return $this->type === self::CELL_TYPE_ERROR; + } + /** * @return string */ @@ -119,4 +149,4 @@ class Cell { return (string)$this->value; } -} \ No newline at end of file +} diff --git a/src/Spout/Writer/ODS/Internal/Worksheet.php b/src/Spout/Writer/ODS/Internal/Worksheet.php index fe20d1f..9e64326 100644 --- a/src/Spout/Writer/ODS/Internal/Worksheet.php +++ b/src/Spout/Writer/ODS/Internal/Worksheet.php @@ -192,33 +192,33 @@ class Worksheet implements WorksheetInterface $data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"'; } - if($cellValue instanceof Cell) { - $cellContent = $cellValue->getValue(); + if ($cellValue instanceof Cell) { + $cell = $cellValue; } else { - $cellContent = $cellValue; + $cell = new Cell($cellValue); } - if (CellHelper::isNonEmptyString($cellContent)) { + if ($cell->isString()) { $data .= ' office:value-type="string" calcext:value-type="string">'; - $cellValueLines = explode("\n", $cellContent); + $cellValueLines = explode("\n", $cell->getValue()); foreach ($cellValueLines as $cellValueLine) { $data .= '' . $this->stringsEscaper->escape($cellValueLine) . ''; } $data .= ''; - } else if (CellHelper::isBoolean($cellContent)) { - $data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellContent . '">'; - $data .= '' . $cellContent . ''; + } else if ($cell->isBoolean()) { + $data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cell->getValue() . '">'; + $data .= '' . $cell->getValue() . ''; $data .= ''; - } else if (CellHelper::isNumeric($cellContent)) { - $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellContent . '">'; - $data .= '' . $cellContent . ''; + } else if ($cell->isNumeric()) { + $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">'; + $data .= '' . $cell->getValue() . ''; $data .= ''; - } else if (empty($cellContent)) { + } else if ($cell->isBlank()) { $data .= '/>'; } else { - throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent)); + throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cell->getValue())); } return $data; diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 18406e3..7150ce9 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -214,19 +214,19 @@ EOD; $cellXML = 'getValue(); + if ($cellValue instanceof Cell) { + $cell = $cellValue; } else { - $cellContent = $cellValue; + $cell = new Cell($cellValue); } - if (CellHelper::isNonEmptyString($cellContent)) { - $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cellContent); - } else if (CellHelper::isBoolean($cellContent)) { - $cellXML .= ' t="b">' . intval($cellContent) . ''; - } else if (CellHelper::isNumeric($cellContent)) { - $cellXML .= '>' . $cellContent . ''; - } else if (empty($cellContent)) { + if ($cell->isString()) { + $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); + } else if ($cell->isBoolean()) { + $cellXML .= ' t="b">' . intval($cell->getValue()) . ''; + } else if ($cell->isNumeric()) { + $cellXML .= '>' . $cell->getValue() . ''; + } else if ($cell->isBlank()) { if ($this->styleHelper->shouldApplyStyleOnEmptyCell($styleId)) { $cellXML .= '/>'; } else { @@ -235,7 +235,7 @@ EOD; $cellXML = ''; } } else { - throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent)); + throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cell->getValue())); } return $cellXML; diff --git a/tests/Spout/Writer/ODS/WriterTest.php b/tests/Spout/Writer/ODS/WriterTest.php index fbce7b5..df60af9 100644 --- a/tests/Spout/Writer/ODS/WriterTest.php +++ b/tests/Spout/Writer/ODS/WriterTest.php @@ -485,6 +485,26 @@ class WriterTest extends \PHPUnit_Framework_TestCase } } + /** + * @return void + */ + public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes() + { + $fileName = 'test_writer_should_accept_cell_objects_with_types.ods'; + $dataRows = [ + [new Cell('i am a string'), new Cell(51465), new Cell(true), new Cell(51465.5)], + ]; + + $this->writeToODSFile($dataRows, $fileName); + + foreach ($dataRows as $dataRow) { + /** @var Cell $cell */ + foreach ($dataRow as $cell) { + $this->assertValueWasWritten($fileName, (string)$cell->getValue(), '', true); + } + } + } + /** * @param array $allRows * @param string $fileName @@ -549,6 +569,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase $xmlContents = file_get_contents('zip://' . $pathToContentFile); $this->assertContains($value, $xmlContents, $message); + } /** diff --git a/tests/Spout/Writer/XLSX/WriterTest.php b/tests/Spout/Writer/XLSX/WriterTest.php index f626fcc..fe89deb 100644 --- a/tests/Spout/Writer/XLSX/WriterTest.php +++ b/tests/Spout/Writer/XLSX/WriterTest.php @@ -529,6 +529,39 @@ class WriterTest extends \PHPUnit_Framework_TestCase } } + /** + * @return void + */ + public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes() + { + $fileName = 'test_writer_should_accept_cell_objects_with_types.xlsx'; + + $dataRowsShared = [ + [new Cell('i am a string')], + ]; + $dataRowsInline = [ + [new Cell(51465), new Cell(true), new Cell(51465.5)] + ]; + + $dataRows = array_merge($dataRowsShared, $dataRowsInline); + + $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false); + + foreach ($dataRowsShared as $dataRow) { + /** @var Cell $cell */ + foreach ($dataRow as $cell) { + $this->assertSharedStringWasWritten($fileName, (string)$cell->getValue()); + } + } + + foreach ($dataRowsInline as $dataRow) { + /** @var Cell $cell */ + foreach ($dataRow as $cell) { + $this->assertInlineDataWasWrittenToSheet($fileName, 1, $cell->getValue()); + } + } + } + /** * @param array $allRows * @param string $fileName