Skip to content

Commit

Permalink
Use full namespaces for documentation and minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 3, 2022
1 parent 725a6a7 commit 6f3c360
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 59 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ rows efficiently without having to load the whole file into memory at once.

## Support us

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

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

```
name;comment
Expand Down Expand Up @@ -155,11 +155,11 @@ test,1,24
"hello world",2,48
```
```php
$stdin = new ReadableResourceStream(STDIN);
$stdin = new React\Stream\ReadableResourceStream(STDIN);

$csv = new Decoder($stdin);
$csv = new Clue\React\Csv\Decoder($stdin);

$csv->on('data', function ($data) {
$csv->on('data', function (array $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');
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
$csv = new Decoder($stdin, ';');
$csv = new Clue\React\Csv\Decoder($stdin, ';');

$csv->on('data', function ($data) {
$csv->on('data', function (array $data) {
// CSV fields will now be delimited by semicolon
});
```
Expand All @@ -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
$csv = new Decoder($stdin, ',', '"', '\\', 64 * 1024);
$csv = new Clue\React\Csv\Decoder($stdin, ',', '"', '\\', 64 * 1024);
```

If the underlying stream emits an `error` event or the plain stream contains
Expand Down Expand Up @@ -261,11 +261,11 @@ test,1
"hello world",2
```
```php
$stdin = new ReadableResourceStream(STDIN);
$stdin = new React\Stream\ReadableResourceStream(STDIN);

$csv = new AssocDecoder($stdin);
$csv = new Clue\React\Csv\AssocDecoder($stdin);

$csv->on('data', function ($data) {
$csv->on('data', function (array $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 Down Expand Up @@ -312,9 +312,9 @@ and accepts its data through the same interface, but handles any data as complet
CSV elements instead of just chunks of strings:

```php
$stdout = new WritableResourceStream(STDOUT);
$stdout = new React\Stream\WritableResourceStream(STDOUT);

$csv = new Encoder($stdout);
$csv = new Clue\React\Csv\Encoder($stdout);

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

```php
$csv = new Encoder($stdout, ';');
$csv = new Clue\React\Csv\Encoder($stdout, ';');

$csv->write(array('hello', 'world'));
```
Expand Down Expand Up @@ -379,7 +379,7 @@ For more details, see ReactPHP's

## Install

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

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

## Tests

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

```bash
$ composer install
Expand All @@ -408,7 +408,7 @@ $ composer install
To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
$ vendor/bin/phpunit
```

## License
Expand Down
9 changes: 3 additions & 6 deletions examples/01-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

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

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

$exit = 0;
$in = new ReadableResourceStream(STDIN);
$info = new WritableResourceStream(STDERR);
$in = new React\Stream\ReadableResourceStream(STDIN);
$info = new React\Stream\WritableResourceStream(STDERR);

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

$csv = new AssocDecoder($in, $delimiter);
$csv = new Clue\React\Csv\AssocDecoder($in, $delimiter);

$count = 0;
$csv->on('data', function () use (&$count) {
Expand Down
14 changes: 5 additions & 9 deletions examples/02-validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

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

use Clue\React\Csv\Decoder;
use Clue\React\Csv\Encoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

$exit = 0;
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);
$info = new React\Stream\WritableResourceStream(STDERR);

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

$csv = new Decoder($in, $delimiter);
$encoder = new Encoder($out, $delimiter);
$csv = new Clue\React\Csv\Decoder($in, $delimiter);
$encoder = new Clue\React\Csv\Encoder($out, $delimiter);
$csv->pipe($encoder);

$csv->on('error', function (Exception $e) use ($info, &$exit) {
Expand Down
14 changes: 5 additions & 9 deletions examples/11-csv2ndjson.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
// $ php examples/11-csv2ndjson.php < examples/users.csv > examples/users.ndjson
// see also https://github.com/clue/reactphp-ndjson

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use React\Stream\ThroughStream;

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

$exit = 0;
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);
$info = new React\Stream\WritableResourceStream(STDERR);

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

$csv = new AssocDecoder($in, $delimiter);
$csv = new Clue\React\Csv\AssocDecoder($in, $delimiter);

$encoder = new ThroughStream(function ($data) {
$encoder = new React\Stream\ThroughStream(function ($data) {
$data = \array_filter($data, function ($one) {
return ($one !== '');
});
Expand Down
14 changes: 5 additions & 9 deletions examples/12-csv2tsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
// $ php examples/12-csv2tsv.php < examples/users.csv > examples/users.tsv
// see also https://github.com/clue/reactphp-tsv

use Clue\React\Csv\Decoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use React\Stream\ThroughStream;

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

$exit = 0;
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);
$info = new React\Stream\WritableResourceStream(STDERR);

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

$csv = new Decoder($in, $delimiter);
$csv = new Clue\React\Csv\Decoder($in, $delimiter);

$encoder = new ThroughStream(function ($data) {
$encoder = new React\Stream\ThroughStream(function ($data) {
$data = \array_map(function ($value) {
return \addcslashes($value, "\0..\37");
}, $data);
Expand Down
4 changes: 1 addition & 3 deletions examples/91-benchmark-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
// 2) pipe CSV into benchmark script:
// $ php examples/91-benchmark-count.php < IRAhandle_tweets_1.csv

use Clue\React\Csv\AssocDecoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;

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

if (extension_loaded('xdebug')) {
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$csv = new AssocDecoder(new ReadableResourceStream(STDIN));
$csv = new Clue\React\Csv\AssocDecoder(new React\Stream\ReadableResourceStream(STDIN));

$count = 0;
$csv->on('data', function () use (&$count) {
Expand Down
6 changes: 2 additions & 4 deletions examples/92-benchmark-count-gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
// 3) pipe compressed CSV into benchmark script:
// $ php examples/92-benchmark-count-gzip.php < IRAhandle_tweets_1.csv.gz

use Clue\React\Csv\AssocDecoder;
use React\ChildProcess\Process;
use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -25,13 +23,13 @@
// is preferred here. If the input source is slower (such as an HTTP download)
// or if `gunzip` is not available (Windows), using a built-in decompressor
// such as https://github.com/clue/reactphp-zlib would be preferable.
$process = new Process('exec gunzip', null, null, array(
$process = new React\ChildProcess\Process('exec gunzip', null, null, array(
0 => STDIN,
1 => array('pipe', 'w'),
STDERR
));
$process->start();
$csv = new AssocDecoder($process->stdout);
$csv = new Clue\React\Csv\AssocDecoder($process->stdout);

$count = 0;
$csv->on('data', function () use (&$count) {
Expand Down

0 comments on commit 6f3c360

Please sign in to comment.