annotate vendor/consolidation/output-formatters/src/StructuredData/MetadataHolderTrait.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
children
rev   line source
Chris@13 1 <?php
Chris@13 2 namespace Consolidation\OutputFormatters\StructuredData;
Chris@13 3
Chris@13 4 /**
Chris@13 5 * A structured data object may contains some elements that
Chris@13 6 * are actually metadata. Metadata is not included in the
Chris@13 7 * output of tabular data formatters (e.g. table, csv), although
Chris@13 8 * some of these (e.g. table) may render the metadata alongside
Chris@13 9 * the data. Raw data formatters (e.g. yaml, json) will render
Chris@13 10 * both the data and the metadata.
Chris@13 11 *
Chris@13 12 * There are two possible options for the data format; either the
Chris@13 13 * data is nested inside some element, and ever other item is
Chris@13 14 * metadata, or the metadata may be nested inside some element,
Chris@13 15 * and every other item is the data rows.
Chris@13 16 *
Chris@13 17 * Example 1: nested data
Chris@13 18 *
Chris@13 19 * [
Chris@13 20 * 'data' => [ ... rows of field data ... ],
Chris@13 21 * 'metadata1' => '...',
Chris@13 22 * 'metadata2' => '...',
Chris@13 23 * ]
Chris@13 24 *
Chris@13 25 * Example 2: nested metadata
Chris@13 26 *
Chris@13 27 * [
Chris@13 28 * 'metadata' => [ ... metadata items ... ],
Chris@13 29 * 'rowid1' => [ ... ],
Chris@13 30 * 'rowid2' => [ ... ],
Chris@13 31 * ]
Chris@13 32 *
Chris@13 33 * It is, of course, also possible that both the data and
Chris@13 34 * the metadata may be nested inside subelements.
Chris@13 35 */
Chris@13 36 trait MetadataHolderTrait
Chris@13 37 {
Chris@13 38 protected $dataKey = false;
Chris@13 39 protected $metadataKey = false;
Chris@13 40
Chris@13 41 public function getDataKey()
Chris@13 42 {
Chris@13 43 return $this->dataKey;
Chris@13 44 }
Chris@13 45
Chris@13 46 public function setDataKey($key)
Chris@13 47 {
Chris@13 48 $this->dataKey = $key;
Chris@13 49 return $this;
Chris@13 50 }
Chris@13 51
Chris@13 52 public function getMetadataKey()
Chris@13 53 {
Chris@13 54 return $this->metadataKey;
Chris@13 55 }
Chris@13 56
Chris@13 57 public function setMetadataKey($key)
Chris@13 58 {
Chris@13 59 $this->metadataKey = $key;
Chris@13 60 return $this;
Chris@13 61 }
Chris@13 62
Chris@13 63 public function extractData($data)
Chris@13 64 {
Chris@13 65 if ($this->metadataKey) {
Chris@13 66 unset($data[$this->metadataKey]);
Chris@13 67 }
Chris@13 68 if ($this->dataKey) {
Chris@13 69 if (!isset($data[$this->dataKey])) {
Chris@13 70 return [];
Chris@13 71 }
Chris@13 72 return $data[$this->dataKey];
Chris@13 73 }
Chris@13 74 return $data;
Chris@13 75 }
Chris@13 76
Chris@13 77 public function extractMetadata($data)
Chris@13 78 {
Chris@13 79 if (!$this->dataKey && !$this->metadataKey) {
Chris@13 80 return [];
Chris@13 81 }
Chris@13 82 if ($this->dataKey) {
Chris@13 83 unset($data[$this->dataKey]);
Chris@13 84 }
Chris@13 85 if ($this->metadataKey) {
Chris@13 86 if (!isset($data[$this->metadataKey])) {
Chris@13 87 return [];
Chris@13 88 }
Chris@13 89 return $data[$this->metadataKey];
Chris@13 90 }
Chris@13 91 return $data;
Chris@13 92 }
Chris@13 93
Chris@13 94 public function reconstruct($data, $metadata)
Chris@13 95 {
Chris@13 96 $reconstructedData = ($this->dataKey) ? [$this->dataKey => $data] : $data;
Chris@13 97 $reconstructedMetadata = ($this->metadataKey) ? [$this->metadataKey => $metadata] : $metadata;
Chris@13 98
Chris@13 99 return $reconstructedData + $reconstructedMetadata;
Chris@13 100 }
Chris@13 101 }