first stab at cell objects, #182
This commit is contained in:
parent
4e6db6a8a1
commit
c2fb23e30f
122
src/Spout/Writer/Common/Cell.php
Normal file
122
src/Spout/Writer/Common/Cell.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common;
|
||||
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
|
||||
class Cell
|
||||
{
|
||||
/**
|
||||
* Numeric cell type (whole numbers, fractional numbers, dates)
|
||||
*/
|
||||
const CELL_TYPE_NUMERIC = 0;
|
||||
|
||||
/**
|
||||
* String (text) cell type
|
||||
*/
|
||||
const CELL_TYPE_STRING = 1;
|
||||
|
||||
/**
|
||||
* Formula cell type
|
||||
*/
|
||||
const CELL_TYPE_FORMULA = 2;
|
||||
|
||||
/**
|
||||
* Blank cell type
|
||||
*/
|
||||
const CELL_TYPE_BLANK = 3;
|
||||
|
||||
/**
|
||||
* Boolean cell type
|
||||
*/
|
||||
const CELL_TYPE_BOOLEAN = 4;
|
||||
|
||||
/**
|
||||
* Error cell type
|
||||
*/
|
||||
const CELL_TYPE_ERROR = 5;
|
||||
|
||||
/**
|
||||
* The value of this cell
|
||||
* @var null | mixed
|
||||
*/
|
||||
protected $value = null;
|
||||
|
||||
/**
|
||||
* Comment of this cell
|
||||
* @var null | string
|
||||
*/
|
||||
protected $comment = null;
|
||||
|
||||
/**
|
||||
* Cell constructor.
|
||||
* @param $value mixed
|
||||
* @param $comment string
|
||||
*/
|
||||
public function __construct($value, $comment = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
$this->setComment($comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value mixed
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $comment string
|
||||
*/
|
||||
public function setComment($comment)
|
||||
{
|
||||
$this->comment = $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getComment()
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current value type
|
||||
* @return int
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
$value = $this->getValue();
|
||||
|
||||
if(CellHelper::isBoolean($value)) {
|
||||
return self::CELL_TYPE_BOOLEAN;
|
||||
} elseif (CellHelper::isEmpty($value)) {
|
||||
return self::CELL_TYPE_BLANK;
|
||||
} elseif(CellHelper::isNumeric($this->getValue())) {
|
||||
return self::CELL_TYPE_NUMERIC;
|
||||
} elseif (CellHelper::isNonEmptyString($value)) {
|
||||
return self::CELL_TYPE_STRING;
|
||||
} else {
|
||||
return self::CELL_TYPE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (string)$this->value;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS\Internal;
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Cell;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
|
||||
|
||||
@ -191,27 +192,33 @@ class Worksheet implements WorksheetInterface
|
||||
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
|
||||
}
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
if($cellValue instanceof Cell) {
|
||||
$cellContent = $cellValue->getValue();
|
||||
} else {
|
||||
$cellContent = $cellValue;
|
||||
}
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellContent)) {
|
||||
$data .= ' office:value-type="string" calcext:value-type="string">';
|
||||
|
||||
$cellValueLines = explode("\n", $cellValue);
|
||||
$cellValueLines = explode("\n", $cellContent);
|
||||
foreach ($cellValueLines as $cellValueLine) {
|
||||
$data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>';
|
||||
}
|
||||
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (CellHelper::isBoolean($cellValue)) {
|
||||
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellValue . '">';
|
||||
$data .= '<text:p>' . $cellValue . '</text:p>';
|
||||
} else if (CellHelper::isBoolean($cellContent)) {
|
||||
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellContent . '">';
|
||||
$data .= '<text:p>' . $cellContent . '</text:p>';
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (CellHelper::isNumeric($cellValue)) {
|
||||
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellValue . '">';
|
||||
$data .= '<text:p>' . $cellValue . '</text:p>';
|
||||
} else if (CellHelper::isNumeric($cellContent)) {
|
||||
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellContent . '">';
|
||||
$data .= '<text:p>' . $cellContent . '</text:p>';
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (empty($cellValue)) {
|
||||
} else if (empty($cellContent)) {
|
||||
$data .= '/>';
|
||||
} else {
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent));
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX\Internal;
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Cell;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
|
||||
|
||||
@ -213,13 +214,19 @@ EOD;
|
||||
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
|
||||
$cellXML .= ' s="' . $styleId . '"';
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cellValue);
|
||||
} else if (CellHelper::isBoolean($cellValue)) {
|
||||
$cellXML .= ' t="b"><v>' . intval($cellValue) . '</v></c>';
|
||||
} else if (CellHelper::isNumeric($cellValue)) {
|
||||
$cellXML .= '><v>' . $cellValue . '</v></c>';
|
||||
} else if (empty($cellValue)) {
|
||||
if($cellValue instanceof Cell) {
|
||||
$cellContent = $cellValue->getValue();
|
||||
} else {
|
||||
$cellContent = $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 ($this->styleHelper->shouldApplyStyleOnEmptyCell($styleId)) {
|
||||
$cellXML .= '/>';
|
||||
} else {
|
||||
@ -228,7 +235,7 @@ EOD;
|
||||
$cellXML = '';
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellContent));
|
||||
}
|
||||
|
||||
return $cellXML;
|
||||
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Common\Helper\EncodingHelper;
|
||||
use Box\Spout\Writer\Common\Cell;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
/**
|
||||
@ -177,6 +178,19 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('#This is, a comma#,csv--12,csv--13', $writtenContent, 'The fields should be enclosed with #');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteShouldAcceptCellObjects()
|
||||
{
|
||||
$allRows = [
|
||||
[new Cell('String Value'), new Cell(1)]
|
||||
];
|
||||
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv');
|
||||
$writtenContent = $this->trimWrittenContent($writtenContent);
|
||||
$this->assertEquals('"String Value",1', $writtenContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
|
@ -6,6 +6,7 @@ use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Cell;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
@ -463,6 +464,27 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteShouldAcceptCellObjects()
|
||||
{
|
||||
$fileName = 'test_writer_should_accept_cell_objects.ods';
|
||||
$dataRows = [
|
||||
[new Cell('ods--11'), new Cell('ods--12')],
|
||||
[new Cell('ods--21'), new Cell('ods--22'), new Cell('ods--23')],
|
||||
];
|
||||
|
||||
$this->writeToODSFile($dataRows, $fileName);
|
||||
|
||||
foreach ($dataRows as $dataRow) {
|
||||
/** @var Cell $cell */
|
||||
foreach ($dataRow as $cell) {
|
||||
$this->assertValueWasWritten($fileName, $cell->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
|
||||
use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Cell;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Writer\XLSX\Internal\Worksheet;
|
||||
|
||||
@ -507,6 +508,27 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $finfo->file($resourcePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriterShouldAcceptCellObjects()
|
||||
{
|
||||
$fileName = 'test_writer_should_accept_cell_objects.xlsx';
|
||||
$dataRows = [
|
||||
[new Cell('xlsx--11'), new Cell('xlsx--12')],
|
||||
[new Cell('xlsx--21'), new Cell('xlsx--22'), new Cell('xlsx--23')],
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
foreach ($dataRows as $dataRow) {
|
||||
/** @var Cell $cell */
|
||||
foreach ($dataRow as $cell) {
|
||||
$this->assertSharedStringWasWritten($fileName, $cell->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
|
Loading…
x
Reference in New Issue
Block a user