Skip to content

Commit

Permalink
Handle items that are not arrays in ExcelWriter
Browse files Browse the repository at this point in the history
If item is an object and headers are present use headers to retrieve values from item, if no headers are present or item is a scalar throw an exception.

Fixes plumphp#1
  • Loading branch information
Florian Eckerstorfer committed Oct 20, 2015
1 parent 334d294 commit 1ae764e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"require": {
"php": ">=5.5",
"plumphp/plum": "~0.4",
"phpoffice/phpexcel": "~1.8"
"phpoffice/phpexcel": "~1.8",
"cocur/vale": "~0.2"
},
"require-dev": {
"phpunit/phpunit": "~4.8",
Expand Down
32 changes: 29 additions & 3 deletions src/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPExcel_IOFactory;
use PHPExcel_Writer_IWriter;
use Plum\Plum\Writer\WriterInterface;
use Cocur\Vale\Vale;

/**
* ExcelWriter
Expand Down Expand Up @@ -114,12 +115,24 @@ public function autoDetectHeader($autoDetectHeader = true)
public function writeItem($item)
{
if ($this->autoDetectHeader && $this->header === null) {
$this->header = array_keys($item);
$this->writeItem($this->header);
$this->handleAutoDetectHeaders($item);
}

if (is_array($item)) {
$keys = array_keys($item);
} else if ($this->header && is_object($item)) {
$keys = $this->header;
} else {
throw new \InvalidArgumentException(sprintf(
'Plum\PlumExcel\ExcelWriter currently only supports array items or objects if headers are set using '.
'the setHeader() method. You have passed an item of type "%s" to writeItem().',
gettype($item)
));
}

$column = 0;
foreach ($item as $value) {
foreach ($keys as $key) {
$value = Vale::get($item, $key);
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($column, $this->currentRow, $value);
$column++;
}
Expand Down Expand Up @@ -155,4 +168,17 @@ public function finish()
}
$writer->save($this->filename);
}

protected function handleAutoDetectHeaders($item)
{
if (!is_array($item)) {
throw new \InvalidArgumentException(sprintf(
'Plum\PlumExcel\ExcelWriter currently only supports header detection if the item passed to '.
'writeItem() is an array. "%s" was passed writeItem().',
gettype($item)
));
}
$this->header = array_keys($item);
$this->writeItem($this->header);
}
}
79 changes: 78 additions & 1 deletion tests/ExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function prepareWritesHeader()
$this->writer->setHeader(['City', 'Country']);
$this->writer->prepare();
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
Expand All @@ -89,6 +89,71 @@ public function writeItemWritesItemToExcel()
$this->writer->writeItem(['City' => 'Vienna', 'Country' => 'Austria']);
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
*/
public function writeItemWritesItemToExcelIfItemIsObjectWithProperties()
{
$sheet = $this->getMockWorksheet();
$sheet->shouldReceive('setCellValueByColumnAndRow')->with(0, 1, 'Vienna')->once();
$sheet->shouldReceive('setCellValueByColumnAndRow')->with(1, 1, 'Austria')->once();

$this->excel->shouldReceive('getActiveSheet')->andReturn($sheet);

$item = new \stdClass();
$item->City = 'Vienna';
$item->Country = 'Austria';

$this->writer->setHeader(['City', 'Country']);
$this->writer->writeItem($item);
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
*/
public function writeItemWritesItemToExcelIfItemIsObjectHeadersSet()
{
$sheet = $this->getMockWorksheet();
$sheet->shouldReceive('setCellValueByColumnAndRow')->with(0, 1, 'Vienna')->once();
$sheet->shouldReceive('setCellValueByColumnAndRow')->with(1, 1, 'Austria')->once();

$this->excel->shouldReceive('getActiveSheet')->andReturn($sheet);

$item = new \stdClass();
$item->City = 'Vienna';
$item->Country = 'Austria';

$this->writer->setHeader(['City', 'Country']);
$this->writer->writeItem($item);
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
* @expectedException \InvalidArgumentException
*/
public function writeItemThrowsExceptionIfItemIsObjectHeadersNotSet()
{
$item = new \stdClass();
$item->City = 'Vienna';
$item->Country = 'Austria';

$this->writer->writeItem($item);
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
* @expectedException \InvalidArgumentException
*/
public function writeItemThrowsExceptionIfItemIsScalarEvenIfHeadersSet()
{
$this->writer->setHeader(['City']);
$this->writer->writeItem('Vienna');
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::autoDetectHeader()
Expand All @@ -108,6 +173,18 @@ public function writeItemWritesHeaderIfAutoDetectHeaderIsTrueAndItemToExcel()
$this->writer->writeItem(['City' => 'Vienna', 'Country' => 'Austria']);
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::autoDetectHeader()
* @covers Plum\PlumExcel\ExcelWriter::writeItem()
* @expectedException \InvalidArgumentException
*/
public function writeItemThrowsExceptionIfAutoDetectHeaderIsTrueAndItemIsNotArray()
{
$this->writer->autoDetectHeader();
$this->writer->writeItem(new \stdClass());
}

/**
* @test
* @covers Plum\PlumExcel\ExcelWriter::finish()
Expand Down

0 comments on commit 1ae764e

Please sign in to comment.