Skip to content

Commit

Permalink
Add 'php' format to produce serialized output.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Apr 24, 2016
1 parent b1672aa commit 40f238c
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -18,6 +18,7 @@ public function __construct()
'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter',
'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter',
'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter',
'php' => '\Consolidation\OutputFormatters\Formatters\SerializeFormatter',
'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter',
'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter',
'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter',
Expand Down
16 changes: 16 additions & 0 deletions src/Formatters/SerializeFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Consolidation\OutputFormatters\Formatters;

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

class SerializeFormatter implements FormatterInterface
{
/**
* @inheritdoc
*/
public function write(OutputInterface $output, $data, $options = [])
{
$output->writeln(serialize($data));
}
}
13 changes: 13 additions & 0 deletions tests/testFormatters.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ function testSimpleJson()
$this->assertFormattedOutputMatches($expected, 'json', $data);
}

function testSerializeFormat()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];

$expected = 'a:3:{s:3:"one";s:1:"a";s:3:"two";s:1:"b";s:5:"three";s:1:"c";}';

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

function testNestedJson()
{
$data = [
Expand Down

0 comments on commit 40f238c

Please sign in to comment.