removed comment parameter, streamlined cell detection, more tests #182

This commit is contained in:
madflow 2017-02-12 11:22:46 +01:00
parent c2fb23e30f
commit 3a4b9bff73
5 changed files with 130 additions and 46 deletions

View File

@ -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
*/

View File

@ -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 .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>';
}
$data .= '</table:table-cell>';
} else if (CellHelper::isBoolean($cellContent)) {
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellContent . '">';
$data .= '<text:p>' . $cellContent . '</text:p>';
} else if ($cell->isBoolean()) {
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cell->getValue() . '">';
$data .= '<text:p>' . $cell->getValue() . '</text:p>';
$data .= '</table:table-cell>';
} else if (CellHelper::isNumeric($cellContent)) {
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellContent . '">';
$data .= '<text:p>' . $cellContent . '</text:p>';
} else if ($cell->isNumeric()) {
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">';
$data .= '<text:p>' . $cell->getValue() . '</text:p>';
$data .= '</table:table-cell>';
} 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;

View File

@ -214,19 +214,19 @@ EOD;
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
$cellXML .= ' s="' . $styleId . '"';
if($cellValue instanceof Cell) {
$cellContent = $cellValue->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"><v>' . intval($cellContent) . '</v></c>';
} else if (CellHelper::isNumeric($cellContent)) {
$cellXML .= '><v>' . $cellContent . '</v></c>';
} else if (empty($cellContent)) {
if ($cell->isString()) {
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
} else if ($cell->isBoolean()) {
$cellXML .= ' t="b"><v>' . intval($cell->getValue()) . '</v></c>';
} else if ($cell->isNumeric()) {
$cellXML .= '><v>' . $cell->getValue() . '</v></c>';
} 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;

View File

@ -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);
}
/**

View File

@ -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