annotate vendor/consolidation/output-formatters/src/Transformations/TableTransformation.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace Consolidation\OutputFormatters\Transformations;
Chris@0 3
Chris@0 4 use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
Chris@0 5 use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface;
Chris@13 6 use Consolidation\OutputFormatters\StructuredData\MetadataHolderInterface;
Chris@0 7
Chris@0 8 class TableTransformation extends \ArrayObject implements TableDataInterface, OriginalDataInterface
Chris@0 9 {
Chris@0 10 protected $headers;
Chris@0 11 protected $rowLabels;
Chris@0 12 protected $layout;
Chris@13 13 /** @var MetadataHolderInterface */
Chris@13 14 protected $originalData;
Chris@0 15
Chris@0 16 const TABLE_LAYOUT = 'table';
Chris@0 17 const LIST_LAYOUT = 'list';
Chris@0 18
Chris@0 19 public function __construct($data, $fieldLabels, $rowLabels = [])
Chris@0 20 {
Chris@0 21 $this->headers = $fieldLabels;
Chris@0 22 $this->rowLabels = $rowLabels;
Chris@0 23 $rows = static::transformRows($data, $fieldLabels);
Chris@0 24 $this->layout = self::TABLE_LAYOUT;
Chris@0 25 parent::__construct($rows);
Chris@0 26 }
Chris@0 27
Chris@0 28 public function setLayout($layout)
Chris@0 29 {
Chris@0 30 $this->layout = $layout;
Chris@0 31 }
Chris@0 32
Chris@0 33 public function getLayout()
Chris@0 34 {
Chris@0 35 return $this->layout;
Chris@0 36 }
Chris@0 37
Chris@0 38 public function isList()
Chris@0 39 {
Chris@0 40 return $this->layout == self::LIST_LAYOUT;
Chris@0 41 }
Chris@0 42
Chris@0 43 protected static function transformRows($data, $fieldLabels)
Chris@0 44 {
Chris@0 45 $rows = [];
Chris@0 46 foreach ($data as $rowid => $row) {
Chris@0 47 $rows[$rowid] = static::transformRow($row, $fieldLabels);
Chris@0 48 }
Chris@0 49 return $rows;
Chris@0 50 }
Chris@0 51
Chris@0 52 protected static function transformRow($row, $fieldLabels)
Chris@0 53 {
Chris@0 54 $result = [];
Chris@0 55 foreach ($fieldLabels as $key => $label) {
Chris@0 56 $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
Chris@0 57 }
Chris@0 58 return $result;
Chris@0 59 }
Chris@0 60
Chris@0 61 public function getHeaders()
Chris@0 62 {
Chris@0 63 return $this->headers;
Chris@0 64 }
Chris@0 65
Chris@0 66 public function getHeader($key)
Chris@0 67 {
Chris@0 68 if (array_key_exists($key, $this->headers)) {
Chris@0 69 return $this->headers[$key];
Chris@0 70 }
Chris@0 71 return $key;
Chris@0 72 }
Chris@0 73
Chris@0 74 public function getRowLabels()
Chris@0 75 {
Chris@0 76 return $this->rowLabels;
Chris@0 77 }
Chris@0 78
Chris@0 79 public function getRowLabel($rowid)
Chris@0 80 {
Chris@0 81 if (array_key_exists($rowid, $this->rowLabels)) {
Chris@0 82 return $this->rowLabels[$rowid];
Chris@0 83 }
Chris@0 84 return $rowid;
Chris@0 85 }
Chris@0 86
Chris@0 87 public function getOriginalData()
Chris@0 88 {
Chris@13 89 if (isset($this->originalData)) {
Chris@13 90 return $this->originalData->reconstruct($this->getArrayCopy(), $this->originalData->getMetadata());
Chris@13 91 }
Chris@0 92 return $this->getArrayCopy();
Chris@0 93 }
Chris@0 94
Chris@13 95 public function setOriginalData(MetadataHolderInterface $data)
Chris@13 96 {
Chris@13 97 $this->originalData = $data;
Chris@13 98 }
Chris@13 99
Chris@0 100 public function getTableData($includeRowKey = false)
Chris@0 101 {
Chris@0 102 $data = $this->getArrayCopy();
Chris@0 103 if ($this->isList()) {
Chris@0 104 $data = $this->convertTableToList();
Chris@0 105 }
Chris@0 106 if ($includeRowKey) {
Chris@0 107 $data = $this->getRowDataWithKey($data);
Chris@0 108 }
Chris@0 109 return $data;
Chris@0 110 }
Chris@0 111
Chris@0 112 protected function convertTableToList()
Chris@0 113 {
Chris@0 114 $result = [];
Chris@0 115 foreach ($this as $row) {
Chris@0 116 foreach ($row as $key => $value) {
Chris@0 117 $result[$key][] = $value;
Chris@0 118 }
Chris@0 119 }
Chris@0 120 return $result;
Chris@0 121 }
Chris@0 122
Chris@0 123 protected function getRowDataWithKey($data)
Chris@0 124 {
Chris@0 125 $result = [];
Chris@0 126 $i = 0;
Chris@0 127 foreach ($data as $key => $row) {
Chris@0 128 array_unshift($row, $this->getHeader($key));
Chris@0 129 $i++;
Chris@0 130 $result[$key] = $row;
Chris@0 131 }
Chris@0 132 return $result;
Chris@0 133 }
Chris@0 134 }