From 725a6a750fc6d72f6157665bbe43deef4b75b08e Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 28 Apr 2022 12:40:04 +0200 Subject: [PATCH] Rename `$stream` to `$csv` in documentation --- README.md | 46 ++++++++++++++-------------- examples/01-count.php | 8 ++--- examples/02-validate.php | 6 ++-- examples/11-csv2ndjson.php | 6 ++-- examples/12-csv2tsv.php | 12 ++++---- examples/91-benchmark-count.php | 6 ++-- examples/92-benchmark-count-gzip.php | 6 ++-- 7 files changed, 45 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index f492dcb..948547b 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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 }); ``` @@ -193,7 +193,7 @@ 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 @@ -201,7 +201,7 @@ 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 }); ``` @@ -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 }); ``` @@ -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 }); @@ -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` @@ -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 @@ -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'); @@ -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); }); @@ -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 @@ -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 @@ -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 }); ``` @@ -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 }); @@ -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 diff --git a/examples/01-count.php b/examples/01-count.php index 0f84cde..54c593b 100644 --- a/examples/01-count.php +++ b/examples/01-count.php @@ -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; }); diff --git a/examples/02-validate.php b/examples/02-validate.php index c65a167..1dda1ad 100644 --- a/examples/02-validate.php +++ b/examples/02-validate.php @@ -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; }); diff --git a/examples/11-csv2ndjson.php b/examples/11-csv2ndjson.php index 75f5030..fdeaf56 100644 --- a/examples/11-csv2ndjson.php +++ b/examples/11-csv2ndjson.php @@ -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) { @@ -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; }); diff --git a/examples/12-csv2tsv.php b/examples/12-csv2tsv.php index 0cec58b..19ef842 100644 --- a/examples/12-csv2tsv.php +++ b/examples/12-csv2tsv.php @@ -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) { @@ -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); diff --git a/examples/91-benchmark-count.php b/examples/91-benchmark-count.php index e956c9b..aea8696 100644 --- a/examples/91-benchmark-count.php +++ b/examples/91-benchmark-count.php @@ -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; }); @@ -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); diff --git a/examples/92-benchmark-count-gzip.php b/examples/92-benchmark-count-gzip.php index e7d9def..2e95128 100644 --- a/examples/92-benchmark-count-gzip.php +++ b/examples/92-benchmark-count-gzip.php @@ -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; }); @@ -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);