diff options
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | examples/README.md | 1 | ||||
-rw-r--r-- | examples/example05.php | 18 | ||||
-rw-r--r-- | examples/example06.php | 74 | ||||
-rw-r--r-- | src/Bakame/Csv/AbstractCsv.php | 6 | ||||
-rw-r--r-- | src/Bakame/Csv/Writer.php | 2 | ||||
-rw-r--r-- | test/Bakame/Csv/CsvTest.php | 10 |
7 files changed, 86 insertions, 28 deletions
diff --git a/composer.json b/composer.json index f31001b..048c3dc 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,7 @@ ], "require": { "php" : ">=5.4.0", - "ext-mbstring" : "*", - "ext-iconv" : "*" + "ext-mbstring" : "*" }, "autoload": { "psr-0": { diff --git a/examples/README.md b/examples/README.md index 85eb91a..4ed3acf 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,6 +8,7 @@ Examples * [Selecting a specific row in the CSV](example03.php) * [Filtering a CSV](example05.php) using the `Bakame\Csv\Reader` class * [Creating a CSV](example05.php) using the `Bakame\Csv\Writer` class +* [Passing a CSV from writing mode to Reader mode](example06.php) The CSV use for the example is from [Paris Opendata](http://opendata.paris.fr/opendata/jsp/site/Portal.jsp?document_id=60&portlet_id=121) diff --git a/examples/example05.php b/examples/example05.php index e98a2e9..74cbffc 100644 --- a/examples/example05.php +++ b/examples/example05.php @@ -37,17 +37,6 @@ $headers = $inputCsv->fetchOne(0); $writer = new Writer(new SplTempFileObject); //because we don't want to create the file $writer->insertOne($headers); $writer->insertAll($res); - -//we create a Reader object from the Writer object to filter the resulting CSV -$reader = $writer->getReader(); -$names = $reader - ->setFilter(function ($row, $index) { - return $index > 0; //we don't want to select the header - }) - ->setSortBy(function ($row1, $row2) { - return strcmp($row1[0], $row2[0]); //we are sorting the name - }) - ->fetchCol(0); //we only return the name column ?> <!doctype html> <html lang="fr"> @@ -64,13 +53,6 @@ $names = $reader <?=$writer?> </pre> <p><em>Notice that the delimiter have changed from <code>;</code> to <code>,</code></em></p> -<h3>Here's the firstname ordered list</h3> -<ol> -<?php foreach ($names as $firstname) : ?> - <li><?=$firstname?> - <?php -endforeach; -?> </ol> </body> </html> diff --git a/examples/example06.php b/examples/example06.php new file mode 100644 index 0000000..b62d491 --- /dev/null +++ b/examples/example06.php @@ -0,0 +1,74 @@ +<?php + +error_reporting(-1); +ini_set('display_errors', 'On'); + +use Bakame\Csv\Writer; + +require '../vendor/autoload.php'; + +$rawCsv = <<<EOF +Anatole;31;M;2004 +Andre;13;M;2004 +Andrea;33;F;2004 +Andrea;20;F;2004 +Andy;19;M;2004 +Ange;15;M;2004 +Angela;9;F;2004 +Angèle;29;F;2004 +Angelina;8;F;2004 +Angelina;7;F;2004 +Angelique;13;F;2004 +Angelo;9;M;2004 +Ania;7;F;2004 +Anis;33;M;2004 +Anissa;21;F;2004 +Anna;117;F;2004 +Annabelle;14;F;2004 +Anne;10;F;2004 +Anouk;48;F;2004 +Anthony;41;M;2004 +Antoine;248;M;2004 +Anton;16;M;2004 +EOF; + +$writer = Writer::createFromString($rawCsv); //we are creating a CSV from a raw string +$writer->setDelimiter(';'); +$writer->insertOne('Ben;7;M;2004'); //because we specified the delimiter to ";" the string delimiter MUST also be ";" +$writer->insertAll([ + 'Benjamin;118;M;2004', + ['Benoit', '6', 'M', '2004'] //because we a inserting an array the delimiter is not necessary +]); + +//we create a Reader object from the Writer object to filter the resulting CSV +$reader = $writer->getReader(); +$names = $reader + ->setSortBy(function ($row1, $row2) { + return strcmp($row1[0], $row2[0]); //we are sorting the name + }) + ->fetchCol(0); //we only return the name column +?> +<!doctype html> +<html lang="fr"> +<head> + <meta charset="utf-8"> + <title>Example 2</title> +</head> +<body> +<h1>Example 4: Using Writer object with Strings</h1> +<h3>The table representation of the csv to be save</h3> +<?=$writer->toHTML();?> +<h3>The Raw CSV as it will be saved</h3> +<pre> +<?=$writer?> +</pre> +<h3>Here's the firstname ordered list</h3> +<ol> +<?php foreach ($names as $firstname) : ?> + <li><?=$firstname?> + <?php +endforeach; +?> +</ol> +</body> +</html> diff --git a/src/Bakame/Csv/AbstractCsv.php b/src/Bakame/Csv/AbstractCsv.php index c8b2983..4077115 100644 --- a/src/Bakame/Csv/AbstractCsv.php +++ b/src/Bakame/Csv/AbstractCsv.php @@ -135,7 +135,9 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate { if (self::isValidString($str)) { $csv = new SplTempFileObject; - $csv->fwrite((string) $str); + $raw = (string) $str; + $raw .= PHP_EOL; + $csv->fwrite($raw); return new static($csv); } @@ -401,7 +403,7 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate if ('UTF-8' != $this->encoding) { $iterator = new MapIterator($iterator, function ($row) { foreach ($row as &$value) { - $value = iconv($this->encoding, 'UTF-8//TRANSLIT', $value); + $value = mb_convert_encoding($value, 'UTF-8', $this->encoding); } unset($value); diff --git a/src/Bakame/Csv/Writer.php b/src/Bakame/Csv/Writer.php index 2b62d37..c853324 100644 --- a/src/Bakame/Csv/Writer.php +++ b/src/Bakame/Csv/Writer.php @@ -80,7 +80,7 @@ class Writer extends AbstractCsv return self::isValidString($value); }); if (count($check) == count($row)) { - $this->csv->fputcsv($row); + $this->csv->fputcsv($row, $this->delimiter, $this->enclosure); return $this; } diff --git a/test/Bakame/Csv/CsvTest.php b/test/Bakame/Csv/CsvTest.php index 7dd34a2..e912505 100644 --- a/test/Bakame/Csv/CsvTest.php +++ b/test/Bakame/Csv/CsvTest.php @@ -65,9 +65,9 @@ class CsvTest extends PHPUnit_Framework_TestCase { $expected = "john,doe,john.doe@example.com".PHP_EOL ."jane,doe,jane.doe@example.com".PHP_EOL; - foreach (Reader::createFromString($expected) as $key => $row) { - $this->assertSame($this->expected[$key], $row); - } + $reader = Reader::createFromString($expected); + $this->assertSame($reader->fetchOne(0), ['john', 'doe', 'john.doe@example.com']); + $this->assertSame($reader->fetchOne(1), ['jane', 'doe', 'jane.doe@example.com']); } /** @@ -178,8 +178,8 @@ EOF; $this->assertSame(json_encode($this->expected), json_encode($this->csv)); $csv = Reader::createFromString($rawCsv); $csv->setEncoding('iso-8859-15'); - $this->assertStringStartsWith('[[', json_encode($csv)); - + json_encode($csv); + $this->assertEquals(JSON_ERROR_NONE, json_last_error()); } public static function getIso8859Csv() |