Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / consolidation / output-formatters / src / Transformations / TableTransformation.php
1 <?php
2 namespace Consolidation\OutputFormatters\Transformations;
3
4 use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
5 use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface;
6 use Consolidation\OutputFormatters\StructuredData\MetadataHolderInterface;
7 use Consolidation\OutputFormatters\Options\FormatterOptions;
8 use Consolidation\OutputFormatters\Formatters\TsvFormatter;
9 use Symfony\Component\Console\Output\BufferedOutput;
10
11 class TableTransformation extends \ArrayObject implements TableDataInterface, StringTransformationInterface, OriginalDataInterface
12 {
13     protected $headers;
14     protected $rowLabels;
15     protected $layout;
16     /** @var MetadataHolderInterface */
17     protected $originalData;
18
19     const TABLE_LAYOUT = 'table';
20     const LIST_LAYOUT = 'list';
21
22     public function __construct($data, $fieldLabels, $rowLabels = [])
23     {
24         $this->headers = $fieldLabels;
25         $this->rowLabels = $rowLabels;
26         $rows = static::transformRows($data, $fieldLabels);
27         $this->layout = self::TABLE_LAYOUT;
28         parent::__construct($rows);
29     }
30
31     public function setLayout($layout)
32     {
33         $this->layout = $layout;
34     }
35
36     public function getLayout()
37     {
38         return $this->layout;
39     }
40
41     public function isList()
42     {
43         return $this->layout == self::LIST_LAYOUT;
44     }
45
46     /**
47      * @inheritdoc
48      */
49     public function simplifyToString(FormatterOptions $options)
50     {
51         $alternateFormatter = new TsvFormatter();
52         $output = new BufferedOutput();
53
54         try {
55             $data = $alternateFormatter->validate($this->getArrayCopy());
56             $alternateFormatter->write($output, $this->getArrayCopy(), $options);
57         } catch (\Exception $e) {
58         }
59         return $output->fetch();
60     }
61
62     protected static function transformRows($data, $fieldLabels)
63     {
64         $rows = [];
65         foreach ($data as $rowid => $row) {
66             $rows[$rowid] = static::transformRow($row, $fieldLabels);
67         }
68         return $rows;
69     }
70
71     protected static function transformRow($row, $fieldLabels)
72     {
73         $result = [];
74         foreach ($fieldLabels as $key => $label) {
75             $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
76         }
77         return $result;
78     }
79
80     public function getHeaders()
81     {
82         return $this->headers;
83     }
84
85     public function getHeader($key)
86     {
87         if (array_key_exists($key, $this->headers)) {
88             return $this->headers[$key];
89         }
90         return $key;
91     }
92
93     public function getRowLabels()
94     {
95         return $this->rowLabels;
96     }
97
98     public function getRowLabel($rowid)
99     {
100         if (array_key_exists($rowid, $this->rowLabels)) {
101             return $this->rowLabels[$rowid];
102         }
103         return $rowid;
104     }
105
106     public function getOriginalData()
107     {
108         if (isset($this->originalData)) {
109             return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata());
110         }
111         return $this->getArrayCopy();
112     }
113
114     public function setOriginalData(MetadataHolderInterface $data)
115     {
116         $this->originalData = $data;
117     }
118
119     public function getTableData($includeRowKey = false)
120     {
121         $data = $this->getArrayCopy();
122         if ($this->isList()) {
123             $data = $this->convertTableToList();
124         }
125         if ($includeRowKey) {
126             $data = $this->getRowDataWithKey($data);
127         }
128         return $data;
129     }
130
131     protected function convertTableToList()
132     {
133         $result = [];
134         foreach ($this as $row) {
135             foreach ($row as $key => $value) {
136                 $result[$key][] = $value;
137             }
138         }
139         return $result;
140     }
141
142     protected function getRowDataWithKey($data)
143     {
144         $result = [];
145         $i = 0;
146         foreach ($data as $key => $row) {
147             array_unshift($row, $this->getHeader($key));
148             $i++;
149             $result[$key] = $row;
150         }
151         return $result;
152     }
153 }