ODS Writer should accept associative arrays
This commit is contained in:
parent
2c80b1f23a
commit
5ccb39a73c
@ -134,6 +134,10 @@ class Worksheet implements WorksheetInterface
|
|||||||
*/
|
*/
|
||||||
public function addRow($dataRow, $style)
|
public function addRow($dataRow, $style)
|
||||||
{
|
{
|
||||||
|
// $dataRow can be an associative array. We need to transform
|
||||||
|
// it into a regular array, as we'll use the numeric indexes.
|
||||||
|
$dataRowWithNumericIndexes = array_values($dataRow);
|
||||||
|
|
||||||
$styleIndex = ($style->getId() + 1); // 1-based
|
$styleIndex = ($style->getId() + 1); // 1-based
|
||||||
$cellsCount = count($dataRow);
|
$cellsCount = count($dataRow);
|
||||||
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
|
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
|
||||||
@ -144,10 +148,12 @@ class Worksheet implements WorksheetInterface
|
|||||||
$nextCellIndex = 1;
|
$nextCellIndex = 1;
|
||||||
|
|
||||||
for ($i = 0; $i < $cellsCount; $i++) {
|
for ($i = 0; $i < $cellsCount; $i++) {
|
||||||
$currentCellValue = $dataRow[$currentCellIndex];
|
$currentCellValue = $dataRowWithNumericIndexes[$currentCellIndex];
|
||||||
|
|
||||||
// Using isset here because it is way faster than array_key_exists...
|
// Using isset here because it is way faster than array_key_exists...
|
||||||
if (!isset($dataRow[$nextCellIndex]) || $currentCellValue !== $dataRow[$nextCellIndex]) {
|
if (!isset($dataRowWithNumericIndexes[$nextCellIndex]) ||
|
||||||
|
$currentCellValue !== $dataRowWithNumericIndexes[$nextCellIndex]) {
|
||||||
|
|
||||||
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
||||||
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
||||||
|
|
||||||
|
@ -74,6 +74,20 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should contain a UTF-8 BOM');
|
$this->assertContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should contain a UTF-8 BOM');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testWriteShouldSupportAssociativeArrays()
|
||||||
|
{
|
||||||
|
$allRows = [
|
||||||
|
['foo' => 'csv--11', 'bar' => 'csv--12'],
|
||||||
|
];
|
||||||
|
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_from_associative_arrays.csv');
|
||||||
|
$writtenContent = $this->trimWrittenContent($writtenContent);
|
||||||
|
|
||||||
|
$this->assertEquals('csv--11,csv--12', $writtenContent, 'Values from associative arrays should be written');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -23,7 +23,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testAddRowShouldThrowExceptionIfCannotOpenAFileForWriting()
|
public function testAddRowShouldThrowExceptionIfCannotOpenAFileForWriting()
|
||||||
{
|
{
|
||||||
$fileName = 'file_that_wont_be_written.ods';
|
$fileName = 'file_that_wont_be_written.ods';
|
||||||
$this->createUnwritableFolderIfNeeded($fileName);
|
$this->createUnwritableFolderIfNeeded();
|
||||||
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);
|
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);
|
||||||
|
|
||||||
$writer = WriterFactory::create(Type::ODS);
|
$writer = WriterFactory::create(Type::ODS);
|
||||||
@ -177,6 +177,25 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAddRowShouldSupportAssociativeArrays()
|
||||||
|
{
|
||||||
|
$fileName = 'test_add_row_should_support_associative_arrays.ods';
|
||||||
|
$dataRows = [
|
||||||
|
['foo' => 'ods--11', 'bar' => 'ods--12'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->writeToODSFile($dataRows, $fileName);
|
||||||
|
|
||||||
|
foreach ($dataRows as $dataRow) {
|
||||||
|
foreach ($dataRow as $cellValue) {
|
||||||
|
$this->assertValueWasWritten($fileName, $cellValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -21,7 +21,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testAddRowShouldThrowExceptionIfCannotOpenAFileForWriting()
|
public function testAddRowShouldThrowExceptionIfCannotOpenAFileForWriting()
|
||||||
{
|
{
|
||||||
$fileName = 'file_that_wont_be_written.xlsx';
|
$fileName = 'file_that_wont_be_written.xlsx';
|
||||||
$this->createUnwritableFolderIfNeeded($fileName);
|
$this->createUnwritableFolderIfNeeded();
|
||||||
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);
|
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);
|
||||||
|
|
||||||
$writer = WriterFactory::create(Type::XLSX);
|
$writer = WriterFactory::create(Type::XLSX);
|
||||||
@ -232,6 +232,25 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testAddRowShouldSupportAssociativeArrays()
|
||||||
|
{
|
||||||
|
$fileName = 'test_add_row_should_support_associative_arrays.xlsx';
|
||||||
|
$dataRows = [
|
||||||
|
['foo' => 'xlsx--11', 'bar' => 'xlsx--12'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true);
|
||||||
|
|
||||||
|
foreach ($dataRows as $dataRow) {
|
||||||
|
foreach ($dataRow as $cellValue) {
|
||||||
|
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cellValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user