-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathDereferencer.php
190 lines (172 loc) · 4.85 KB
/
Dereferencer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Drupal\metastore\Reference;
use Contracts\FactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Psr\Log\LoggerInterface;
use Drupal\metastore\Exception\MissingObjectException;
/**
* Metastore dereferencer.
*/
class Dereferencer {
use HelperTrait;
/**
* Storage factory interface service.
*
* @var \Contracts\FactoryInterface
*/
private $storageFactory;
/**
* DKAN logger channel service.
*/
private LoggerInterface $logger;
/**
* Constructor.
*/
public function __construct(
ConfigFactoryInterface $configService,
FactoryInterface $storageFactory,
LoggerInterface $loggerChannel
) {
$this->setConfigService($configService);
$this->storageFactory = $storageFactory;
$this->logger = $loggerChannel;
}
/**
* Replaces value references in a dataset with their actual values.
*
* @param object $data
* The json metadata object.
*
* @return mixed
* Modified json metadata object.
*/
public function dereference($data) {
$this->validate($data);
// Cycle through the dataset properties we seek to dereference.
foreach ($this->getPropertyList() as $propertyId) {
if (isset($data->{$propertyId})) {
$this->dereferenceProperty($propertyId, $data);
}
}
return $data;
}
/**
* Dereferences property and handles empty values if any.
*
* @param string $propertyId
* The dataset property id.
* @param object $data
* Modified json metadata object.
*/
private function dereferenceProperty(string $propertyId, $data) {
$referenceProperty = "%Ref:{$propertyId}";
$ref = NULL;
$actual = NULL;
[$ref, $actual] = $this->dereferencePropertyUuid($propertyId, $data->{$propertyId});
if (!empty($ref) && !empty($actual)) {
$data->{$referenceProperty} = $ref;
$data->{$propertyId} = $actual;
}
else {
unset($data->{$propertyId});
}
}
/**
* Replaces a property reference with its actual value, general case.
*
* @param string $property_id
* The dataset property id.
* @param string|array $uuid
* A single reference uuid string, or an array of reference uuids.
*
* @return mixed
* An array of dereferenced values, a single one, or NULL.
*/
private function dereferencePropertyUuid(string $property_id, $uuid) {
if (is_array($uuid)) {
return $this->dereferenceMultiple($property_id, $uuid);
}
elseif (is_string($uuid) && $this->getUuidService()->isValid($uuid)) {
return $this->dereferenceSingle($property_id, $uuid);
}
else {
$this->logger->error('Unexpected data type when dereferencing property_id: @property_id with uuid: @uuid', [
'@property_id' => $property_id,
'@uuid' => var_export($uuid, TRUE),
]);
return NULL;
}
}
/**
* Replaces a property reference with its actual value, array case.
*
* @param string $property_id
* A dataset property id.
* @param array $uuids
* An array of reference uuids.
*
* @return array
* An array of dereferenced values.
*/
private function dereferenceMultiple(string $property_id, array $uuids) : array {
$result = [];
$reference = [];
$ref = NULL;
$actual = NULL;
foreach ($uuids as $uuid) {
[$ref, $actual] = $this->dereferenceSingle($property_id, $uuid);
if (NULL !== $ref && NULL !== $actual) {
$result[] = $actual;
$reference[] = $ref;
}
}
return [$reference, $result];
}
/**
* Replaces a property reference with its actual value, string or object case.
*
* @param string $property_id
* The dataset property id.
* @param string $uuid
* Either a uuid or an actual json value.
*
* @return object|string
* The data from this reference.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function dereferenceSingle(string $property_id, string $uuid) {
$storage = $this->storageFactory->getInstance($property_id);
try {
$value = $storage->retrieve($uuid);
}
catch (MissingObjectException) {
$value = FALSE;
}
if ($value) {
$metadata = json_decode($value);
return [$metadata, $metadata->data];
}
// If a property node was not found, it most likely means it was deleted
// while still being referenced.
$this->logger->error('Property @property_id reference @uuid not found', [
'@property_id' => $property_id,
'@uuid' => var_export($uuid, TRUE),
]);
return [NULL, NULL];
}
/**
* Validates data.
*
* @param object $data
* The json metadata object.
*
* @throws \Exception
*/
private function validate($data) {
if (!is_object($data)) {
throw new \Exception("data must be an object.");
}
}
}