Skip to content

Commit

Permalink
Rename $stream to $csv in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 3, 2022
1 parent a02acf1 commit 725a6a7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ test,1,24
```php
$stdin = new ReadableResourceStream(STDIN);

$stream = new Decoder($stdin);
$csv = new Decoder($stdin);

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

```php
$stream = new Decoder($stdin, ';');
$csv = new Decoder($stdin, ';');

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

```php
$stream = new Decoder($stdin, ',', '"', '\\', 64 * 1024);
$csv = new Decoder($stdin, ',', '"', '\\', 64 * 1024);
```

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

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

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

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

```php
$stream->close();
$csv->close();
```

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

```php
$stream->pipe($logger);
$csv->pipe($logger);
```

For more details, see ReactPHP's
Expand All @@ -263,9 +263,9 @@ test,1
```php
$stdin = new ReadableResourceStream(STDIN);

$stream = new AssocDecoder($stdin);
$csv = new AssocDecoder($stdin);

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

```php
$stream->on('headers', function (array $headers) {
$csv->on('headers', function (array $headers) {
// header line: $headers = array('name', 'id');
var_dump($headers);
});
Expand Down Expand Up @@ -314,10 +314,10 @@ CSV elements instead of just chunks of strings:
```php
$stdout = new WritableResourceStream(STDOUT);

$stream = new Encoder($stdout);
$csv = new Encoder($stdout);

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

```php
$stream = new Encoder($stdout, ';');
$csv = new Encoder($stdout, ';');

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

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

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

```php
$stream->end();
$csv->end();
```

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

```php
$stream->close();
$csv->close();
```

For more details, see ReactPHP's
Expand Down
8 changes: 4 additions & 4 deletions examples/01-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

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

$decoder = new AssocDecoder($in, $delimiter);
$csv = new AssocDecoder($in, $delimiter);

$count = 0;
$decoder->on('data', function () use (&$count) {
$csv->on('data', function () use (&$count) {
++$count;
});

$decoder->on('end', function () use (&$count) {
$csv->on('end', function () use (&$count) {
echo $count . PHP_EOL;
});

$decoder->on('error', function (Exception $e) use (&$count, &$exit, $info) {
$csv->on('error', function (Exception $e) use (&$count, &$exit, $info) {
$info->write('ERROR after record ' . $count . ': ' . $e->getMessage() . PHP_EOL);
$exit = 1;
});
Expand Down
6 changes: 3 additions & 3 deletions examples/02-validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

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

$decoder = new Decoder($in, $delimiter);
$csv = new Decoder($in, $delimiter);
$encoder = new Encoder($out, $delimiter);
$decoder->pipe($encoder);
$csv->pipe($encoder);

$decoder->on('error', function (Exception $e) use ($info, &$exit) {
$csv->on('error', function (Exception $e) use ($info, &$exit) {
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
$exit = 1;
});
Expand Down
6 changes: 3 additions & 3 deletions examples/11-csv2ndjson.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

$decoder = new AssocDecoder($in, $delimiter);
$csv = new AssocDecoder($in, $delimiter);

$encoder = new ThroughStream(function ($data) {
$data = \array_filter($data, function ($one) {
Expand All @@ -28,9 +28,9 @@
return \json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
});

$decoder->pipe($encoder)->pipe($out);
$csv->pipe($encoder)->pipe($out);

$decoder->on('error', function (Exception $e) use ($info, &$exit) {
$csv->on('error', function (Exception $e) use ($info, &$exit) {
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
$exit = 1;
});
Expand Down
12 changes: 6 additions & 6 deletions examples/12-csv2tsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

$decoder = new Decoder($in, $delimiter);
$csv = new Decoder($in, $delimiter);

$encoder = new ThroughStream(function ($data) {
$data = \array_map(function ($value) {
Expand All @@ -28,20 +28,20 @@
return \implode("\t", $data) . "\n";
});

$decoder->pipe($encoder)->pipe($out);
$csv->pipe($encoder)->pipe($out);

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

// TSV files MUST include a header line, so complain if CSV input ends without a single line
$decoder->on('end', $empty = function () use ($info, &$exit) {
$csv->on('end', $empty = function () use ($info, &$exit) {
$info->write('ERROR: Empty CSV input' . PHP_EOL);
$exit = 1;
});
$decoder->once('data', function () use ($decoder, $empty) {
$decoder->removeListener('end', $empty);
$csv->once('data', function () use ($csv, $empty) {
$csv->removeListener('end', $empty);
});

$info->write('You can pipe/write a valid CSV stream to STDIN' . PHP_EOL);
Expand Down
6 changes: 3 additions & 3 deletions examples/91-benchmark-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$decoder = new AssocDecoder(new ReadableResourceStream(STDIN));
$csv = new AssocDecoder(new ReadableResourceStream(STDIN));

$count = 0;
$decoder->on('data', function () use (&$count) {
$csv->on('data', function () use (&$count) {
++$count;
});

Expand All @@ -32,7 +32,7 @@
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
});

$decoder->on('close', function () use (&$count, $report, $start) {
$csv->on('close', function () use (&$count, $report, $start) {
$now = microtime(true);
Loop::cancelTimer($report);

Expand Down
6 changes: 3 additions & 3 deletions examples/92-benchmark-count-gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
STDERR
));
$process->start();
$decoder = new AssocDecoder($process->stdout);
$csv = new AssocDecoder($process->stdout);

$count = 0;
$decoder->on('data', function () use (&$count) {
$csv->on('data', function () use (&$count) {
++$count;
});

Expand All @@ -43,7 +43,7 @@
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
});

$decoder->on('close', function () use (&$count, $report, $start) {
$csv->on('close', function () use (&$count, $report, $start) {
$now = microtime(true);
Loop::cancelTimer($report);

Expand Down

0 comments on commit 725a6a7

Please sign in to comment.