-
Notifications
You must be signed in to change notification settings - Fork 2
/
rdfmapper.php
266 lines (228 loc) · 7.56 KB
/
rdfmapper.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
class midgardmvc_ui_create_rdfmapper
{
public $typeof = '';
public $about = '';
public $mgdschema = '';
public function __construct($mgdschema, $identifier = null)
{
if (is_object($mgdschema))
{
if ($mgdschema->guid)
{
$this->about = "urn:uuid:{$mgdschema->guid}";
}
$mgdschema = get_class($mgdschema);
}
if ($identifier)
{
$this->about = $identifier;
}
$this->mgdschema = $mgdschema;
$this->typeof = self::class_to_typeof($mgdschema);
}
public function __get($key)
{
return $this->map_property($key);
}
private function map_property($property)
{
$property = trim($property, '<');
$property = trim($property, '>');
static $property_map = array();
if (!isset($property_map[$this->mgdschema]))
{
$property_map[$this->mgdschema] = array();
}
if (isset($property_map[$this->mgdschema][$property]))
{
return $property_map[$this->mgdschema][$property];
}
$namespaces = self::get_namespace_map($this->mgdschema);
$namespaced_property = self::expand_prefix($property, $namespaces);
$dummy = new $this->mgdschema();
$props = get_object_vars($dummy);
$reflector = new midgard_reflection_property($this->mgdschema);
foreach ($props as $prop => $value)
{
$rdfprop = $reflector->get_user_value($prop, 'property');
if (!$rdfprop)
{
$rdfprop = "mgd:{$prop}";
}
if ($property == $prop)
{
// Straight, un-namespaced match
$property_map[$this->mgdschema][$property] = $rdfprop;
return $property_map[$this->mgdschema][$property];
}
$nsprop = self::expand_prefix($rdfprop, $namespaces);
if ($namespaced_property == $nsprop)
{
$property_map[$this->mgdschema][$property] = $prop;
return $property_map[$this->mgdschema][$property];
}
}
throw new midgardmvc_exception_notfound("Unable to map {$property} to a property of {$this->mgdschema}");
}
public static function class_to_typeof($mgdschema)
{
static $typeofs = array();
if (isset($typeofs[$mgdschema]))
{
return $typeofs[$mgdschema];
}
$mgdschemas = self::get_mgdschemas();
if (!in_array($mgdschema, $mgdschemas))
{
throw new midgardmvc_exception_notfound("Unrecognized MgdSchema type {$mgdschema}");
}
$namespaces = self::get_namespace_map($mgdschema);
$reflector = new midgard_reflection_class($mgdschema);
$typeofs[$mgdschema] = $reflector->get_user_value('typeof');
if (!$typeofs[$mgdschema])
{
$typeofs[$mgdschema] = "{$namespaces['mgd']}{$mgdschema}";
}
return $typeofs[$mgdschema];
}
private static function from_reference($string)
{
/*
if ( substr($string, 0, 1) == '<'
&& substr($string, -1, 1) == '>') {
return substr($string, 1, strlen($string) - 2);
}
*/
$string = trim($string, '<');
$string = trim($string, '>');
return $string;
}
public static function typeof_to_class($type)
{
$type = self::from_reference($type);
static $mapped_classes = array();
if (isset($mapped_classes[$type]))
{
return $mapped_classes[$type];
}
// Load class mappings
$mgdschemas = self::get_mgdschemas();
// Check for RDF type match
foreach ($mgdschemas as $mgdschema)
{
$namespaces = self::get_namespace_map($mgdschema);
$reflector = new midgard_reflection_class($mgdschema);
$typeof = $reflector->get_user_value('typeof');
if (!$typeof)
{
$typeof = "{$namespaces['mgd']}{$mgdschema}";
}
if ($typeof == $type)
{
// Straight, fully-qualified match
$mapped_classes[$type] = $mgdschema;
return $mapped_classes[$type];
}
foreach ($namespaces as $prefix => $url)
{
$nstype = $type;
if (substr($type, 0, strlen($prefix)) == $prefix)
{
$nstype = str_replace("{$prefix}:", $url, $type);
}
if ($typeof == $nstype)
{
$mapped_classes[$type] = $mgdschema;
return $mapped_classes[$type];
}
}
}
throw new midgardmvc_exception_notfound("Unrecognized MgdSchema type {$type}");
}
private static function get_mgdschemas()
{
return midgardmvc_core::get_instance()->dispatcher->get_mgdschema_classes();
}
private static function expand_prefix($value, array $namespaces)
{
foreach ($namespaces as $prefix => $url)
{
if (substr($value, 0, strlen($prefix)) == $prefix)
{
// Expand to prefix to URL
return str_replace("{$prefix}:", $url, $value);
}
if (substr($value, 0, strlen($url)) == $url)
{
// Full URL match
return $value;
}
}
return "{$namespaces['mgd']}{$value}";
}
private static function get_namespace_map($type)
{
static $nsmap = array();
if (isset($nsmap[$type]))
{
return $nsmap[$type];
}
$mgdschemas = self::get_mgdschemas();
if (!in_array($type, $mgdschemas))
{
throw new midgardmvc_exception_notfound("Type {$type} is not a registered MgdSchema");
}
$nsmap[$type] = array
(
'mgd' => 'http://www.midgard-project.org/midgard2/9.03/',
);
$reflector = new midgard_reflection_class($type);
$namespaces = $reflector->get_user_value('namespaces');
if (empty($namespaces))
{
// Type didn't register additional namespaces
return $nsmap[$type];
}
$namespaces = explode(',', $namespaces);
foreach ($namespaces as $namespace)
{
$namespace_parts = explode(':', $namespace, 2);
$nsmap[$type][$namespace_parts[0]] = $namespace_parts[1];
}
return $nsmap[$type];
}
public static function load_object($type, $identifier = null)
{
if ($type)
{
$mgdschema = self::typeof_to_class($type);
if ( is_null($identifier)
|| substr(self::from_reference($identifier), 0, 7) == '_:bnode')
{
return new $mgdschema;
}
}
$identifier = self::from_reference($identifier);
if (substr($identifier, 0, 4) == 'mgd:')
{
$identifier = substr($identifier, 4);
}
elseif (substr($identifier, 0, 9) == 'urn:uuid:')
{
$identifier = substr($identifier, 9);
}
try
{
if (!$type)
{
return midgard_object_class::get_object_by_guid($identifier);
}
return new $mgdschema($identifier);
}
catch (midgard_error_exception $e)
{
throw new midgardmvc_exception_notfound("Object {$guid}: " . $e->getMessage());
}
}
}