removed comment parameter, streamlined cell detection, more tests #182
This commit is contained in:
parent
c2fb23e30f
commit
3a4b9bff73
@ -43,20 +43,19 @@ class Cell
|
|||||||
protected $value = null;
|
protected $value = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comment of this cell
|
* The cell type
|
||||||
* @var null | string
|
* @var null
|
||||||
*/
|
*/
|
||||||
protected $comment = null;
|
protected $type = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cell constructor.
|
* Cell constructor.
|
||||||
* @param $value mixed
|
* @param $value mixed
|
||||||
* @param $comment string
|
* @param $comment string
|
||||||
*/
|
*/
|
||||||
public function __construct($value, $comment = null)
|
public function __construct($value)
|
||||||
{
|
{
|
||||||
$this->setValue($value);
|
$this->setValue($value);
|
||||||
$this->setComment($comment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,6 +64,7 @@ class Cell
|
|||||||
public function setValue($value)
|
public function setValue($value)
|
||||||
{
|
{
|
||||||
$this->value = $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 $this->type;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return null|string
|
|
||||||
*/
|
|
||||||
public function getComment()
|
|
||||||
{
|
|
||||||
return $this->comment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current value type
|
* Get the current value type
|
||||||
* @return int
|
* @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;
|
return self::CELL_TYPE_BOOLEAN;
|
||||||
} elseif (CellHelper::isEmpty($value)) {
|
} elseif (CellHelper::isEmpty($value)) {
|
||||||
return self::CELL_TYPE_BLANK;
|
return self::CELL_TYPE_BLANK;
|
||||||
} elseif(CellHelper::isNumeric($this->getValue())) {
|
} elseif (CellHelper::isNumeric($this->getValue())) {
|
||||||
return self::CELL_TYPE_NUMERIC;
|
return self::CELL_TYPE_NUMERIC;
|
||||||
} elseif (CellHelper::isNonEmptyString($value)) {
|
} elseif (CellHelper::isNonEmptyString($value)) {
|
||||||
return self::CELL_TYPE_STRING;
|
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
|
* @return string
|
||||||
*/
|
*/
|
||||||
@ -119,4 +149,4 @@ class Cell
|
|||||||
{
|
{
|
||||||
return (string)$this->value;
|
return (string)$this->value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,33 +192,33 @@ class Worksheet implements WorksheetInterface
|
|||||||
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
|
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
if($cellValue instanceof Cell) {
|
if ($cellValue instanceof Cell) {
|
||||||
$cellContent = $cellValue->getValue();
|
$cell = $cellValue;
|
||||||
} else {
|
} else {
|
||||||
$cellContent = $cellValue;
|
$cell = new Cell($cellValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CellHelper::isNonEmptyString($cellContent)) {
|
if ($cell->isString()) {
|
||||||
$data .= ' office:value-type="string" calcext:value-type="string">';
|
$data .= ' office:value-type="string" calcext:value-type="string">';
|
||||||
|
|
||||||
$cellValueLines = explode("\n", $cellContent);
|
$cellValueLines = explode("\n", $cell->getValue());
|
||||||
foreach ($cellValueLines as $cellValueLine) {
|
foreach ($cellValueLines as $cellValueLine) {
|
||||||
$data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>';
|
$data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>';
|
||||||
}
|
}
|
||||||
|
|
||||||
$data .= '</table:table-cell>';
|
$data .= '</table:table-cell>';
|
||||||
} else if (CellHelper::isBoolean($cellContent)) {
|
} else if ($cell->isBoolean()) {
|
||||||
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellContent . '">';
|
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cell->getValue() . '">';
|
||||||
$data .= '<text:p>' . $cellContent . '</text:p>';
|
$data .= '<text:p>' . $cell->getValue() . '</text:p>';
|
||||||
$data .= '</table:table-cell>';
|
$data .= '</table:table-cell>';
|
||||||
} else if (CellHelper::isNumeric($cellContent)) {
|
} else if ($cell->isNumeric()) {
|
||||||
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellContent . '">';
|
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">';
|
||||||
$data .= '<text:p>' . $cellContent . '</text:p>';
|
$data .= '<text:p>' . $cell->getValue() . '</text:p>';
|
||||||
$data .= '</table:table-cell>';
|
$data .= '</table:table-cell>';
|
||||||
} else if (empty($cellContent)) {
|
} else if ($cell->isBlank()) {
|
||||||
$data .= '/>';
|
$data .= '/>';
|
||||||
} else {
|
} 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;
|
return $data;
|
||||||
|
@ -214,19 +214,19 @@ EOD;
|
|||||||
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
|
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
|
||||||
$cellXML .= ' s="' . $styleId . '"';
|
$cellXML .= ' s="' . $styleId . '"';
|
||||||
|
|
||||||
if($cellValue instanceof Cell) {
|
if ($cellValue instanceof Cell) {
|
||||||
$cellContent = $cellValue->getValue();
|
$cell = $cellValue;
|
||||||
} else {
|
} else {
|
||||||
$cellContent = $cellValue;
|
$cell = new Cell($cellValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CellHelper::isNonEmptyString($cellContent)) {
|
if ($cell->isString()) {
|
||||||
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cellContent);
|
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
|
||||||
} else if (CellHelper::isBoolean($cellContent)) {
|
} else if ($cell->isBoolean()) {
|
||||||
$cellXML .= ' t="b"><v>' . intval($cellContent) . '</v></c>';
|
$cellXML .= ' t="b"><v>' . intval($cell->getValue()) . '</v></c>';
|
||||||
} else if (CellHelper::isNumeric($cellContent)) {
|
} else if ($cell->isNumeric()) {
|
||||||
$cellXML .= '><v>' . $cellContent . '</v></c>';
|
$cellXML .= '><v>' . $cell->getValue() . '</v></c>';
|
||||||
} else if (empty($cellContent)) {
|
} else if ($cell->isBlank()) {
|
||||||
if ($this->styleHelper->shouldApplyStyleOnEmptyCell($styleId)) {
|
if ($this->styleHelper->shouldApplyStyleOnEmptyCell($styleId)) {
|
||||||
$cellXML .= '/>';
|
$cellXML .= '/>';
|
||||||
} else {
|
} else {
|
||||||
@ -235,7 +235,7 @@ EOD;
|
|||||||
$cellXML = '';
|
$cellXML = '';
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
return $cellXML;
|
||||||
|
@ -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 array $allRows
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
@ -549,6 +569,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$xmlContents = file_get_contents('zip://' . $pathToContentFile);
|
$xmlContents = file_get_contents('zip://' . $pathToContentFile);
|
||||||
|
|
||||||
$this->assertContains($value, $xmlContents, $message);
|
$this->assertContains($value, $xmlContents, $message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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 array $allRows
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
|
Loading…
x
Reference in New Issue
Block a user