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