2 namespace Consolidation\OutputFormatters\StructuredData;
5 * A structured data object may contains some elements that
6 * are actually metadata. Metadata is not included in the
7 * output of tabular data formatters (e.g. table, csv), although
8 * some of these (e.g. table) may render the metadata alongside
9 * the data. Raw data formatters (e.g. yaml, json) will render
10 * both the data and the metadata.
12 * There are two possible options for the data format; either the
13 * data is nested inside some element, and ever other item is
14 * metadata, or the metadata may be nested inside some element,
15 * and every other item is the data rows.
17 * Example 1: nested data
20 * 'data' => [ ... rows of field data ... ],
21 * 'metadata1' => '...',
22 * 'metadata2' => '...',
25 * Example 2: nested metadata
28 * 'metadata' => [ ... metadata items ... ],
29 * 'rowid1' => [ ... ],
30 * 'rowid2' => [ ... ],
33 * It is, of course, also possible that both the data and
34 * the metadata may be nested inside subelements.
36 trait MetadataHolderTrait
38 protected $dataKey = false;
39 protected $metadataKey = false;
41 public function getDataKey()
43 return $this->dataKey;
46 public function setDataKey($key)
48 $this->dataKey = $key;
52 public function getMetadataKey()
54 return $this->metadataKey;
57 public function setMetadataKey($key)
59 $this->metadataKey = $key;
63 public function extractData($data)
65 if ($this->metadataKey) {
66 unset($data[$this->metadataKey]);
69 if (!isset($data[$this->dataKey])) {
72 return $data[$this->dataKey];
77 public function extractMetadata($data)
79 if (!$this->dataKey && !$this->metadataKey) {
83 unset($data[$this->dataKey]);
85 if ($this->metadataKey) {
86 if (!isset($data[$this->metadataKey])) {
89 return $data[$this->metadataKey];
94 public function reconstruct($data, $metadata)
96 $reconstructedData = ($this->dataKey) ? [$this->dataKey => $data] : $data;
97 $reconstructedMetadata = ($this->metadataKey) ? [$this->metadataKey => $metadata] : $metadata;
99 return $reconstructedData + $reconstructedMetadata;