Open Data Catalog v
Data.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\node\Entity\Node;
10 
14 class Data implements MetastoreItemInterface {
15  use LoggerTrait;
16 
22  protected $node;
23 
29  protected $rawMetadata;
30 
39  public function __construct(EntityInterface $entity) {
40  $this->validate($entity);
41  $this->node = $entity;
42  }
43 
47  public function getCacheContexts() {
48  return $this->node->getCacheContexts();
49  }
50 
54  public function getCacheTags() {
55  $cacheTags = $this->node->getCacheTags();
56  return $cacheTags;
57  }
58 
62  public function getCacheMaxAge() {
63  return $this->node->getCacheMaxAge();
64  }
65 
69  private function fix() {
70  $this->fixDataType();
71  $this->saveRawMetadata();
72  }
73 
77  public function getModifiedDate() {
78  $this->fix();
79  return $this->node->getChangedTime();
80  }
81 
85  public function getIdentifier() {
86  $this->fix();
87 
88  return $this->node->uuid();
89  }
90 
94  public function getRawMetadata() {
95  $this->fix();
96  if (isset($this->node->rawMetadata)) {
97  return json_decode($this->node->rawMetadata);
98  }
99  }
100 
104  public function getDataType() {
105  $this->fix();
106  return $this->node->get('field_data_type')->value;
107  }
108 
112  public function getMetaData() {
113  $this->fix();
114  return json_decode($this->node->get('field_json_metadata')->getString());
115  }
116 
120  public function setMetadata($metadata) {
121  $this->fix();
122  $this->node->set('field_json_metadata', json_encode($metadata));
123  }
124 
128  public function setIdentifier($identifier) {
129  $this->fix();
130  $this->node->set('uuid', $identifier);
131  }
132 
136  public function setTitle($title) {
137  $this->fix();
138  $this->node->set('title', $title);
139  }
140 
144  public function isNew() {
145  return $this->node->isNew();
146  }
147 
151  private function validate(EntityInterface $entity) {
152  if (!($entity instanceof Node)) {
153  throw new DataNodeLifeCycleEntityValidationException("We only work with nodes.");
154  }
155 
156  if ($entity->bundle() != "data") {
157  throw new DataNodeLifeCycleEntityValidationException("We only work with data nodes.");
158  }
159  }
160 
164  private function fixDataType() {
165  if (empty($this->node->get('field_data_type')->getString())) {
166  $this->node->set('field_data_type', 'dataset');
167  }
168  }
169 
173  public function getSchemaId() {
174  $this->fix();
175  $schemaId = $this->node->get('field_data_type')->getString();
176  return $schemaId;
177  }
178 
182  private function saveRawMetadata() {
183  // Temporarily save the raw json metadata, for later use.
184  if (!isset($this->node->rawMetadata)) {
185  $raw = $this->node->get('field_json_metadata')->value;
186  $this->node->rawMetadata = $raw;
187  }
188  }
189 
193  public function getOriginal() {
194  if (isset($this->node->original)) {
195  return new Data($this->node->original);
196  }
197  }
198 
199 }
Drupal\metastore\NodeWrapper\Data\getOriginal
getOriginal()
Definition: Data.php:193
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\metastore\NodeWrapper\Data\setMetadata
setMetadata($metadata)
Definition: Data.php:120
Drupal\metastore\NodeWrapper\Data\getModifiedDate
getModifiedDate()
Definition: Data.php:77
Drupal\metastore\NodeWrapper\Data\getMetaData
getMetaData()
Definition: Data.php:112
Drupal\metastore\NodeWrapper\Data\setIdentifier
setIdentifier($identifier)
Definition: Data.php:128
Drupal\metastore\NodeWrapper\Data\getDataType
getDataType()
Definition: Data.php:104
Drupal\metastore\NodeWrapper\Data
Definition: Data.php:14
Drupal\common\Exception\DataNodeLifeCycleEntityValidationException
Definition: DataNodeLifeCycleEntityValidationException.php:8
Drupal\metastore\NodeWrapper\Data\getCacheContexts
getCacheContexts()
Definition: Data.php:47
Drupal\metastore\NodeWrapper\Data\getCacheMaxAge
getCacheMaxAge()
Definition: Data.php:62
Drupal\metastore\NodeWrapper\Data\setTitle
setTitle($title)
Definition: Data.php:136
Drupal\metastore\NodeWrapper\Data\getCacheTags
getCacheTags()
Definition: Data.php:54
Drupal\metastore\NodeWrapper\Data\$node
$node
Definition: Data.php:22
Drupal\metastore\NodeWrapper\Data\__construct
__construct(EntityInterface $entity)
Definition: Data.php:39
Drupal\metastore\NodeWrapper\Data\isNew
isNew()
Definition: Data.php:144
Drupal\metastore\NodeWrapper\Data\getSchemaId
getSchemaId()
Definition: Data.php:173
Drupal\metastore\MetastoreItemInterface
Definition: MetastoreItemInterface.php:10
Drupal\metastore\NodeWrapper\Data\getRawMetadata
getRawMetadata()
Definition: Data.php:94
Drupal\metastore\NodeWrapper\Data\getIdentifier
getIdentifier()
Definition: Data.php:85
Drupal\metastore\NodeWrapper\Data\$rawMetadata
$rawMetadata
Definition: Data.php:29
Drupal\metastore\NodeWrapper
Definition: Data.php:3