Open Data Catalog v2.0.0
Referencer.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityTypeManager;
8 use stdClass;
9 
13 class Referencer {
14  use HelperTrait;
15  use LoggerTrait;
16 
17  private $entityTypeManager;
18 
22  public function __construct(ConfigFactoryInterface $configService, EntityTypeManager $entityTypeManager) {
23  $this->setConfigService($configService);
24  $this->entityTypeManager = $entityTypeManager;
25  }
26 
36  public function reference(stdClass $data) {
37  // Cycle through the dataset properties we seek to reference.
38  foreach ($this->getPropertyList() as $property_id) {
39  if (isset($data->{$property_id})) {
40  $data->{$property_id} = $this->referenceProperty($property_id, $data->{$property_id});
41  }
42  }
43  return $data;
44  }
45 
57  private function referenceProperty(string $property_id, $data) {
58  if (is_array($data)) {
59  return $this->referenceMultiple($property_id, $data);
60  }
61  else {
62  // Case for $data being an object or a string.
63  return $this->referenceSingle($property_id, $data);
64  }
65  }
66 
78  private function referenceMultiple(string $property_id, array $values) : array {
79  $result = [];
80  foreach ($values as $value) {
81  $data = $this->referenceSingle($property_id, $value);
82  if (NULL !== $data) {
83  $result[] = $data;
84  }
85  }
86  return $result;
87  }
88 
100  private function referenceSingle(string $property_id, $value) {
101  $uuid = $this->checkExistingReference($property_id, $value);
102  if (!$uuid) {
103  $uuid = $this->createPropertyReference($property_id, $value);
104  }
105  if ($uuid) {
106  return $uuid;
107  }
108  else {
109  if ($this->loggerService) {
110  $this->loggerService->get('value_referencer')->error(
111  'Neither found an existing nor could create a new reference for property_id: @property_id with value: @value',
112  [
113  '@property_id' => $property_id,
114  '@value' => var_export($value, TRUE),
115  ]
116  );
117  }
118  return NULL;
119  }
120  }
121 
136  private function checkExistingReference(string $property_id, $data) {
137  $nodes = $this->entityTypeManager
138  ->getStorage('node')
139  ->loadByProperties([
140  'field_data_type' => $property_id,
141  'title' => md5(json_encode($data)),
142  ]);
143 
144  if ($node = reset($nodes)) {
145  return $node->uuid();
146  }
147  return NULL;
148  }
149 
165  private function createPropertyReference(string $property_id, $value) {
166  // Create json metadata for the reference.
167  $data = new stdClass();
168  $data->identifier = $this->getUuidService()->generate($property_id, $value);
169  $data->data = $value;
170 
171  // Create node to store this reference.
172  $node = $this->entityTypeManager
173  ->getStorage('node')
174  ->create([
175  'title' => md5(json_encode($value)),
176  'type' => 'data',
177  'uuid' => $data->identifier,
178  'field_data_type' => $property_id,
179  'field_json_metadata' => json_encode($data),
180  ]);
181  $node->save();
182 
183  return $node->uuid();
184  }
185 
186 }
Drupal\dkan_data\Reference
Definition: Dereferencer.php:3
Drupal\dkan_data\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\dkan_data\Reference\Referencer\reference
reference(stdClass $data)
Definition: Referencer.php:36
Drupal\dkan_data\Reference\Referencer
Definition: Referencer.php:13
Drupal\dkan_data\Reference\Referencer\__construct
__construct(ConfigFactoryInterface $configService, EntityTypeManager $entityTypeManager)
Definition: Referencer.php:22
Drupal\dkan_common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11