-
Notifications
You must be signed in to change notification settings - Fork 61
/
Document.php
206 lines (173 loc) · 4.82 KB
/
Document.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
<?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;
use ML\IRI\IRI;
/**
* A Document represents a JSON-LD document.
*
* Named graphs are not supported yet.
*
* @author Markus Lanthaler <[email protected]>
*/
class Document implements DocumentInterface, JsonLdSerializable
{
/**
* @var IRI The document's IRI
*/
protected $iri = null;
/**
* @var GraphInterface The default graph
*/
protected $defaultGraph = null;
/**
* @var array An associative array holding all named graphs in the document
*/
protected $namedGraphs = array();
/**
* Parses a JSON-LD document and returns it as a Document
*
* The document can be supplied directly as a string or by passing a
* file path or an IRI.
*
* Usage:
* <code>
* $document = Document::load('document.jsonld');
* </code>
*
* <strong>Please note that currently all data is merged into the
* default graph, named graphs are not supported yet!</strong>
*
* It is possible to configure the processing by setting the options
* parameter accordingly. Available options are:
*
* - <em>base</em> The base IRI of the input document.
*
* @param string|array|JsonObject $document The JSON-LD document to process.
* @param null|array|JsonObject $options Options to configure the processing.
*
* @return Document The parsed JSON-LD document.
*
* @throws ParseException If the JSON-LD input document is invalid.
*/
public static function load($document, $options = null)
{
return JsonLD::getDocument($document, $options);
}
/**
* Constructor
*
* @param null|string|IRI $iri The document's IRI
*/
public function __construct($iri = null)
{
$this->iri = new IRI($iri);
$this->defaultGraph = new Graph($this);
}
/**
* {@inheritdoc}
*/
public function setIri($iri)
{
$this->iri = new IRI($iri);
return $this;
}
/**
* {@inheritdoc}
*/
public function getIri($asObject = false)
{
return ($asObject) ? $this->iri : (string) $this->iri;
}
/**
* {@inheritdoc}
*/
public function createGraph($name)
{
$name = (string) $this->iri->resolve($name);
if (isset($this->namedGraphs[$name])) {
return $this->namedGraphs[$name];
}
return $this->namedGraphs[$name] = new Graph($this, $name);
}
/**
* {@inheritdoc}
*/
public function getGraph($name = null)
{
if (null === $name) {
return $this->defaultGraph;
}
$name = (string) $this->iri->resolve($name);
return isset($this->namedGraphs[$name])
? $this->namedGraphs[$name]
: null;
}
/**
* {@inheritdoc}
*/
public function getGraphNames()
{
return array_keys($this->namedGraphs);
}
/**
* {@inheritdoc}
*/
public function containsGraph($name)
{
$name = (string) $this->iri->resolve($name);
return isset($this->namedGraphs[$name]);
}
/**
* {@inheritdoc}
*/
public function removeGraph($graph = null)
{
// The default graph can't be "removed", it can just be reset
if (null === $graph) {
$this->defaultGraph = new Graph($this);
return $this;
}
if ($graph instanceof GraphInterface) {
foreach ($this->namedGraphs as $n => $g) {
if ($g === $graph) {
$name = $n;
break;
}
}
} else {
$name = (string) $this->iri->resolve($graph);
}
if (isset($this->namedGraphs[$name])) {
if ($this->namedGraphs[$name]->getDocument() === $this) {
$this->namedGraphs[$name]->removeFromDocument();
}
unset($this->namedGraphs[$name]);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function toJsonLd($useNativeTypes = true)
{
$defGraph = $this->defaultGraph->toJsonLd($useNativeTypes);
if (0 === count($this->namedGraphs)) {
return $defGraph;
}
foreach ($this->namedGraphs as $graphName => $graph) {
$namedGraph = new JsonObject();
$namedGraph->{'@id'} = $graphName;
$namedGraph->{'@graph'} = $graph->toJsonLd($useNativeTypes);
$defGraph[] = $namedGraph;
}
$document = new JsonObject();
$document->{'@graph'} = $defGraph;
return array($document);
}
}