Skip to content

Commit

Permalink
ability to prepend header in csv und excel outputs
Browse files Browse the repository at this point in the history
xmlwriter writes rootnode on prepare
fixed some tests
  • Loading branch information
mathielen committed Jun 29, 2015
1 parent 4708ea1 commit 11b412c
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 19 deletions.
73 changes: 73 additions & 0 deletions src/Mathielen/DataImport/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Mathielen\DataImport\Writer;
use Ddeboer\DataImport\Writer\AbstractStreamWriter;

/**
* Writes to a CSV file
*
* @author David de Boer <[email protected]>
*/
class CsvWriter extends AbstractStreamWriter
{
/**
* @var string
*/
private $delimiter = ';';

/**
* @var string
*/
private $enclosure = '"';

/**
* @var boolean
*/
private $utf8Encoding = false;
private $row = 1;

/**
* @var boolean
*/
protected $prependHeaderRow;

/**
* @param string $delimiter The delimiter
* @param string $enclosure The enclosure
* @param resource $stream
* @param boolean $utf8Encoding
*/
public function __construct($delimiter = ';', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false)
{
parent::__construct($stream);

$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->utf8Encoding = $utf8Encoding;

$this->prependHeaderRow = $prependHeaderRow;
}

/**
* {@inheritdoc}
*/
public function prepare()
{
if ($this->utf8Encoding) {
fprintf($this->getStream(), chr(0xEF) . chr(0xBB) . chr(0xBF));
}
}

/**
* {@inheritdoc}
*/
public function writeItem(array $item)
{
if ($this->prependHeaderRow && 1 == $this->row++) {
$headers = array_keys($item);
fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure);
}

fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure);
}
}
113 changes: 113 additions & 0 deletions src/Mathielen/DataImport/Writer/ExcelWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Mathielen\DataImport\Writer;

use Ddeboer\DataImport\Writer\AbstractWriter;
use PHPExcel;
use PHPExcel_IOFactory;

/**
* Writes to an Excel file
*
* @author David de Boer <[email protected]>
*/
class ExcelWriter extends AbstractWriter
{
/**
* @var string
*/
protected $filename;

/**
* @var null|string
*/
protected $sheet;

/**
* @var string
*/
protected $type;

/**
* @var boolean
*/
protected $prependHeaderRow;

/**
* @var PHPExcel
*/
protected $excel;

/**
* @var integer
*/
protected $row = 1;

/**
* @param \SplFileObject $file File
* @param string $sheet Sheet title (optional)
* @param string $type Excel file type (defaults to Excel2007)
* @param boolean $prependHeaderRow
*/
public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
{
$this->filename = $file->getPathname();
$this->sheet = $sheet;
$this->type = $type;
$this->prependHeaderRow = $prependHeaderRow;
}

/**
* {@inheritdoc}
*/
public function prepare()
{
$reader = PHPExcel_IOFactory::createReader($this->type);
if ($reader->canRead($this->filename)) {
$this->excel = $reader->load($this->filename);
} else {
$this->excel = new PHPExcel();
}

if (null !== $this->sheet) {
if (!$this->excel->sheetNameExists($this->sheet)) {
$this->excel->createSheet()->setTitle($this->sheet);
}
$this->excel->setActiveSheetIndexByName($this->sheet);
}
}

/**
* {@inheritdoc}
*/
public function writeItem(array $item)
{
$count = count($item);

if ($this->prependHeaderRow && 1 == $this->row) {
$headers = array_keys($item);

for ($i = 0; $i < $count; $i++) {
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]);
}
$this->row++;
}

$values = array_values($item);

for ($i = 0; $i < $count; $i++) {
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
}

$this->row++;
}

/**
* {@inheritdoc}
*/
public function finish()
{
$writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
$writer->save($this->filename);
}
}
21 changes: 15 additions & 6 deletions src/Mathielen/DataImport/Writer/XmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ class XmlWriter implements WriterInterface
private $xml;
private $filename;

private $nodeName;
/**
* @var \DOMNode
*/
private $rootNode;

private $rowNodeName;
private $rootNodeName;

public function __construct(\SplFileObject $file, $nodeName='node')
public function __construct(\SplFileObject $file, $rowNodeName='node', $rootNodeName = 'root')
{
$this->filename = $file->getRealPath();
$this->nodeName = $nodeName;
$this->rowNodeName = $rowNodeName;
$this->rootNodeName = $rootNodeName;
}

