3ae18d46319de032145306cd3b13383f391e60a4
[yaffs-website] / vendor / consolidation / output-formatters / src / StructuredData / MetadataHolderTrait.php
1 <?php
2 namespace Consolidation\OutputFormatters\StructuredData;
3
4 /**
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.
11  *
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.
16  *
17  * Example 1: nested data
18  *
19  * [
20  *     'data' => [ ... rows of field data ... ],
21  *     'metadata1' => '...',
22  *     'metadata2' => '...',
23  * ]
24  *
25  * Example 2: nested metadata
26  *
27  * [
28  *      'metadata' => [ ... metadata items ... ],
29  *      'rowid1' => [ ... ],
30  *      'rowid2' => [ ... ],
31  * ]
32  *
33  * It is, of course, also possible that both the data and
34  * the metadata may be nested inside subelements.
35  */
36 trait MetadataHolderTrait
37 {
38     protected $dataKey = false;
39     protected $metadataKey = false;
40
41     public function getDataKey()
42     {
43         return $this->dataKey;
44     }
45
46     public function setDataKey($key)
47     {
48         $this->dataKey = $key;
49         return $this;
50     }
51
52     public function getMetadataKey()
53     {
54         return $this->metadataKey;
55     }
56
57     public function setMetadataKey($key)
58     {
59         $this->metadataKey = $key;
60         return $this;
61     }
62
63     public function extractData($data)
64     {
65         if ($this->metadataKey) {
66             unset($data[$this->metadataKey]);
67         }
68         if ($this->dataKey) {
69             if (!isset($data[$this->dataKey])) {
70                 return [];
71             }
72             return $data[$this->dataKey];
73         }
74         return $data;
75     }
76
77     public function extractMetadata($data)
78     {
79         if (!$this->dataKey && !$this->metadataKey) {
80             return [];
81         }
82         if ($this->dataKey) {
83             unset($data[$this->dataKey]);
84         }
85         if ($this->metadataKey) {
86             if (!isset($data[$this->metadataKey])) {
87                 return [];
88             }
89             return $data[$this->metadataKey];
90         }
91         return $data;
92     }
93
94     public function reconstruct($data, $metadata)
95     {
96         $reconstructedData = ($this->dataKey) ? [$this->dataKey => $data] : $data;
97         $reconstructedMetadata = ($this->metadataKey) ? [$this->metadataKey => $metadata] : $metadata;
98
99         return $reconstructedData + $reconstructedMetadata;
100     }
101 }