This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractStatement.php
202 lines (188 loc) · 6.23 KB
/
AbstractStatement.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
<?php
/*
* This file is part of Saft.
*
* (c) Konrad Abicht <[email protected]>
* (c) Natanael Arndt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Saft\Rdf;
/**
* @api
*
* @since 0.1
*/
abstract class AbstractStatement implements Statement
{
/**
* Returns true if neither subject, predicate, object nor, if available, graph, are patterns.
*
* @return bool true, if if neither subject, predicate, object nor, if available, graph, are patterns,
* false otherwise
*
* @api
*
* @since 0.1
*/
public function isConcrete()
{
if ($this->isQuad() && !$this->getGraph()->isConcrete()) {
return false;
}
return $this->getSubject()->isConcrete()
&& $this->getPredicate()->isConcrete()
&& $this->getObject()->isConcrete();
}
/**
* Returns true if at least subject, predicate, object or, if available, graph, are patterns.
*
* @return bool true, if at least subject, predicate, object or, if available, graph, are patterns,
* false otherwise
*
* @api
*
* @since 0.1
*/
public function isPattern()
{
return !$this->isConcrete();
}
/**
* Transforms content of the Statement to n-quads form.
*
* @return string N-Quads string containing subject, predicate, object and graph, if available
*
* @throws \Exception if this instance is a non-concrete statement
*
* @api
*
* @since 0.1
*/
public function toNQuads()
{
if ($this->isConcrete()) {
if ($this->isQuad()) {
return $this->getSubject()->toNQuads().' '.
$this->getPredicate()->toNQuads().' '.
$this->getObject()->toNQuads().' '.
$this->getGraph()->toNQuads().' .';
} else {
return $this->getSubject()->toNQuads().' '.
$this->getPredicate()->toNQuads().' '.
$this->getObject()->toNQuads().' .';
}
} else {
throw new \Exception('A Statement has to be concrete in N-Quads.');
}
}
/**
* Transforms content of the Statement to n-triples form.
*
* @return string N-triples string, containing subject, predicate and object
*
* @throws \Exception if this instance is a non-concrete statement
*
* @api
*
* @since 0.1
*/
public function toNTriples()
{
if ($this->isConcrete()) {
return $this->getSubject()->toNQuads().' '.
$this->getPredicate()->toNQuads().' '.
$this->getObject()->toNQuads().' .';
} else {
throw new \Exception('A Statement has to be concrete in N-Triples.');
}
}
/**
* This method is ment for getting some kind of human readable string representation of the current node.
* It returns a string which contains subject, predicate and object.
*
* @return string formated string which contains subject, predicate and object
*
* @api
*
* @since 0.1
*/
public function __toString()
{
$string = sprintf('s: %s, p: %s, o: %s', $this->getSubject(), $this->getPredicate(), $this->getObject());
if ($this->isQuad()) {
$string .= ', g: '.$this->getGraph();
}
return $string;
}
/**
* Checks if a given statement is equal to this instance.
*
* @param Statement $toTest statement to check this instance against
*
* @return bool true, if this instance is equal to the given instance, false otherwise
*
* @api
*
* @since 0.1
*/
public function equals(Statement $toTest)
{
if ($toTest instanceof Statement &&
$this->getSubject()->equals($toTest->getSubject()) &&
$this->getPredicate()->equals($toTest->getPredicate()) &&
$this->getObject()->equals($toTest->getObject())
) {
if ($this->isQuad() && $toTest->isQuad() && $this->getGraph()->equals($toTest->getGraph())) {
return true;
} elseif ($this->isTriple() && $toTest->isTriple()) {
return true;
}
}
return false;
}
/**
* Checks if this instance matches a given instance.
*
* @param Statement $toTest statement instance to check for a match
*
* @return bool true, if this instance matches a given Statement instance, false otherwise
*
* @api
*
* @since 0.1
*/
public function matches(Statement $toTest)
{
if ($this->isConcrete() && $this->equals($toTest)) {
return true;
}
if ($toTest instanceof Statement &&
$this->getSubject()->matches($toTest->getSubject()) &&
$this->getPredicate()->matches($toTest->getPredicate()) &&
$this->getObject()->matches($toTest->getObject())
) {
if ($this->isQuad() && $toTest->isQuad() && $this->getGraph()->matches($toTest->getGraph())) {
return true;
} elseif ($this->isQuad() && $this->getGraph()->isPattern()) {
/*
* This case also matches the default graph i.e. if the graph is set to a variable it also matches the
* defaultgraph
*/
return true;
} elseif ($this->isTriple() && $toTest->isTriple()) {
return true;
}
/*
* TODO What should happen if $this->isTriple() is true, should this pattern match any $quad?
* This is the same descission, as, if the default graph should contain the union of all graphs!
*
* As I understand the situation with SPARQL it doesn't give a descission for this, but in the case that
* named graphs are included in a query only using FROM NAMED the default graph is empty per definiton.
* {@url http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rdfDataset}
*/
}
return false;
}
}