Skip to content

Commit 952539b

Browse files
authored
Merge pull request #30 from SimonFrings/naming
Improve documentation and examples
2 parents a02acf1 + 852cb6a commit 952539b

7 files changed

+72
-91
lines changed

README.md

+35-35
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ such as a list of user records or log entries. CSV is not exactly a new format
1111
and has been used in a large number of systems for decades. In particular, CSV
1212
is often used for historical reasons and despite its shortcomings, it is still a
1313
very common export format for a large number of tools to interface with
14-
spreadsheet processors (such as Exel, Calc etc.). This library provides a simple
14+
spreadsheet processors (such as Excel, Calc etc.). This library provides a simple
1515
streaming API to process very large CSV files with thousands or even millions of
1616
rows efficiently without having to load the whole file into memory at once.
1717

@@ -40,7 +40,7 @@ rows efficiently without having to load the whole file into memory at once.
4040

4141
## Support us
4242

43-
We invest a lot of time developing, maintaining and updating our awesome
43+
We invest a lot of time developing, maintaining, and updating our awesome
4444
open-source projects. You can help us sustain this high-quality of our work by
4545
[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get
4646
numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue)
@@ -96,9 +96,9 @@ World!"
9696
started using some CSV-variant long before this standard was defined.
9797

9898
Some applications refer to CSV as Character-Separated Values, simply because
99-
using another delimiter (such as semicolon or tab) is a rather common approach
99+
using another delimiter (such as a semicolon or tab) is a rather common approach
100100
to avoid the need to enclose common values in quotes. This is particularly
101-
common for systems in Europe (and elsewhere) that use a comma as decimal separator.
101+
common for systems in Europe (and elsewhere) that use a comma as a decimal separator.
102102

103103
```
104104
name;comment
@@ -115,7 +115,7 @@ consistently.
115115

116116
Despite its shortcomings, CSV is widely used and this is unlikely to change any
117117
time soon. In particular, CSV is a very common export format for a lot of tools
118-
to interface with spreadsheet processors (such as Exel, Calc etc.). This means
118+
to interface with spreadsheet processors (such as Excel, Calc etc.). This means
119119
that CSV is often used for historical reasons and using CSV to store structured
120120
application data is usually not a good idea nowadays – but exporting to CSV for
121121
known applications continues to be a very reasonable approach.
@@ -155,12 +155,12 @@ test,1,24
155155
"hello world",2,48
156156
```
157157
```php
158-
$stdin = new ReadableResourceStream(STDIN);
158+
$stdin = new React\Stream\ReadableResourceStream(STDIN);
159159

160-
$stream = new Decoder($stdin);
160+
$csv = new Clue\React\Csv\Decoder($stdin);
161161

162-
$stream->on('data', function ($data) {
163-
// data is a parsed element from the CSV stream
162+
$csv->on('data', function (array $data) {
163+
// $data is a parsed element from the CSV stream
164164
// line 1: $data = array('test', '1', '24');
165165
// line 2: $data = array('hello world', '2', '48');
166166
var_dump($data);
@@ -179,9 +179,9 @@ use a quote enclosure character (`"`) and a backslash escape character (`\`).
179179
This behavior can be controlled through the optional constructor parameters:
180180

181181
```php
182-
$stream = new Decoder($stdin, ';');
182+
$csv = new Clue\React\Csv\Decoder($stdin, ';');
183183

184-
$stream->on('data', function ($data) {
184+
$csv->on('data', function (array $data) {
185185
// CSV fields will now be delimited by semicolon
186186
});
187187
```
@@ -193,15 +193,15 @@ unreasonably long lines. It accepts an additional argument if you want to change
193193
this from the default of 64 KiB:
194194

195195
```php
196-
$stream = new Decoder($stdin, ',', '"', '\\', 64 * 1024);
196+
$csv = new Clue\React\Csv\Decoder($stdin, ',', '"', '\\', 64 * 1024);
197197
```
198198

199199
If the underlying stream emits an `error` event or the plain stream contains
200200
any data that does not represent a valid CSV stream,
201201
it will emit an `error` event and then `close` the input stream:
202202

203203
```php
204-
$stream->on('error', function (Exception $error) {
204+
$csv->on('error', function (Exception $error) {
205205
// an error occured, stream will close next
206206
});
207207
```
@@ -212,7 +212,7 @@ followed by an `end` event on success or an `error` event for
212212
incomplete/invalid CSV data as above:
213213

214214
```php
215-
$stream->on('end', function () {
215+
$csv->on('end', function () {
216216
// stream successfully ended, stream will close next
217217
});
218218
```
@@ -221,7 +221,7 @@ If either the underlying stream or the `Decoder` is closed, it will forward
221221
the `close` event:
222222

