2 namespace Consolidation\OutputFormatters\Transformations;
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;
11 class TableTransformation extends \ArrayObject implements TableDataInterface, StringTransformationInterface, OriginalDataInterface
16 /** @var MetadataHolderInterface */
17 protected $originalData;
19 const TABLE_LAYOUT = 'table';
20 const LIST_LAYOUT = 'list';
22 public function __construct($data, $fieldLabels, $rowLabels = [])
24 $this->headers = $fieldLabels;
25 $this->rowLabels = $rowLabels;
26 $rows = static::transformRows($data, $fieldLabels);
27 $this->layout = self::TABLE_LAYOUT;
28 parent::__construct($rows);
31 public function setLayout($layout)
33 $this->layout = $layout;
36 public function getLayout()
41 public function isList()
43 return $this->layout == self::LIST_LAYOUT;
49 public function simplifyToString(FormatterOptions $options)
51 $alternateFormatter = new TsvFormatter();
52 $output = new BufferedOutput();
55 $data = $alternateFormatter->validate($this->getArrayCopy());
56 $alternateFormatter->write($output, $this->getArrayCopy(), $options);
57 } catch (\Exception $e) {
59 return $output->fetch();
62 protected static function transformRows($data, $fieldLabels)
65 foreach ($data as $rowid => $row) {
66 $rows[$rowid] = static::transformRow($row, $fieldLabels);
71 protected static function transformRow($row, $fieldLabels)
74 foreach ($fieldLabels as $key => $label) {
75 $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
80 public function getHeaders()
82 return $this->headers;
85 public function getHeader($key)
87 if (array_key_exists($key, $this->headers)) {
88 return $this->headers[$key];
93 public function getRowLabels()
95 return $this->rowLabels;
98 public function getRowLabel($rowid)
100 if (array_key_exists($rowid, $this->rowLabels)) {
101 return $this->rowLabels[$rowid];
106 public function getOriginalData()
108 if (isset($this->originalData)) {
109 return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata());
111 return $this->getArrayCopy();
114 public function setOriginalData(MetadataHolderInterface $data)
116 $this->originalData = $data;
119 public function getTableData($includeRowKey = false)
121 $data = $this->getArrayCopy();
122 if ($this->isList()) {
123 $data = $this->convertTableToList();
125 if ($includeRowKey) {
126 $data = $this->getRowDataWithKey($data);
131 protected function convertTableToList()
134 foreach ($this as $row) {
135 foreach ($row as $key => $value) {
136 $result[$key][] = $value;
142 protected function getRowDataWithKey($data)
146 foreach ($data as $key => $row) {
147 array_unshift($row, $this->getHeader($key));
149 $result[$key] = $row;