-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from clue-labs/tsv
Add reference to TSV (Tab-Separated Values)
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
// $ 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\Factory; | ||
use React\Stream\ReadableResourceStream; | ||
use React\Stream\WritableResourceStream; | ||
use React\Stream\ThroughStream; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loop = Factory::create(); | ||
|
||
$exit = 0; | ||
$in = new ReadableResourceStream(STDIN, $loop); | ||
$out = new WritableResourceStream(STDOUT, $loop); | ||
$info = new WritableResourceStream(STDERR, $loop); | ||
|
||
$delimiter = isset($argv[1]) ? $argv[1] : ','; | ||
|
||
$decoder = new Decoder($in, $delimiter); | ||
|
||
$encoder = new ThroughStream(function ($data) { | ||
$data = \array_map(function ($value) { | ||
return \addcslashes($value, "\0..\37"); | ||
}, $data); | ||
|
||
return \implode("\t", $data) . "\n"; | ||
}); | ||
|
||
$decoder->pipe($encoder)->pipe($out); | ||
|
||
$decoder->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) { | ||
$info->write('ERROR: Empty CSV input' . PHP_EOL); | ||
$exit = 1; | ||
}); | ||
$decoder->once('data', function () use ($decoder, $empty) { | ||
$decoder->removeListener('end', $empty); | ||
}); | ||
|
||
$info->write('You can pipe/write a valid CSV stream to STDIN' . PHP_EOL); | ||
$info->write('Valid TSV (Tab-Separated Values) will be forwarded to STDOUT' . PHP_EOL); | ||
$info->write('Invalid CSV will raise an error on STDERR and exit with code 1' . PHP_EOL); | ||
|
||
$loop->run(); | ||
|
||
exit($exit); |