/**
Expand All @@ -29,6 +36,8 @@ public function __construct(\SplFileObject $file, $nodeName='node')
public function prepare()
{
$this->xml = new \DOMDocument();
$this->rootNode = $this->xml->createElement($this->rootNodeName);
$this->xml->appendChild($this->rootNode);

return $this;
}
Expand All @@ -39,10 +48,10 @@ public function prepare()
*/
public function writeItem(array $item)
{
$newNode = $this->xml->createElement($this->nodeName);
$newNode = $this->xml->createElement($this->rowNodeName);

//attributes
if (array_key_exists('@attributes', $item) && is_array($item['@attributes'])) {
if (isset($item['@attributes']) && is_array($item['@attributes'])) {
foreach ($item['@attributes'] as $key => $value) {
$attr = $this->xml->createAttribute($key);
$attr->value = $value;
Expand All @@ -58,7 +67,7 @@ public function writeItem(array $item)
$newNode->appendChild($node);
}

$this->xml->appendChild( $newNode );
$this->rootNode->appendChild( $newNode );

return $this;
}
Expand Down
16 changes: 5 additions & 11 deletions src/Mathielen/ImportEngine/Storage/LocalFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\Reader\ExcelReader;
use Ddeboer\DataImport\Reader\ReaderInterface;
use Ddeboer\DataImport\Writer\CsvWriter;
use Ddeboer\DataImport\Writer\ExcelWriter;
use Mathielen\DataImport\Writer\CsvWriter;
use Mathielen\DataImport\Writer\ExcelWriter;
use Mathielen\DataImport\Writer\XmlWriter;
use Ddeboer\DataImport\Writer\WriterInterface;
use Mathielen\DataImport\Reader\XmlReader;
Expand Down Expand Up @@ -128,19 +128,13 @@ public function writer()
private function formatToWriter(Format $format, \SplFileInfo $file)
{
if ($format instanceof CsvFormat) {
$writer = new CsvWriter($format->getDelimiter(), $format->getEnclosure(), fopen($file, 'w'));
if ($format->isHeaderInFirstRow()) {
//TODO how to handle header?
}
$writer = new CsvWriter($format->getDelimiter(), $format->getEnclosure(), fopen($file, 'a'), false, $format->isHeaderInFirstRow());

} elseif ($format instanceof ExcelFormat) {
$writer = new ExcelWriter($file->openFile('w'), $format->getActivesheet(), $format->getExceltype());
if ($format->isHeaderInFirstRow()) {
//TODO how to handle header?
}
$writer = new ExcelWriter($file->openFile('a'), $format->getActivesheet(), $format->getExceltype(), $format->isHeaderInFirstRow());

} elseif ($format instanceof XmlFormat) {
$writer = new XMLWriter($file->openFile('w'));
$writer = new XMLWriter($file->openFile('a'));

} elseif ($format instanceof CompressedFormat) {
throw new \LogicException("Not implemented!");
Expand Down
9 changes: 7 additions & 2 deletions tests/functional/Mathielen/ImportEngine/LocalFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ public function testImport($sourceFile, Format $format, Format $targetFormat=nul
$importRunner->run($import);

$this->assertFileExists($targetFile);
//$this->assertFileEquals($sourceFile, $targetFile); excel files have binary differences :/

if ($format instanceof XmlFormat) {
$this->assertXmlFileEqualsXmlFile($sourceFile, $targetFile);
} elseif ($format instanceof CsvFormat) {
$this->assertFileEquals($sourceFile, $targetFile);
}
}

public function getStorages()
{
return array(
array(__DIR__ . '/../../../metadata/testfiles/flatdata.csv', new CsvFormat(';', '"', '\\', false)),
array(__DIR__ . '/../../../metadata/testfiles/flatdata.csv', new CsvFormat()),
array(__DIR__ . '/../../../metadata/testfiles/flatdata-excel.xls', new ExcelFormat(false)),
array(__DIR__ . '/../../../metadata/testfiles/flatdata-excel-xml.xlsx', new ExcelFormat(false)),
array(__DIR__ . '/../../../metadata/testfiles/flatdata-csv-zip.zip', new CompressedFormat('testmapping.csv', 'zip', new CsvFormat()), new CsvFormat()),
Expand Down

0 comments on commit 11b412c

Please sign in to comment.