Skip to content

Commit

Permalink
ConverterTrait improved
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 17, 2014
1 parent 82edad4 commit 51bbbd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
58 changes: 23 additions & 35 deletions src/ConverterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function output($filename = null)
//@codeCoverageIgnoreStart
if (! is_null($filename) && AbstractCsv::isValidString($filename)) {
header('Content-Type: text/csv; charset="'.$this->encoding.'"');
header('Content-Disposition: attachment; filename="firstname.csv"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
if (! $iterator instanceof SplTempFileObject) {
header('Content-Length: '.$iterator->getSize());
}
Expand All @@ -101,58 +101,46 @@ public function __toString()
}

/**
* Return a HTML table representation of the CSV Table
* transform a CSV into a XML
*
* @param string $classname optional classname
* @param string $root_name XML root node name
* @param string $row_name XML row node name
* @param string $cell_name XML cell node name
*
* @return string
* @return \DomDocument
*/
public function toHTML($classname = 'table-csv-data')
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
{
$doc = new DomDocument('1.0', $this->encoding);
$table = $doc->createElement('table');
$table->setAttribute('class', $classname);
foreach ($this->getIterator() as $row) {
$tr = $doc->createElement('tr');
$doc = new DomDocument('1.0', 'UTF-8');
$root = $doc->createElement($root_name);
foreach ($this->convert2Utf8() as $row) {
$item = $doc->createElement($row_name);
foreach ($row as $value) {
$content = $doc->createTextNode($value);
$td = $doc->createElement('td');
$td->appendChild($content);
$tr->appendChild($td);
$cell = $doc->createElement($cell_name);
$cell->appendChild($content);
$item->appendChild($cell);
}
$table->appendChild($tr);
$root->appendChild($item);
}
$doc->appendChild($root);

return $doc->saveHTML($table);
return $doc;
}

/**
* transform a CSV into a XML
* Return a HTML table representation of the CSV Table
*
* @param string $root_name XML root node name
* @param string $row_name XML row node name
* @param string $cell_name XML cell node name
* @param string $classname optional classname
*
* @return string
*/
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
public function toHTML($classname = 'table-csv-data')
{
$doc = new DomDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$table = $doc->createElement($root_name);
foreach ($this->convert2Utf8() as $row) {
$tr = $doc->createElement($row_name);
foreach ($row as $value) {
$content = $doc->createTextNode($value);
$td = $doc->createElement($cell_name);
$td->appendChild($content);
$tr->appendChild($td);
}
$table->appendChild($tr);
}
$doc->appendChild($table);
$doc = $this->toXML('table', 'tr', 'td');
$doc->documentElement->setAttribute('class', $classname);

return $doc->saveXML();
return $doc->saveHTML($doc->documentElement);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion test/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ public function testToXML()
</csv>
EOF;
$this->assertSame($expected, $this->csv->toXML());
$doc = $this->csv->toXML();
$this->assertInstanceof('\DomDocument', $doc);
$doc->formatOutput = true;
$this->assertSame($expected, $doc->saveXML());
}

/**
Expand Down

0 comments on commit 51bbbd9

Please sign in to comment.