summaryrefslogtreecommitdiffstats
path: root/examples/stream.php
blob: 2fe8d91700c143f326fefb629359f138716021de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

header('Content-type: text/html; charset=utf-8');

use League\Csv\Reader;
use League\Csv\Writer;
use lib\FilterTranscode;

require '../vendor/autoload.php';  //load all the necessary classes when using composer install in dev mode

//you must register your class for it to be usable by the CSV Lib
stream_filter_register(FilterTranscode::FILTER_NAME."*", "\lib\FilterTranscode");
?>
<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>League\Csv Stream Filter API example</title>
    <link rel="stylesheet" href="example.css">
</head>
<body>

<p>Stream Filters can only be used if the <code><strong>isActiveStreamFilter</strong></code> method returns <code><strong>true</strong></code></p>
<h3>Using the Reader class</h3>

<pre><code>$reader = Reader::createFromPath(__DIR__.'/data/prenoms.csv');
if ($reader->isActiveStreamFilter()) {
    $reader->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8");
    $reader->appendStreamFilter('string.toupper');
    $reader->appendStreamFilter('string.rot13');
}
$reader->setDelimiter(';');
$reader->setOffset(6);
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);
</code></pre>

<p>the data is :</p>
<ol>
<li>transcoded by the Stream Filter from ISO-8859-1 to UTF-8</li>
<li>uppercased</li>
<li>rot13 transformed</li>
</ol>
<?php

//BETWEEN fetch* call you CAN update/remove/add stream filter
$reader = Reader::createFromPath(__DIR__.'/data/prenoms.csv');
if ($reader->isActiveStreamFilter()) {
    $reader->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8");
    $reader->appendStreamFilter('string.toupper');
    $reader->appendStreamFilter('string.rot13');
}
$reader->setDelimiter(';');
$reader->setOffset(6);
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

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()) {
    $reader->removeStreamFilter('string.toupper');
}
$reader->setOffset(6);
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

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

<?php
if ($reader->isActiveStreamFilter()) {
    $reader->removeStreamFilter('string.toupper');
}
$reader->setOffset(6);
$reader->setLimit(3);
$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']);

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

<p><strong>You can not add/remove/update stream filters between inserts calls</strong>
<pre><code>$writer = Writer::createFromPath('/tmp/test.csv', 'w');
if ($writer->isActiveStreamFilter()) {
    $writer->appendStreamFilter('string.toupper');
}
$writer->insertOne('je,suis,toto,le,héros');
</code></pre>
<?php
$writer = Writer::createFromPath('/tmp/test.csv', 'w');
if ($writer->isActiveStreamFilter()) {
    $writer->appendStreamFilter('string.toupper');
}
$writer->insertOne('je,suis,toto,le,héros');
?>
<p>When the first insert call is done... the stream filter status is
frozen and can no longer be updated !! Any added row will be uppercased only no matter what.</p>
<?php
if ($writer->isActiveStreamFilter()) {
    $writer->appendStreamFilter('string.rot13');
    $writer->removeStreamFilter('string.toupper');
}
$writer->insertOne('je,suis,toto,le,héros');
?>

<p>To update the filters you need to:</p>
<ol>
<li> create a new Writer object <em>don't forget to update the <code>$open_mode</code></em></li>
<li>apply the new stream filters</li>
</ol>

<pre><code>$writer = $writer->newWriter('a+');
if ($writer->isActiveStreamFilter()) {
    $writer->appendStreamFilter('string.rot13');
    $writer->prependStreamFilter('string.strip_tags');
}
$writer->insertAll([
    'je,suis,toto,le,héros',
    'je,&lt;strong&gt;suis&lt;/strong&gt;,toto,le,héros'
]);
echo $writer->newReader()->toHTML();
</code></pre>

<?php
$writer = $writer->newWriter('a+');
if ($writer->isActiveStreamFilter()) {
    $writer->appendStreamFilter('string.rot13');
    $writer->prependStreamFilter('string.strip_tags');
}
$writer->insertAll([
    'je,suis,toto,le,héros',
    'je,<strong>suis</strong>,toto,le,héros',
]);

echo $writer->newReader()->toHTML(), PHP_EOL;
?>

</body>
</html>