fb1bc5375ff23d035d2b85a9264278880294a204
[yaffs-website] / vendor / consolidation / output-formatters / src / StructuredData / AbstractStructuredList.php
1 <?php
2 namespace Consolidation\OutputFormatters\StructuredData;
3
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;
9
10 /**
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.
14  *
15  * It is presumed that every row contains the same keys.
16  */
17 abstract class AbstractStructuredList extends \ArrayObject implements RestructureInterface, ListDataInterface, RenderCellCollectionInterface
18 {
19     use RenderCellCollectionTrait;
20     protected $data;
21
22     public function __construct($data)
23     {
24         parent::__construct($data);
25     }
26
27     abstract public function restructure(FormatterOptions $options);
28
29     abstract public function getListData(FormatterOptions $options);
30
31     protected function createTableTransformation($data, $options)
32     {
33         $defaults = $this->defaultOptions();
34         $fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults);
35
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);
39         }
40
41         return $tableTransformer;
42     }
43
44     protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
45     {
46         return new TableTransformation($data, $fieldLabels, $rowLabels);
47     }
48
49     protected function getReorderedFieldLabels($data, $options, $defaults)
50     {
51         $reorderer = new ReorderFields();
52         $fieldLabels = $reorderer->reorder(
53             $this->getFields($options, $defaults),
54             $options->get(FormatterOptions::FIELD_LABELS, $defaults),
55             $data
56         );
57         return $fieldLabels;
58     }
59
60     protected function getFields($options, $defaults)
61     {
62         $fieldShortcut = $options->get(FormatterOptions::FIELD);
63         if (!empty($fieldShortcut)) {
64             return [$fieldShortcut];
65         }
66         $result = $options->get(FormatterOptions::FIELDS, $defaults);
67         if (!empty($result)) {
68             return $result;
69         }
70         return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults);
71     }
72
73     /**
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).
78      *
79      * @return array
80      */
81     protected function defaultOptions()
82     {
83         return [
84             FormatterOptions::FIELDS => [],
85             FormatterOptions::FIELD_LABELS => [],
86             FormatterOptions::ROW_LABELS => [],
87             FormatterOptions::DEFAULT_FIELDS => [],
88         ];
89     }
90 }