Skip to content

Commit

Permalink
Update examples for version 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 11, 2015
1 parent 630f690 commit 0a1b535
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All Notable changes to `Csv` will be documented in this file

## Next - 2015-xx-xx
## 8.0.0 - 2015-12-11

### Added

Expand All @@ -20,6 +20,7 @@ All Notable changes to `Csv` will be documented in this file
- `Reader::fetchColumn` callable argument expects the selected column value as its first argument
- Default value on `setOutputBOM` is removed
- `AbstractCsv::getOutputBOM` always return a string
- `AbstractCsv::getInputBOM` always return a string

### Removed

Expand Down
8 changes: 4 additions & 4 deletions examples/example.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pre {
line-height: 1.5;
text-align: left;
-webkit-tab-size: 4;
-moz-tab-size: 4;
-ms-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;
-moz-tab-size: 4;
-ms-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;
}
body {
margin:0 auto;
Expand Down
8 changes: 4 additions & 4 deletions examples/extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
$headers = $inputCsv->fetchOne(0);

//get at maximum 25 rows starting from the 801th row
$res = $inputCsv->setOffset(800)->setLimit(25)->fetchAll();
$res = $inputCsv->setOffset(800)->setLimit(25)->fetch();
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>\League\Csv\Reader simple usage</title>
<title>League\Csv\Reader simple usage</title>
<link rel="stylesheet" href="example.css">
</head>
<body>
<h1>\League\Csv\Reader simple usage</h1>
<h1>League\Csv\Reader simple usage</h1>
<table class="table-csv-data">
<caption>Part of the CSV from the 801th row with at most 25 rows</caption>
<thead>
Expand All @@ -32,7 +32,7 @@
<tbody>
<?php foreach ($res as $row) : ?>
<tr>
<td><?=implode('</td>'.PHP_EOL.'<td>', $row), '</td>', PHP_EOL; ?>
<td><?=implode('</td>'.PHP_EOL.'<td>', $row), '</td>', PHP_EOL; ?>
</tr>
<?php
endforeach;
Expand Down
4 changes: 2 additions & 2 deletions examples/filtering.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
return strcmp($row1[1], $row2[1]); //we order the result according to the number of firstname given
})
->setLimit(20) //we just want the first 20 results
->fetchAll();
->fetch();

//get the headers
$headers = $inputCsv->fetchOne(0);
Expand All @@ -37,7 +37,7 @@
<html lang="fr">
<head>
<meta charset="iso-8859-15">
<title>\League\Csv\Reader filtering method</title>
<title>League\Csv\Reader filtering method</title>
<link rel="stylesheet" href="example.css">
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions examples/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
$inputCsv = Reader::createFromPath('data/prenoms.csv');
$inputCsv->setDelimiter(';');
$inputCsv->setEncodingFrom('ISO-8859-15');
//we limit the output to max. 10 rows
$inputCsv->setLimit(10);
$res = json_encode($inputCsv, JSON_PRETTY_PRINT|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
if (JSON_ERROR_NONE != json_last_error()) {
die(json_last_error_msg());
Expand Down
6 changes: 3 additions & 3 deletions examples/stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

var_dump($res);
var_dump(iterator_to_array($res, false));
?>
<p>Let's remove the <code><strong>string.toupper</strong></code> stream filter</p>
<pre><code>if ($reader->isActiveStreamFilter()) {
Expand All @@ -65,7 +65,7 @@
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

var_dump($res);</code></pre>
var_dump(iterator_to_array($res, false));</code></pre>

<?php
if ($reader->isActiveStreamFilter()) {
Expand All @@ -75,7 +75,7 @@
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

var_dump($res);
var_dump(iterator_to_array($res, false));
?>
<h3>Using the Writer class</h3>

Expand Down
2 changes: 1 addition & 1 deletion examples/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$inputCsv = Reader::createFromPath('data/prenoms.csv');
$inputCsv->setDelimiter(';');
$inputCsv->setEncodingFrom("iso-8859-15");
$inputCsv->setLimit(30);
$inputCsv->setLimit(30); //we are limiting the convertion to the first 31 rows
?>
<!doctype html>
<html lang="fr">
Expand Down
22 changes: 11 additions & 11 deletions examples/writing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@

require '../vendor/autoload.php';

$writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created into a temporary File
$writer->setDelimiter("\t"); //the delimiter will be the tab character
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries

$headers = ["position" , "team", "played", "goals difference", "points"];
$writer->insertOne($headers);
$header = ["position" , "team", "played", "goals difference", "points"];

$teams = [
$contents = [
[1, "Chelsea", 26, 27, 57],
[2, "Arsenal", 26, 22, 56],
[3, "Manchester City", 25, 41, 54],
[3, "Manchester City", 25, 41, 54,],
[4, "Liverpool", 26, 34, 53],
[5, "Tottenham", 26, 4, 50],
[6, "Everton", 25, 11, 45],
[7, "Manchester United", 26, 10, 42],
];

$writer->insertAll($teams);
$writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created using a temporary File
$writer->setDelimiter("\t"); //the delimiter will be the tab character
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
$writer->setOutputBOM(Writer::BOM_UTF8); //adding the BOM sequence on output
$writer->insertOne($header);
$writer->insertAll($contents);
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Using the \League\Csv\Writer object</title>
<title>Using the Writer class</title>
<link rel="stylesheet" href="example.css">
</head>
<body>
<h1>Example 4: Using Writer object</h1>
<h1>Example 4: Using the Writer class</h1>
<h3>The table representation of the csv</h3>
<?=$writer->toHTML('table-csv-data with-header');?>
<h3>The Raw CSV to be saved</h3>
Expand Down
11 changes: 6 additions & 5 deletions examples/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

require '../vendor/autoload.php';

//we order the result according to the number of firstname given
$func = function ($row1, $row2) {
return strcmp($row2[1], $row1[1]);
};

$csv = Reader::createFromPath('data/prenoms.csv');
$csv->setEncodingFrom('ISO-8859-15');
$csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
$csv->setDelimiter(';');
//since version 7.0 only 10 rows will be converted using the query options
$csv->setOffset(1);
$csv->setLimit(10);
$csv->addSortBy(function ($row1, $row2) {
return strcmp($row2[1], $row1[1]); //we order the result according to the number of firstname given
});
$csv->addSortBy($func);
$doc = $csv->toXML('csv', 'ligne', 'cellule');
$xml = $doc->saveXML();
header('Content-Type: application/xml; charset="utf-8"');
Expand Down

0 comments on commit 0a1b535

Please sign in to comment.