-
Notifications
You must be signed in to change notification settings - Fork 61
/
Graph.php
273 lines (230 loc) · 6.57 KB
/
Graph.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
<?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 ML\IRI\IRI;
/**
* A Graph represents a JSON-LD graph.
*
* @author Markus Lanthaler <[email protected]>
*/
class Graph implements GraphInterface, JsonLdSerializable
{
/**
* @var DocumentInterface The document this graph belongs to.
*/
private $document;
/**
* @var array An associative array holding all nodes in the graph
*/
protected $nodes = array();
/**
* A term map containing terms/prefixes mapped to IRIs. This is similar
* to a JSON-LD context but ignores all definitions except the IRI.
*
* @var array
*/
protected $termMap = array();
/**
* @var int Blank node counter
*/
private $blankNodeCounter = 0;
/**
* Constructor
*
* @param null|DocumentInterface $document The document the graph belongs to.
*/
public function __construct(DocumentInterface $document = null)
{
$this->document = $document;
}
/**
* {@inheritdoc}
*/
public function createNode($id = null, $preserveBnodeId = false)
{
if (!is_string($id) || (!$preserveBnodeId && ('_:' === substr($id, 0, 2)))) {
$id = $this->createBlankNodeId();
} else {
$id = (string) $this->resolveIri($id);
if (isset($this->nodes[$id])) {
return $this->nodes[$id];
}
}
return $this->nodes[$id] = new Node($this, $id);
}
/**
* {@inheritdoc}
*/
public function removeNode(NodeInterface $node)
{
if ($node->getGraph() === $this) {
$node->removeFromGraph();
}
$id = $node->getId();
if (!$node->isBlankNode()) {
$id = (string) $this->resolveIri($id);
}
unset($this->nodes[$id]);
return $this;
}
/**
* {@inheritdoc}
*/
public function getNodes()
{
return array_values($this->nodes);
}
/**
* {@inheritdoc}
*/
public function getNode($id)
{
if (!((strlen($id) >= 2) && ('_:' === substr($id, 0, 2)))) {
$id = (string) $this->resolveIri($id);
}
return isset($this->nodes[$id])
? $this->nodes[$id]
: null;
}
/**
* {@inheritdoc}
*/
public function getNodesByType($type)
{
if (is_string($type)) {
if (null === ($type = $this->getNode($type))) {
return array();
}
}
return $type->getNodesWithThisType();
}
/**
* {@inheritdoc}
*/
public function containsNode($id)
{
$node = $id;
if ($node instanceof Node) {
$id = $node->getId();
}
if ((null === $id) || !is_string($id)) {
return false;
}
if ((strlen($id) >= 2) && ('_:' === substr($id, 0, 2))) {
if (isset($this->nodes[$id]) && ($node === $this->nodes[$id])) {
return true;
}
return false;
}
$id = (string) $this->resolveIri($id);
return isset($this->nodes[$id]);
}
/**
* {@inheritdoc}
*/
public function getDocument()
{
return $this->document;
}
/**
* {@inheritdoc}
*/
public function removeFromDocument()
{
$doc = $this->document;
$this->document = null;
$doc->removeGraph($this);
return $this;
}
/**
* {@inheritdoc}
*/
public function merge(GraphInterface $graph)
{
$nodes = $graph->getNodes();
$bnodeMap = array();
foreach ($nodes as $node) {
if ($node->isBlankNode()) {
if (false === isset($bnodeMap[$node->getId()])) {
$bnodeMap[$node->getId()] = $this->createNode();
}
$n = $bnodeMap[$node->getId()];
} else {
$n = $this->createNode($node->getId());
}
foreach ($node->getProperties() as $property => $values) {
if (false === is_array($values)) {
$values = array($values);
}
foreach ($values as $val) {
if ($val instanceof NodeInterface) {
// If the value is another node, we just need to
// create a reference to the corresponding node
// in this graph. The properties will be merged
// in the outer loop
if ($val->isBlankNode()) {
if (false === isset($bnodeMap[$val->getId()])) {
$bnodeMap[$val->getId()] = $this->createNode();
}
$val = $bnodeMap[$val->getId()];
} else {
$val = $this->createNode($val->getId());
}
} elseif (is_object($val)) {
// Clone typed values and language-tagged strings
$val = clone $val;
}
$n->addPropertyValue($property, $val);
}
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function toJsonLd($useNativeTypes = true)
{
// Bring nodes into a deterministic order
$nodes = $this->nodes;
ksort($nodes);
$nodes = array_values($nodes);
$serializeNode = function ($node) use ($useNativeTypes) {
return $node->toJsonLd($useNativeTypes);
};
return array_map($serializeNode, $nodes);
}
/**
* Create a new blank node identifier unique to the document.
*
* @return string The new blank node identifier.
*/
protected function createBlankNodeId()
{
return '_:b' . $this->blankNodeCounter++;
}
/**
* Resolves an IRI against the document's IRI
*
* If the graph isn't attached to a document or the document's IRI is
* not set, the IRI is returned as-is.
*
* @param string|IRI $iri The (relative) IRI to resolve
*
* @return IRI The resolved IRI.
*/
protected function resolveIri($iri)
{
if (null === $this->document) {
$base = new IRI();
} else {
$base = $this->document->getIri(true);
}
return $base->resolve($iri);
}
}