summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2014-02-16 22:53:21 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2014-02-16 23:02:51 +0100
commit82edad42b60b5744937d6b6b7210560a85b2996b (patch)
tree637a9a91717357acf455db8969f8762089f092a1
parent8a3ce4d7f8673227cd60f5a99e5a047c44c9b19b (diff)
downloadcsv-82edad42b60b5744937d6b6b7210560a85b2996b.zip
csv-82edad42b60b5744937d6b6b7210560a85b2996b.tar.gz
csv-82edad42b60b5744937d6b6b7210560a85b2996b.tar.bz2
Documentation update
-rw-r--r--README.md21
-rw-r--r--src/AbstractCsv.php6
-rw-r--r--src/ConverterTrait.php53
-rw-r--r--src/Iterator/IteratorQuery.php2
-rw-r--r--src/Iterator/MapIterator.php2
-rw-r--r--src/Reader.php2
-rw-r--r--src/Writer.php2
7 files changed, 63 insertions, 25 deletions
diff --git a/README.md b/README.md
index d803628..616ec43 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ Usage
* [Downloading the CSV](examples/download.php)
* [Converting the CSV into a Json String](examples/json.php)
* [Converting the CSV into a HTML Table](examples/table.php)
+* [Converting the CSV into a XML file](examples/xml.php)
* [Selecting specific rows in the CSV](examples/extract.php)
* [Filtering a CSV](examples/filtering.php)
* [Creating a CSV](examples/writing.php)
@@ -140,12 +141,26 @@ Use the `toHTML` method to format the CSV data into an HTML table. This method a
echo $writer->toHTML('table table-bordered table-hover');
```
+#### convert the CSV into an XML string:
+
+Use the `toXML` method to format the CSV data into an XML String. This methods accepts 3 optionals arguments `$root_name`, `$row_name`, `$cell_name` to help you customize the XML tree.
+
+By default:
+* `$root_name = 'csv'`
+* `$row_name = 'row'`
+* `$cell_name = 'cell'`
+
+```php
+echo $writer->toXML('data', 'item', 'cell');
+```
+
#### convert the CSV into a Json string:
Use the `json_encode` function directly on the instantiated object.
```php
echo json_encode($writer);
+
```
#### make the CSV downloadable
@@ -158,6 +173,12 @@ header('Content-Type: text/csv; charset="'.$reader->getEncoding().'"');
header('Content-Disposition: attachment; filename="name-for-your-file.csv"');
$reader->output();
```
+The output method can take an optional argument `$filename`. When present you can even omit most of the headers.
+
+```php
+$reader->setEncoding('ISO-8859-15');
+$reader->output("name-for-your-file.csv");
+```
Extracting data from the CSV
-------
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index cdf0469..c98fc90 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE
@@ -49,7 +49,9 @@ use InvalidArgumentException;
*/
class AbstractCsv implements JsonSerializable, IteratorAggregate
{
-
+ /**
+ * Trait to output the full CSV data
+ */
use ConverterTrait;
/**
diff --git a/src/ConverterTrait.php b/src/ConverterTrait.php
index 5d8a49e..83d5844 100644
--- a/src/ConverterTrait.php
+++ b/src/ConverterTrait.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE
@@ -40,13 +40,36 @@ use Bakame\Csv\Iterator\MapIterator;
* A abstract class to enable basic CSV manipulation
*
* @package Bakame.csv
- * @since 4.0.0
+ * @since 4.2.0
*
*/
trait ConverterTrait
{
/**
+ * Convert Csv file into UTF-8
+ *
+ * @return \Iterator
+ */
+ protected function convert2Utf8()
+ {
+ if ('UTF-8' == $this->encoding) {
+ return $this->getIterator();
+ }
+
+ return new MapIterator($this->getIterator(), function ($row) {
+ foreach ($row as &$value) {
+ $value = mb_convert_encoding($value, 'UTF-8', $this->encoding);
+ }
+ unset($value);
+
+ return $row;
+ });
+ }
+
+ /**
* Output all data on the CSV file
+ *
+ * @param string $filename CSV downloaded name if present adds extra headers
*/
public function output($filename = null)
{
@@ -103,23 +126,15 @@ trait ConverterTrait
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;
- }
-
+ /**
+ * transform a CSV into a XML
+ *
+ * @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
+ */
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
{
$doc = new DomDocument('1.0', 'UTF-8');
diff --git a/src/Iterator/IteratorQuery.php b/src/Iterator/IteratorQuery.php
index 5541b43..f498f5a 100644
--- a/src/Iterator/IteratorQuery.php
+++ b/src/Iterator/IteratorQuery.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE
diff --git a/src/Iterator/MapIterator.php b/src/Iterator/MapIterator.php
index 12843c0..866adf4 100644
--- a/src/Iterator/MapIterator.php
+++ b/src/Iterator/MapIterator.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE
diff --git a/src/Reader.php b/src/Reader.php
index 15cfede..ee7ac6b 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE
diff --git a/src/Writer.php b/src/Writer.php
index c853324..f151988 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -6,7 +6,7 @@
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/Bakame.csv
* @license http://opensource.org/licenses/MIT
-* @version 4.0.0
+* @version 4.2.0
* @package Bakame.csv
*
* MIT LICENSE