2 namespace Consolidation\OutputFormatters\StructuredData;
4 use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
5 use Consolidation\OutputFormatters\Options\FormatterOptions;
6 use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
7 use Consolidation\OutputFormatters\Transformations\ReorderFields;
8 use Consolidation\OutputFormatters\Transformations\TableTransformation;
11 * Holds an array where each element of the array is one row,
12 * and each row contains an associative array where the keys
13 * are the field names, and the values are the field data.
15 * It is presumed that every row contains the same keys.
17 abstract class AbstractStructuredList extends ListDataFromKeys implements RestructureInterface, RenderCellCollectionInterface
19 use RenderCellCollectionTrait;
21 public function __construct($data)
23 parent::__construct($data);
26 abstract public function restructure(FormatterOptions $options);
28 protected function createTableTransformation($data, $options)
30 $defaults = $this->defaultOptions();
31 $fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults);
33 $tableTransformer = $this->instantiateTableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
34 if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
35 $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
38 return $tableTransformer;
41 protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
43 return new TableTransformation($data, $fieldLabels, $rowLabels);
46 protected function getReorderedFieldLabels($data, $options, $defaults)
48 $reorderer = new ReorderFields();
49 $fieldLabels = $reorderer->reorder(
50 $this->getFields($options, $defaults),
51 $options->get(FormatterOptions::FIELD_LABELS, $defaults),
57 protected function getFields($options, $defaults)
59 $fieldShortcut = $options->get(FormatterOptions::FIELD);
60 if (!empty($fieldShortcut)) {
61 return [$fieldShortcut];
63 $result = $options->get(FormatterOptions::FIELDS, $defaults);
64 if (!empty($result)) {
67 return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults);
71 * A structured list may provide its own set of default options. These
72 * will be used in place of the command's default options (from the
73 * annotations) in instances where the user does not provide the options
74 * explicitly (on the commandline) or implicitly (via a configuration file).
78 protected function defaultOptions()
81 FormatterOptions::FIELDS => [],
82 FormatterOptions::FIELD_LABELS => [],
83 FormatterOptions::ROW_LABELS => [],
84 FormatterOptions::DEFAULT_FIELDS => [],