Skip to content

Commit 0a1b535

Browse files
committed
Update examples for version 8.0
1 parent 630f690 commit 0a1b535

File tree

9 files changed

+35
-31
lines changed

9 files changed

+35
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
## Next - 2015-xx-xx
5+
## 8.0.0 - 2015-12-11
66

77
### Added
88

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

2425
### Removed
2526

examples/example.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pre {
1313
line-height: 1.5;
1414
text-align: left;
1515
-webkit-tab-size: 4;
16-
-moz-tab-size: 4;
17-
-ms-tab-size: 4;
18-
-o-tab-size: 4;
19-
tab-size: 4;
16+
-moz-tab-size: 4;
17+
-ms-tab-size: 4;
18+
-o-tab-size: 4;
19+
tab-size: 4;
2020
}
2121
body {
2222
margin:0 auto;

examples/extract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
$headers = $inputCsv->fetchOne(0);
1212

1313
//get at maximum 25 rows starting from the 801th row
14-
$res = $inputCsv->setOffset(800)->setLimit(25)->fetchAll();
14+
$res = $inputCsv->setOffset(800)->setLimit(25)->fetch();
1515
?>
1616
<!doctype html>
1717
<html lang="fr">
1818
<head>
1919
<meta charset="utf-8">
20-
<title>\League\Csv\Reader simple usage</title>
20+
<title>League\Csv\Reader simple usage</title>
2121
<link rel="stylesheet" href="example.css">
2222
</head>
2323
<body>
24-
<h1>\League\Csv\Reader simple usage</h1>
24+
<h1>League\Csv\Reader simple usage</h1>
2525
<table class="table-csv-data">
2626
<caption>Part of the CSV from the 801th row with at most 25 rows</caption>
2727
<thead>
@@ -32,7 +32,7 @@
3232
<tbody>
3333
<?php foreach ($res as $row) : ?>
3434
<tr>
35-
<td><?=implode('</td>'.PHP_EOL.'<td>', $row), '</td>', PHP_EOL; ?>
35+
<td><?=implode('</td>'.PHP_EOL.'<td>', $row), '</td>', PHP_EOL; ?>
3636
</tr>
3737
<?php
3838
endforeach;

examples/filtering.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
return strcmp($row1[1], $row2[1]); //we order the result according to the number of firstname given
2929
})
3030
->setLimit(20) //we just want the first 20 results
31-
->fetchAll();
31+
->fetch();
3232

3333
//get the headers
3434
$headers = $inputCsv->fetchOne(0);
@@ -37,7 +37,7 @@
3737
<html lang="fr">
3838
<head>
3939
<meta charset="iso-8859-15">
40-
<title>\League\Csv\Reader filtering method</title>
40+
<title>League\Csv\Reader filtering method</title>
4141
<link rel="stylesheet" href="example.css">
4242
</head>
4343
<body>

examples/json.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
$inputCsv = Reader::createFromPath('data/prenoms.csv');
88
$inputCsv->setDelimiter(';');
99
$inputCsv->setEncodingFrom('ISO-8859-15');
10+
//we limit the output to max. 10 rows
11+
$inputCsv->setLimit(10);
1012
$res = json_encode($inputCsv, JSON_PRETTY_PRINT|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
1113
if (JSON_ERROR_NONE != json_last_error()) {
1214
die(json_last_error_msg());

examples/stream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
$reader->setLimit(3);
5656
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);
5757

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

68-
var_dump($res);</code></pre>
68+
var_dump(iterator_to_array($res, false));</code></pre>
6969

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

78-
var_dump($res);
78+
var_dump(iterator_to_array($res, false));
7979
?>
8080
<h3>Using the Writer class</h3>
8181

examples/table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$inputCsv = Reader::createFromPath('data/prenoms.csv');
88
$inputCsv->setDelimiter(';');
99
$inputCsv->setEncodingFrom("iso-8859-15");
10-
$inputCsv->setLimit(30);
10+
$inputCsv->setLimit(30); //we are limiting the convertion to the first 31 rows
1111
?>
1212
<!doctype html>
1313
<html lang="fr">

examples/writing.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44

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

7-
$writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created into a temporary File
8-
$writer->setDelimiter("\t"); //the delimiter will be the tab character
9-
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
10-
11-
$headers = ["position" , "team", "played", "goals difference", "points"];
12-
$writer->insertOne($headers);
7+
$header = ["position" , "team", "played", "goals difference", "points"];
138

14-
$teams = [
9+
$contents = [
1510
[1, "Chelsea", 26, 27, 57],
1611
[2, "Arsenal", 26, 22, 56],
17-
[3, "Manchester City", 25, 41, 54],
12+
[3, "Manchester City", 25, 41, 54,],
1813
[4, "Liverpool", 26, 34, 53],
1914
[5, "Tottenham", 26, 4, 50],
2015
[6, "Everton", 25, 11, 45],
2116
[7, "Manchester United", 26, 10, 42],
2217
];
2318

24-
$writer->insertAll($teams);
19+
$writer = Writer::createFromFileObject(new SplTempFileObject()); //the CSV file will be created using a temporary File
20+
$writer->setDelimiter("\t"); //the delimiter will be the tab character
21+
$writer->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
22+
$writer->setOutputBOM(Writer::BOM_UTF8); //adding the BOM sequence on output
23+
$writer->insertOne($header);
24+
$writer->insertAll($contents);
2525
?>
2626
<!doctype html>
2727
<html lang="fr">
2828
<head>
2929
<meta charset="utf-8">
30-
<title>Using the \League\Csv\Writer object</title>
30+
<title>Using the Writer class</title>
3131
<link rel="stylesheet" href="example.css">
3232
</head>
3333
<body>
34-
<h1>Example 4: Using Writer object</h1>
34+
<h1>Example 4: Using the Writer class</h1>
3535
<h3>The table representation of the csv</h3>
3636
<?=$writer->toHTML('table-csv-data with-header');?>
3737
<h3>The Raw CSV to be saved</h3>

examples/xml.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

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

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

0 commit comments

Comments
 (0)