summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2014-02-16 22:38:24 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2014-02-16 22:38:24 +0100
commit8a3ce4d7f8673227cd60f5a99e5a047c44c9b19b (patch)
treeb3aa962722a8db67e086f10cff4f13709beb7ce4
parentd2a17e10e9e1cb72218263cf66aa8838e70ba899 (diff)
downloadcsv-8a3ce4d7f8673227cd60f5a99e5a047c44c9b19b.zip
csv-8a3ce4d7f8673227cd60f5a99e5a047c44c9b19b.tar.gz
csv-8a3ce4d7f8673227cd60f5a99e5a047c44c9b19b.tar.bz2
move AbstractCsv output methods to ConverterTrait trait
-rw-r--r--examples/download.php4
-rw-r--r--examples/json.php2
-rw-r--r--examples/xml.php16
-rw-r--r--src/AbstractCsv.php75
-rw-r--r--src/ConverterTrait.php154
-rw-r--r--test/CsvTest.php21
6 files changed, 197 insertions, 75 deletions
diff --git a/examples/download.php b/examples/download.php
index 3e017b6..150827c 100644
--- a/examples/download.php
+++ b/examples/download.php
@@ -6,6 +6,4 @@ require '../vendor/autoload.php';
$inputCsv = new Reader('data/prenoms.csv');
$inputCsv->setEncoding('ISO-8859-15');
-header('Content-Type: text/csv; charset="'.$inputCsv->getEncoding().'"');
-header('Content-Disposition: attachment; filename="firstname.csv"');
-$inputCsv->output();
+$inputCsv->output('firstname.csv'); //specifying a filename triggers header sending
diff --git a/examples/json.php b/examples/json.php
index b4caf2e..a679fec 100644
--- a/examples/json.php
+++ b/examples/json.php
@@ -12,5 +12,5 @@ $res = json_encode($inputCsv, JSON_PRETTY_PRINT|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_
if (JSON_ERROR_NONE != json_last_error()) {
die(json_last_error_msg());
}
-header('Content-Type: application/json; charset="'.$inputCsv->getEncoding().'"');
+header('Content-Type: application/json; charset="utf-8"');
die($res);
diff --git a/examples/xml.php b/examples/xml.php
new file mode 100644
index 0000000..650c453
--- /dev/null
+++ b/examples/xml.php
@@ -0,0 +1,16 @@
+<?php
+
+error_reporting(-1);
+ini_set('display_errors', 1);
+
+use Bakame\Csv\Reader;
+
+require '../vendor/autoload.php';
+
+$inputCsv = new Reader('data/prenoms.csv');
+$inputCsv->setEncoding('ISO-8859-15');
+$inputCsv->setDelimiter(';');
+$xml = $inputCsv->toXML('csv', 'ligne', 'cellule');
+header('Content-Type: application/xml; charset="utf-8"');
+header('Content-Length: '.strlen($xml));
+die($xml);
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index a10a2c4..cdf0469 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -33,14 +33,12 @@
namespace Bakame\Csv;
use IteratorAggregate;
-use DomDocument;
use JsonSerializable;
use RuntimeException;
use SplFileInfo;
use SplFileObject;
use SplTempFileObject;
use InvalidArgumentException;
-use Bakame\Csv\Iterator\MapIterator;
/**
* A abstract class to enable basic CSV manipulation
@@ -51,6 +49,9 @@ use Bakame\Csv\Iterator\MapIterator;
*/
class AbstractCsv implements JsonSerializable, IteratorAggregate
{
+
+ use ConverterTrait;
+
/**
* The CSV object holder
*
@@ -153,7 +154,7 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate
*
* @return boolean
*/
- protected static function isValidString($str)
+ public static function isValidString($str)
{
return (is_scalar($str) || (is_object($str) && method_exists($str, '__toString')));
}
@@ -345,72 +346,4 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate
return $this->csv;
}
-
- /**
- * Output all data on the CSV file
- */
- public function output()
- {
- $iterator = $this->getIterator();
- $iterator->rewind();
- $iterator->fpassthru();
- }
-
- /**
- * Retrieves the CSV content
- *
- * @return string
- */
- public function __toString()
- {
- ob_start();
- $this->output();
-
- return ob_get_clean();
- }
-
- /**
- * Return a HTML table representation of the CSV Table
- *
- * @param string $classname optional classname
- *
- * @return string
- */
- public function toHTML($classname = 'table-csv-data')
- {
- $doc = new DomDocument('1.0', $this->encoding);
- $table = $doc->createElement('table');
- $table->setAttribute('class', $classname);
- foreach ($this->getIterator() as $row) {
- $tr = $doc->createElement('tr');
- foreach ($row as $value) {
- $tr->appendChild($doc->createElement('td', htmlspecialchars($value, ENT_COMPAT, $this->encoding)));
- }
- $table->appendChild($tr);
- }
-
- return $doc->saveHTML($table);
- }
-
- /**
- * JsonSerializable Interface
- *
- * @return array
- */
- public function jsonSerialize()
- {
- $iterator = $this->getIterator();
- if ('UTF-8' != $this->encoding) {
- $iterator = new MapIterator($iterator, function ($row) {
- foreach ($row as &$value) {
- $value = mb_convert_encoding($value, 'UTF-8', $this->encoding);
- }
- unset($value);
-
- return $row;
- });
- }
-
- return iterator_to_array($iterator, false);
- }
}
diff --git a/src/ConverterTrait.php b/src/ConverterTrait.php
new file mode 100644
index 0000000..5d8a49e
--- /dev/null
+++ b/src/ConverterTrait.php
@@ -0,0 +1,154 @@
+<?php
+/**
+* Bakame.csv - A lightweight CSV Coder/Decoder library
+*
+* @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
+* @copyright 2014 Ignace Nyamagana Butera
+* @link https://github.com/nyamsprod/Bakame.csv
+* @license http://opensource.org/licenses/MIT
+* @version 4.0.0
+* @package Bakame.csv
+*
+* MIT LICENSE
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+namespace Bakame\Csv;
+
+use DomDocument;
+use SplTempFileObject;
+use Bakame\Csv\Iterator\MapIterator;
+
+/**
+ * A abstract class to enable basic CSV manipulation
+ *
+ * @package Bakame.csv
+ * @since 4.0.0
+ *
+ */
+trait ConverterTrait
+{
+ /**
+ * Output all data on the CSV file
+ */
+ public function output($filename = null)
+ {
+ $iterator = $this->getIterator();
+ //@codeCoverageIgnoreStart
+ if (! is_null($filename) && AbstractCsv::isValidString($filename)) {
+ header('Content-Type: text/csv; charset="'.$this->encoding.'"');
+ header('Content-Disposition: attachment; filename="firstname.csv"');
+ if (! $iterator instanceof SplTempFileObject) {
+ header('Content-Length: '.$iterator->getSize());
+ }
+ }
+ //@codeCoverageIgnoreEnd
+ $iterator->rewind();
+ $iterator->fpassthru();
+ }
+
+ /**
+ * Retrieves the CSV content
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ ob_start();
+ $this->output();
+
+ return ob_get_clean();
+ }
+
+ /**
+ * Return a HTML table representation of the CSV Table
+ *
+ * @param string $classname optional classname
+ *
+ * @return string
+ */
+ public function toHTML($classname = 'table-csv-data')
+ {
+ $doc = new DomDocument('1.0', $this->encoding);
+ $table = $doc->createElement('table');
+ $table->setAttribute('class', $classname);
+ foreach ($this->getIterator() as $row) {
+ $tr = $doc->createElement('tr');
+ foreach ($row as $value) {
+ $content = $doc->createTextNode($value);
+ $td = $doc->createElement('td');
+ $td->appendChild($content);
+ $tr->appendChild($td);
+ }
+ $table->appendChild($tr);
+ }
+
+ return $doc->saveHTML($table);
+ }
+
+ protected function convert2Utf8()
+ {
+ $iterator = $this->getIterator();
+ if ('UTF-8' != $this->encoding) {
+ $iterator = new MapIterator($iterator, function ($row) {
+ foreach ($row as &$value) {
+ $value = mb_convert_encoding($value, 'UTF-8', $this->encoding);
+ }
+ unset($value);
+
+ return $row;
+ });
+ }
+
+ return $iterator;
+ }
+
+ public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
+ {
+ $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);
+
+ return $doc->saveXML();
+ }
+
+ /**
+ * JsonSerializable Interface
+ *
+ * @return array
+ */
+ public function jsonSerialize()
+ {
+ $iterator = $this->convert2Utf8();
+
+ return iterator_to_array($iterator, false);
+ }
+}
diff --git a/test/CsvTest.php b/test/CsvTest.php
index e894ec8..36f4515 100644
--- a/test/CsvTest.php
+++ b/test/CsvTest.php
@@ -171,6 +171,27 @@ EOF;
$this->assertSame($expected, $this->csv->toHTML());
}
+ public function testToXML()
+ {
+ $expected = <<<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<csv>
+ <row>
+ <cell>john</cell>
+ <cell>doe</cell>
+ <cell>john.doe@example.com</cell>
+ </row>
+ <row>
+ <cell>jane</cell>
+ <cell>doe</cell>
+ <cell>jane.doe@example.com</cell>
+ </row>
+</csv>
+
+EOF;
+ $this->assertSame($expected, $this->csv->toXML());
+ }
+
/**
* @param $rawCsv
*