52e9403b170606a0c898cc87460d0f3633532582
[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
8 class TableTransformation extends \ArrayObject implements TableDataInterface, OriginalDataInterface
9 {
10     protected $headers;
11     protected $rowLabels;
12     protected $layout;
13     /** @var MetadataHolderInterface */
14     protected $originalData;
15
16     const TABLE_LAYOUT = 'table';
17     const LIST_LAYOUT = 'list';
18
19     public function __construct($data, $fieldLabels, $rowLabels = [])
20     {
21         $this->headers = $fieldLabels;
22         $this->rowLabels = $rowLabels;
23         $rows = static::transformRows($data, $fieldLabels);
24         $this->layout = self::TABLE_LAYOUT;
25         parent::__construct($rows);
26     }
27
28     public function setLayout($layout)
29     {
30         $this->layout = $layout;
31     }
32
33     public function getLayout()
34     {
35         return $this->layout;
36     }
37
38     public function isList()
39     {
40         return $this->layout == self::LIST_LAYOUT;
41     }
42
43     protected static function transformRows($data, $fieldLabels)
44     {
45         $rows = [];
46         foreach ($data as $rowid => $row) {
47             $rows[$rowid] = static::transformRow($row, $fieldLabels);
48         }
49         return $rows;
50     }
51
52     protected static function transformRow($row, $fieldLabels)
53     {
54         $result = [];
55         foreach ($fieldLabels as $key => $label) {
56             $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
57         }
58         return $result;
59     }
60
61     public function getHeaders()
62     {
63         return $this->headers;
64     }
65
66     public function getHeader($key)
67     {
68         if (array_key_exists($key, $this->headers)) {
69             return $this->headers[$key];
70         }
71         return $key;
72     }
73
74     public function getRowLabels()
75     {
76         return $this->rowLabels;
77     }
78
79     public function getRowLabel($rowid)
80     {
81         if (array_key_exists($rowid, $this->rowLabels)) {
82             return $this->rowLabels[$rowid];
83         }
84         return $rowid;
85     }
86
87     public function getOriginalData()
88     {
89         if (isset($this->originalData)) {
90             return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata());
91         }
92         return $this->getArrayCopy();
93     }
94
95     public function setOriginalData(MetadataHolderInterface $data)
96     {
97         $this->originalData = $data;
98     }
99
100     public function getTableData($includeRowKey = false)
101     {
102         $data = $this->getArrayCopy();
103         if ($this->isList()) {
104             $data = $this->convertTableToList();
105         }
106         if ($includeRowKey) {
107             $data = $this->getRowDataWithKey($data);
108         }
109         return $data;
110     }
111
112     protected function convertTableToList()
113     {
114         $result = [];
115         foreach ($this as $row) {
116             foreach ($row as $key => $value) {
117                 $result[$key][] = $value;
118             }
119         }
120         return $result;
121     }
122
123     protected function getRowDataWithKey($data)
124     {
125         $result = [];
126         $i = 0;
127         foreach ($data as $key => $row) {
128             array_unshift($row, $this->getHeader($key));
129             $i++;
130             $result[$key] = $row;
131         }
132         return $result;
133     }
134 }