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 \ArrayObject implements RestructureInterface, ListDataInterface, RenderCellCollectionInterface
19 use RenderCellCollectionTrait;
22 public function __construct($data)
24 parent::__construct($data);
27 abstract public function restructure(FormatterOptions $options);
29 abstract public function getListData(FormatterOptions $options);
31 protected function createTableTransformation($data, $options)
33 $defaults = $this->defaultOptions();
34 $fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults);
36 $tableTransformer = $this->instantiateTableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
37 if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
38 $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
41 return $tableTransformer;
44 protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
46 return new TableTransformation($data, $fieldLabels, $rowLabels);
49 protected function getReorderedFieldLabels($data, $options, $defaults)
51 $reorderer = new ReorderFields();
52 $fieldLabels = $reorderer->reorder(
53 $this->getFields($options, $defaults),
54 $options->get(FormatterOptions::FIELD_LABELS, $defaults),
60 protected function getFields($options, $defaults)
62 $fieldShortcut = $options->get(FormatterOptions::FIELD);
63 if (!empty($fieldShortcut)) {
64 return [$fieldShortcut];
66 $result = $options->get(FormatterOptions::FIELDS, $defaults);
67 if (!empty($result)) {
70 return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults);
74 * A structured list may provide its own set of default options. These
75 * will be used in place of the command's default options (from the
76 * annotations) in instances where the user does not provide the options
77 * explicitly (on the commandline) or implicitly (via a configuration file).
81 protected function defaultOptions()
84 FormatterOptions::FIELDS => [],
85 FormatterOptions::FIELD_LABELS => [],
86 FormatterOptions::ROW_LABELS => [],
87 FormatterOptions::DEFAULT_FIELDS => [],