Skip to content

Commit

Permalink
Add csv formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Apr 4, 2016
1 parent de23ee3 commit 0eabbb5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FormatterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct()
'print-r' => \Consolidation\OutputFormatters\Formatters\PrintRFormatter::class,
'var_export' => \Consolidation\OutputFormatters\Formatters\VarExportFormatter::class,
'list' => \Consolidation\OutputFormatters\Formatters\ListFormatter::class,
'csv' => \Consolidation\OutputFormatters\Formatters\CsvFormatter::class,
'table' => \Consolidation\OutputFormatters\Formatters\TableFormatter::class,
];
}
Expand Down
45 changes: 45 additions & 0 deletions src/Formatters/CsvFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace Consolidation\OutputFormatters\Formatters;

use Consolidation\OutputFormatters\FormatterInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CsvFormatter implements FormatterInterface
{
/**
* @inheritdoc
*/
public function write($data, $options, OutputInterface $output)
{
if (empty($data) || !is_array($data)) {
return;
}
if (!is_array($data[0])) {
$this->writeCsvLine($data, $options, $output);
return;
}
$this->writeCsvLines($data, $options, $output);
}

public function writeCsvLines($data, $options, OutputInterface $output)
{
foreach ($data as $line) {
$this->writeCsvLine($line, $options, $output);
}
}

public function writeCsvLine($data, $options, OutputInterface $output)
{
$output->write($this->csvEscape($data));
}

protected function csvEscape($data, $delimiter = ',')
{
$buffer = fopen('php://temp', 'r+');
fputcsv($buffer, $data, $delimiter);
rewind($buffer);
$csv = fgets($buffer);
fclose($buffer);
return $csv;
}
}
54 changes: 54 additions & 0 deletions tests/testFormatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,60 @@ function testList()
$this->assertFormattedOutputMatches($expected, 'list', $data);
}

function testSimpleCsv()
{
$data = ['a', 'b', 'c'];
$expected = "a,b,c";

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testLinesOfCsv()
{
$data = [['a', 'b', 'c'], ['x', 'y', 'z']];
$expected = "a,b,c\nx,y,z";

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testCsvWithEscapedValues()
{
$data = ["Red apple", "Yellow lemon"];
$expected = '"Red apple","Yellow lemon"';

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testCsvWithEmbeddedSingleQuote()
{
$data = ["John's book", "Mary's laptop"];
$expected = <<<EOT
"John's book","Mary's laptop"
EOT;

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testCsvWithEmbeddedDoubleQuote()
{
$data = ['The "best" solution'];
$expected = <<<EOT
"The ""best"" solution"
EOT;

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testCsvBothKindsOfQuotes()
{
$data = ["John's \"new\" book", "Mary's \"modified\" laptop"];
$expected = <<<EOT
"John's ""new"" book","Mary's ""modified"" laptop"
EOT;

$this->assertFormattedOutputMatches($expected, 'csv', $data);
}

function testSimpleTable()
{
$data = [
Expand Down

0 comments on commit 0eabbb5

Please sign in to comment.