-
Notifications
You must be signed in to change notification settings - Fork 61
/
Node.php
509 lines (424 loc) · 13 KB
/
Node.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<?php
/*
* (c) Markus Lanthaler <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ML\JsonLD;
use stdClass as JsonObject;
/**
* A Node represents a node in a JSON-LD graph.
*
* @author Markus Lanthaler <[email protected]>
*/
class Node implements NodeInterface, JsonLdSerializable
{
/** The @type constant. */
const TYPE = '@type';
/**
* @var GraphInterface The graph the node belongs to.
*/
private $graph;
/**
* @var string The ID of the node
*/
private $id;
/**
* @var array An associative array holding all properties of the node except it's ID
*/
private $properties = array();
/**
* An associative array holding all reverse properties of this node, i.e.,
* a pointers to all nodes that link to this node.
*
* @var array
*/
private $revProperties = array();
/**
* Constructor
*
* @param GraphInterface $graph The graph the node belongs to.
* @param null|string $id The ID of the node.
*/
public function __construct(GraphInterface $graph, $id = null)
{
$this->graph = $graph;
$this->id = $id;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function setType($type)
{
if ((null !== $type) && !($type instanceof NodeInterface)) {
if (is_array($type)) {
foreach ($type as $val) {
if ((null !== $val) && !($val instanceof NodeInterface)) {
throw new \InvalidArgumentException('type must be null, a Node, or an array of Nodes');
}
}
} else {
throw new \InvalidArgumentException('type must be null, a Node, or an array of Nodes');
}
}
$this->setProperty(self::TYPE, $type);
return $this;
}
/**
* {@inheritdoc}
*/
public function addType(NodeInterface $type)
{
$this->addPropertyValue(self::TYPE, $type);
return $this;
}
/**
* {@inheritdoc}
*/
public function removeType(NodeInterface $type)
{
$this->removePropertyValue(self::TYPE, $type);
return $this;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return $this->getProperty(self::TYPE);
}
/**
* {@inheritdoc}
*/
public function getNodesWithThisType()
{
if (null === ($nodes = $this->getReverseProperty(self::TYPE))) {
return array();
}
return (is_array($nodes)) ? $nodes : array($nodes);
}
/**
* {@inheritdoc}
*/
public function getGraph()
{
return $this->graph;
}
/**
* {@inheritdoc}
*/
public function removeFromGraph()
{
// Remove other node's properties and reverse properties pointing to
// this node
foreach ($this->revProperties as $property => $nodes) {
foreach ($nodes as $node) {
$node->removePropertyValue($property, $this);
}
}
foreach ($this->properties as $property => $values) {
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $value) {
if ($value instanceof NodeInterface) {
$this->removePropertyValue($property, $value);
}
}
}
$g = $this->graph;
$this->graph = null;
$g->removeNode($this);
return $this;
}
/**
* {@inheritdoc}
*/
public function isBlankNode()
{
return ((null === $this->id) || ('_:' === substr($this->id, 0, 2)));
}
/**
* {@inheritdoc}
*/
public function setProperty($property, $value)
{
if (null === $value) {
$this->removeProperty($property);
} else {
$this->doMergeIntoProperty((string) $property, array(), $value);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addPropertyValue($property, $value)
{
$existing = (isset($this->properties[(string) $property]))
? $this->properties[(string) $property]
: array();
if (!is_array($existing)) {
$existing = array($existing);
}
$this->doMergeIntoProperty((string) $property, $existing, $value);
return $this;
}
/**
* Merge a value into a set of existing values.
*
* @param string $property The name of the property.
* @param array $existingValues The existing values.
* @param mixed $value The value to merge into the existing
* values. This MUST NOT be an array.
*
* @throws \InvalidArgumentException If value is an array or an object
* which is neither a language-tagged
* string nor a typed value or a node.
*/
private function doMergeIntoProperty($property, $existingValues, $value)
{
// TODO: Handle lists!
if (null === $value) {
return;
}
if (!$this->isValidPropertyValue($value)) {
throw new \InvalidArgumentException(
'value must be a scalar, a node, a language-tagged string, or a typed value'
);
}
$normalizedValue = $this->normalizePropertyValue($value);
foreach ($existingValues as $existing) {
if ($this->equalValues($existing, $normalizedValue)) {
return;
}
}
$existingValues[] = $normalizedValue;
if (1 === count($existingValues)) {
$existingValues = $existingValues[0];
}
$this->properties[$property] = $existingValues;
if ($normalizedValue instanceof NodeInterface) {
$value->addReverseProperty($property, $this);
}
}
/**
* {@inheritdoc}
*/
public function removeProperty($property)
{
if (!isset($this->properties[(string) $property])) {
return $this;
}
$values = is_array($this->properties[(string) $property])
? $this->properties[(string) $property]
: array($this->properties[(string) $property]);
foreach ($values as $value) {
if ($value instanceof NodeInterface) {
$value->removeReverseProperty((string) $property, $this);
}
}
unset($this->properties[(string) $property]);
return $this;
}
/**
* {@inheritdoc}
*/
public function removePropertyValue($property, $value)
{
if (!$this->isValidPropertyValue($value) || !isset($this->properties[(string) $property])) {
return $this;
}
$normalizedValue = $this->normalizePropertyValue($value);
$values =& $this->properties[(string) $property];
if (!is_array($this->properties[(string) $property])) {
$values = array($values);
}
for ($i = 0, $length = count($values); $i < $length; $i++) {
if ($this->equalValues($values[$i], $normalizedValue)) {
if ($normalizedValue instanceof NodeInterface) {
$normalizedValue->removeReverseProperty((string) $property, $this);
}
unset($values[$i]);
break;
}
}
if (0 === count($values)) {
unset($this->properties[(string) $property]);
return $this;
}
$this->properties[(string) $property] = array_values($values); // re-index the array
if (1 === count($this->properties[(string) $property])) {
$this->properties[(string) $property] = $this->properties[(string) $property][0];
}
}
/**
* {@inheritdoc}
*/
public function getProperties()
{
return $this->properties;
}
/**
* {@inheritdoc}
*/
public function getProperty($property)
{
return (isset($this->properties[(string) $property]))
? $this->properties[(string) $property]
: null;
}
/**
* {@inheritdoc}
*/
public function getReverseProperties()
{
$result = array();
foreach ($this->revProperties as $key => $nodes) {
$result[$key] = array_values($nodes);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function getReverseProperty($property)
{
if (!isset($this->revProperties[(string) $property])) {
return null;
}
$result = array_values($this->revProperties[(string) $property]);
return (1 === count($result))
? $result[0]
: $result;
}
/**
* {@inheritdoc}
*/
public function equals(NodeInterface $other)
{
return $this === $other;
}
/**
* {@inheritdoc}
*/
public function toJsonLd($useNativeTypes = true)
{
$node = new \stdClass();
// Only label blank nodes if other nodes point to it
if ((false === $this->isBlankNode()) || (count($this->getReverseProperties()) > 0)) {
$node->{'@id'} = $this->getId();
}
$properties = $this->getProperties();
foreach ($properties as $prop => $values) {
if (false === is_array($values)) {
$values = array($values);
}
if (self::TYPE === $prop) {
$node->{'@type'} = array();
foreach ($values as $val) {
$node->{'@type'}[] = $val->getId();
}
continue;
}
$node->{$prop} = array();
foreach ($values as $value) {
if ($value instanceof NodeInterface) {
$ref = new \stdClass();
$ref->{'@id'} = $value->getId();
$node->{$prop}[] = $ref;
} elseif (is_object($value)) { // language-tagged string or typed value
$node->{$prop}[] = $value->toJsonLd($useNativeTypes);
} else {
$val = new JsonObject();
$val->{'@value'} = $value;
$node->{$prop}[] = $val;
}
}
}
return $node;
}
/**
* Add a reverse property.
*
* @param string $property The name of the property.
* @param NodeInterface $node The node which has a property pointing
* to this node instance.
*/
protected function addReverseProperty($property, NodeInterface $node)
{
$this->revProperties[$property][$node->getId()] = $node;
}
/**
* Remove a reverse property.
*
* @param string $property The name of the property.
* @param NodeInterface $node The node which has a property pointing
* to this node instance.
*/
protected function removeReverseProperty($property, NodeInterface $node)
{
unset($this->revProperties[$property][$node->getId()]);
if (0 === count($this->revProperties[$property])) {
unset($this->revProperties[$property]);
}
}
/**
* Checks whether a value is a valid property value.
*
* @param mixed $value The value to check.
*
* @return bool Returns true if the value is a valid property value;
* false otherwise.
*/
protected function isValidPropertyValue($value)
{
return (is_scalar($value) ||
(is_object($value) &&
((($value instanceof NodeInterface) && ($value->getGraph() === $this->graph)) ||
($value instanceof Value))));
}
/**
* Normalizes a property value by converting scalars to Value objects.
*
* @param mixed $value The value to normalize.
*
* @return NodeInterface|Value The normalized value.
*/
protected function normalizePropertyValue($value)
{
if (false === is_scalar($value)) {
return $value;
}
return Value::fromJsonLd((object) array('@value' => $value));
}
/**
* Checks whether the two specified values are the same.
*
* Scalars and nodes are checked for identity, value objects for
* equality.
*
* @param mixed $value1 Value 1.
* @param mixed $value2 Value 2.
*
* @return bool Returns true if the two values are equals; otherwise false.
*/
protected function equalValues($value1, $value2)
{
if (gettype($value1) !== gettype($value2)) {
return false;
}
if (is_object($value1) && ($value1 instanceof Value)) {
return $value1->equals($value2);
}
return ($value1 === $value2);
}
}