223223
```php
224-
$stream->on('close', function () {
224+
$csv->on('close', function () {
225225
// stream closed
226226
// possibly after an "end" event or due to an "error" event
227227
});
@@ -231,7 +231,7 @@ The `close(): void` method can be used to explicitly close the `Decoder` and
231231
its underlying stream:
232232

233233
```php
234-
$stream->close();
234+
$csv->close();
235235
```
236236

237237
The `pipe(WritableStreamInterface $dest, array $options = array(): WritableStreamInterface`
@@ -240,7 +240,7 @@ Please note that the `Decoder` emits decoded/parsed data events, while many
240240
(most?) writable streams expect only data chunks:
241241

242242
```php
243-
$stream->pipe($logger);
243+
$csv->pipe($logger);
244244
```
245245

246246
For more details, see ReactPHP's
@@ -261,11 +261,11 @@ test,1
261261
"hello world",2
262262
```
263263
```php
264-
$stdin = new ReadableResourceStream(STDIN);
264+
$stdin = new React\Stream\ReadableResourceStream(STDIN);
265265

266-
$stream = new AssocDecoder($stdin);
266+
$csv = new Clue\React\Csv\AssocDecoder($stdin);
267267

268-
$stream->on('data', function ($data) {
268+
$csv->on('data', function (array $data) {
269269
// $data is a parsed element from the CSV stream
270270
// line 1: $data = array('name' => 'test', 'id' => '1');
271271
// line 2: $data = array('name' => 'hello world', 'id' => '2');
@@ -285,7 +285,7 @@ assoc arrays. After receiving the name of headers, this class will always emit
285285
a `headers` event with a list of header names.
286286

287287
```php
288-
$stream->on('headers', function (array $headers) {
288+
$csv->on('headers', function (array $headers) {
289289
// header line: $headers = array('name', 'id');
290290
var_dump($headers);
291291
});
@@ -312,12 +312,12 @@ and accepts its data through the same interface, but handles any data as complet
312312
CSV elements instead of just chunks of strings:
313313

314314
```php
315-
$stdout = new WritableResourceStream(STDOUT);
315+
$stdout = new React\Stream\WritableResourceStream(STDOUT);
316316

317-
$stream = new Encoder($stdout);
317+
$csv = new Clue\React\Csv\Encoder($stdout);
318318

319-
$stream->write(array('test', true, 24));
320-
$stream->write(array('hello world', 2, 48));
319+
$csv->write(array('test', true, 24));
320+
$csv->write(array('hello world', 2, 48));
321321
```
322322
```
323323
test,1,24
@@ -332,9 +332,9 @@ a Unix-style EOL (`\n` or `LF`).
332332
This behavior can be controlled through the optional constructor parameters:
333333

334334
```php
335-
$stream = new Encoder($stdout, ';');
335+
$csv = new Clue\React\Csv\Encoder($stdout, ';');
336336

337-
$stream->write(array('hello', 'world'));
337+
$csv->write(array('hello', 'world'));
338338
```
339339
```
340340
hello;world
@@ -345,7 +345,7 @@ any data that can not be represented as a valid CSV stream,
345345
it will emit an `error` event and then `close` the input stream:
346346

347347
```php
348-
$stream->on('error', function (Exception $error) {
348+
$csv->on('error', function (Exception $error) {
349349
// an error occured, stream will close next
350350
});
351351
```
@@ -354,7 +354,7 @@ If either the underlying stream or the `Encoder` is closed, it will forward
354354
the `close` event:
355355

356356
```php
357-
$stream->on('close', function () {
357+
$csv->on('close', function () {
358358
// stream closed
359359
// possibly after an "end" event or due to an "error" event
360360
});
@@ -364,22 +364,22 @@ The `end(mixed $data = null): void` method can be used to optionally emit
364364
any final data and then soft-close the `Encoder` and its underlying stream:
365365

366366
```php
367-
$stream->end();
367+
$csv->end();
368368
```
369369

370370
The `close(): void` method can be used to explicitly close the `Encoder` and
371371
its underlying stream:
372372

373373
```php
374-
$stream->close();
374+
$csv->close();
375375
```
376376

377377
For more details, see ReactPHP's
378378
[`WritableStreamInterface`](https://github.com/reactphp/stream#writablestreaminterface).
379379

380380
## Install
381381

382-
The recommended way to install this library is [through Composer](https://getcomposer.org).
382+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
383383
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
384384

385385
This project follows [SemVer](https://semver.org/).
@@ -394,12 +394,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
394394
This project aims to run on any platform and thus does not require any PHP
395395
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
396396
HHVM.
397-
It's *highly recommended to use the latest supported PHP version PHP 7+* for this project.
397+
It's *highly recommended to use the latest supported PHP version* for this project.
398398

399399
## Tests
400400

401401
To run the test suite, you first need to clone this repo and then install all
402-
dependencies [through Composer](https://getcomposer.org):
402+
dependencies [through Composer](https://getcomposer.org/):
403403

404404
```bash
405405
$ composer install
@@ -408,7 +408,7 @@ $ composer install
408408
To run the test suite, go to the project root and run:
409409

410410
```bash
411-
$ php vendor/bin/phpunit
411+
$ vendor/bin/phpunit
412412
```
413413

414414
## License

examples/01-count.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,28 @@
22

33
// $ php examples/01-count.php < examples/users.csv
44

5-
use Clue\React\Csv\AssocDecoder;
65
use React\EventLoop\Loop;
7-
use React\Stream\ReadableResourceStream;
8-
use React\Stream\WritableResourceStream;
96

107
require __DIR__ . '/../vendor/autoload.php';
118

129
$exit = 0;
13-
$in = new ReadableResourceStream(STDIN);
14-
$info = new WritableResourceStream(STDERR);
10+
$in = new React\Stream\ReadableResourceStream(STDIN);
11+
$info = new React\Stream\WritableResourceStream(STDERR);
1512

1613
$delimiter = isset($argv[1]) ? $argv[1] : ',';
1714

18-
$decoder = new AssocDecoder($in, $delimiter);
15+
$csv = new Clue\React\Csv\AssocDecoder($in, $delimiter);
1916

2017
$count = 0;
21-
$decoder->on('data', function () use (&$count) {
18+
$csv->on('data', function () use (&$count) {
2219
++$count;
2320
});
2421

25-
$decoder->on('end', function () use (&$count) {
22+
$csv->on('end', function () use (&$count) {
2623
echo $count . PHP_EOL;
2724
});
2825

29-
$decoder->on('error', function (Exception $e) use (&$count, &$exit, $info) {
26+
$csv->on('error', function (Exception $e) use (&$count, &$exit, $info) {
3027
$info->write('ERROR after record ' . $count . ': ' . $e->getMessage() . PHP_EOL);
3128
$exit = 1;
3229
});

examples/02-validate.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
// $ php examples/02-validate.php < examples/users.csv
44

5-
use Clue\React\Csv\Decoder;
6-
use Clue\React\Csv\Encoder;
75
use React\EventLoop\Loop;
8-
use React\Stream\ReadableResourceStream;
9-
use React\Stream\WritableResourceStream;
106

117
require __DIR__ . '/../vendor/autoload.php';
128

139
$exit = 0;
14-
$in = new ReadableResourceStream(STDIN);
15-
$out = new WritableResourceStream(STDOUT);
16-
$info = new WritableResourceStream(STDERR);
10+
$in = new React\Stream\ReadableResourceStream(STDIN);
11+
$out = new React\Stream\WritableResourceStream(STDOUT);
12+
$info = new React\Stream\WritableResourceStream(STDERR);
1713

1814
$delimiter = isset($argv[1]) ? $argv[1] : ',';
1915

20-
$decoder = new Decoder($in, $delimiter);
21-
$encoder = new Encoder($out, $delimiter);
22-
$decoder->pipe($encoder);
16+
$csv = new Clue\React\Csv\Decoder($in, $delimiter);
17+
$encoder = new Clue\React\Csv\Encoder($out, $delimiter);
18+
$csv->pipe($encoder);
2319

24-
$decoder->on('error', function (Exception $e) use ($info, &$exit) {
20+
$csv->on('error', function (Exception $e) use ($info, &$exit) {
2521
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
2622
$exit = 1;
2723
});

examples/11-csv2ndjson.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,30 @@
33
// $ php examples/11-csv2ndjson.php < examples/users.csv > examples/users.ndjson
44
// see also https://github.com/clue/reactphp-ndjson
55

6-
use Clue\React\Csv\AssocDecoder;
76
use React\EventLoop\Loop;
8-
use React\Stream\ReadableResourceStream;
9-
use React\Stream\WritableResourceStream;
10-
use React\Stream\ThroughStream;
117

128
require __DIR__ . '/../vendor/autoload.php';
139

1410
$exit = 0;
15-
$in = new ReadableResourceStream(STDIN);
16-
$out = new WritableResourceStream(STDOUT);
17-
$info = new WritableResourceStream(STDERR);
11+
$in = new React\Stream\ReadableResourceStream(STDIN);
12+
$out = new React\Stream\WritableResourceStream(STDOUT);
13+
$info = new React\Stream\WritableResourceStream(STDERR);
1814

1915
$delimiter = isset($argv[1]) ? $argv[1] : ',';
2016

21-
$decoder = new AssocDecoder($in, $delimiter);
17+
$csv = new Clue\React\Csv\AssocDecoder($in, $delimiter);
2218

23-
$encoder = new ThroughStream(function ($data) {
19+
$encoder = new React\Stream\ThroughStream(function ($data) {
2420
$data = \array_filter($data, function ($one) {
2521
return ($one !== '');
2622
});
2723

2824
return \json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
2925
});
3026

31-
$decoder->pipe($encoder)->pipe($out);
27+
$csv->pipe($encoder)->pipe($out);
3228

33-
$decoder->on('error', function (Exception $e) use ($info, &$exit) {
29+
$csv->on('error', function (Exception $e) use ($info, &$exit) {
3430
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
3531
$exit = 1;
3632
});

0 commit comments

Comments
 (0)