Open Data Catalog v2.0.0
Dereferencer.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 Dereferencer {
14  use HelperTrait;
15  use LoggerTrait;
16 
23 
30 
36  private $dereferenceMethod;
37 
38  private $entityTypeManager;
39 
43  public function __construct(ConfigFactoryInterface $configService, EntityTypeManager $entityTypeManager) {
44  $this->setConfigService($configService);
45  $this->entityTypeManager = $entityTypeManager;
46  }
47 
59  public function dereference(stdClass $data, int $method = self::DEREFERENCE_OUTPUT_DEFAULT) {
60 
61  $this->setDereferenceMethod($method);
62  // Cycle through the dataset properties we seek to dereference.
63  foreach ($this->getPropertyList() as $property_id) {
64  if (isset($data->{$property_id})) {
65  $data->{$property_id} = $this->dereferenceProperty($property_id, $data->{$property_id});
66  }
67  }
68  return $data;
69  }
70 
80  private function setDereferenceMethod(int $method) {
81  return $this->dereferenceMethod = $method;
82  }
83 
95  private function dereferenceProperty(string $property_id, $uuid) {
96  if (is_array($uuid)) {
97  return $this->dereferenceMultiple($property_id, $uuid);
98  }
99  elseif (is_string($uuid) && $this->getUuidService()->isValid($uuid)) {
100  return $this->dereferenceSingle($property_id, $uuid);
101  }
102  else {
103  $this->log('value_referencer', 'Unexpected data type when dereferencing property_id: @property_id with uuid: @uuid',
104  [
105  '@property_id' => $property_id,
106  '@uuid' => var_export($uuid, TRUE),
107  ]);
108  return NULL;
109  }
110  }
111 
123  private function dereferenceMultiple(string $property_id, array $uuids) : array {
124  $result = [];
125  foreach ($uuids as $uuid) {
126  $data = $this->dereferenceSingle($property_id, $uuid);
127  if (NULL !== $data) {
128  $result[] = $data;
129  }
130  }
131  return $result;
132  }
133 
148  private function dereferenceSingle(string $property_id, string $uuid) {
149  $nodes = $this->entityTypeManager
150  ->getStorage('node')
151  ->loadByProperties([
152  'field_data_type' => $property_id,
153  'uuid' => $uuid,
154  ]);
155 
156  if ($node = reset($nodes)) {
157  if (isset($node->field_json_metadata->value)) {
158  $metadata = json_decode($node->field_json_metadata->value);
159  return ($this->dereferenceMethod == self::DEREFERENCE_OUTPUT_REFERENCE_IDS) ? $metadata : $metadata->data;
160  }
161  }
162  // If a property node was not found, it most likely means it was deleted
163  // while still being referenced.
164  $this->log(
165  'value_referencer',
166  'Property @property_id reference @uuid not found',
167  [
168  '@property_id' => $property_id,
169  '@uuid' => var_export($uuid, TRUE),
170  ]
171  );
172 
173  return NULL;
174  }
175 
176 }
Drupal\dkan_data\Reference\Dereferencer\DEREFERENCE_OUTPUT_REFERENCE_IDS
const DEREFERENCE_OUTPUT_REFERENCE_IDS
Definition: Dereferencer.php:29
Drupal\dkan_data\Reference
Definition: Dereferencer.php:3
Drupal\dkan_data\Reference\Dereferencer\__construct
__construct(ConfigFactoryInterface $configService, EntityTypeManager $entityTypeManager)
Definition: Dereferencer.php:43
Drupal\dkan_data\Reference\HelperTrait
trait HelperTrait
Definition: HelperTrait.php:11
Drupal\dkan_data\Reference\Dereferencer\DEREFERENCE_OUTPUT_DEFAULT
const DEREFERENCE_OUTPUT_DEFAULT
Definition: Dereferencer.php:22
Drupal\dkan_data\Reference\Dereferencer
Definition: Dereferencer.php:13
Drupal\dkan_common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\dkan_data\Reference\Dereferencer\dereference
dereference(stdClass $data, int $method=self::DEREFERENCE_OUTPUT_DEFAULT)
Definition: Dereferencer.php:59