Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SectionsFormatter to implement --sections data format. #17

Merged
merged 4 commits into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/FormatterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct()
'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter',
'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter',
'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter',
'sections' => '\Consolidation\OutputFormatters\Formatters\SectionsFormatter',
];

// Make the empty format an alias for the 'string' formatter.
Expand Down
62 changes: 62 additions & 0 deletions src/Formatters/SectionsFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace Consolidation\OutputFormatters\Formatters;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

use Consolidation\OutputFormatters\FormatterInterface;
use Consolidation\OutputFormatters\ConfigureInterface;
use Consolidation\OutputFormatters\ValidationInterface;
use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
use Consolidation\OutputFormatters\Transformations\ReorderFields;
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
use Consolidation\OutputFormatters\StructuredData\AssociativeList;

/**
* Display sections of data.
*
* This formatter takes data in the RowsOfFields data type.
* Each row represents one section; the data in each section
* is rendered in two columns, with the key in the first column
* and the value in the second column.
*/
class SectionsFormatter implements FormatterInterface, ValidationInterface, RenderDataInterface
{
use RenderTableDataTrait;

/**
* @inheritdoc
*/
public function validate($structuredData)
{
// If the provided data was of class RowsOfFields
// or AssociativeList, it will be converted into
// a TableTransformation object by the restructure call.
if (!$structuredData instanceof TableDataInterface) {
throw new IncompatibleDataException(
$this,
$structuredData,
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields')
);
}
return $structuredData;
}

/**
* @inheritdoc
*/
public function write(OutputInterface $output, $tableTransformer, $options = [])
{
$table = new Table($output);
$table->setStyle('compact');
foreach ($tableTransformer as $rowid => $row) {
$rowLabel = $tableTransformer->getRowLabel($rowid);
$output->writeln('');
$output->writeln($rowLabel); // TODO: convert to a label
$sectionData = new AssociativeList($row);
$sectionTableTransformer = $sectionData->restructure([], $options);
$table->setRows($sectionTableTransformer->getTableData(true));
$table->render();
}
}
}
4 changes: 3 additions & 1 deletion src/StructuredData/RowsOfFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function createTableTransformation($data, $configurationData, $options
$reorderer = new ReorderFields();
$fieldLabels = $reorderer->reorder($options['fields'], $options['field-labels'], $data);

$tableTransformer = new TableTransformation($data, $fieldLabels);
$tableTransformer = new TableTransformation($data, $fieldLabels, $options['row-labels']);

return $tableTransformer;
}
Expand All @@ -51,6 +51,7 @@ protected function interpretOptions($configurationData, $options)
$configurationData += $this->defaultOptions();

$configurationData['field-labels'] = PropertyParser::parse($configurationData['field-labels']);
$configurationData['row-labels'] = PropertyParser::parse($configurationData['row-labels']);
$configurationData['default-fields'] = PropertyParser::parse($configurationData['default-fields']);

return $options + $configurationData;
Expand All @@ -61,6 +62,7 @@ protected function defaultOptions()
return [
'fields' => [],
'field-labels' => [],
'row-labels' => [],
'default-fields' => [],
];
}
Expand Down
21 changes: 18 additions & 3 deletions src/Transformations/TableTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
class TableTransformation extends \ArrayObject implements TableDataInterface
{
protected $headers;
protected $rowLabels;
protected $layout;

const TABLE_LAYOUT = 'table';
const LIST_LAYOUT = 'list';

public function __construct($data, $fieldLabels)
public function __construct($data, $fieldLabels, $rowLabels = [])
{
$this->headers = $fieldLabels;
$this->rowLabels = $rowLabels;
$rows = static::transformRows($data, $fieldLabels);
$this->layout = self::TABLE_LAYOUT;
parent::__construct($rows);
Expand All @@ -37,8 +39,8 @@ public function isList()
protected static function transformRows($data, $fieldLabels)
{
$rows = [];
foreach ($data as $row) {
$rows[] = static::transformRow($row, $fieldLabels);
foreach ($data as $rowid => $row) {
$rows[$rowid] = static::transformRow($row, $fieldLabels);
}
return $rows;
}
Expand All @@ -65,6 +67,19 @@ public function getHeader($key)
return $key;
}

public function getRowLabels()
{
return $this->rowLabels;
}

public function getRowLabel($rowid)
{
if (array_key_exists($rowid, $this->rowLabels)) {
return $this->rowLabels[$rowid];
}
return $rowid;
}

public function getData()
{
return $this->getArrayCopy();
Expand Down
Loading