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
/
LiteralPatternImpl.php
152 lines (133 loc) · 3.56 KB
/
LiteralPatternImpl.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
<?php
/*
* This file is part of Saft.
*
* (c) Konrad Abicht <hi@inspirito.de>
* (c) Natanael Arndt <arndt@informatik.uni-leipzig.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Saft\Rdf;
/**
* This is a specific variable node, which is meant to match certain constructs of literal nodes by specifying
* parts of the literal.
* see also {@url http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#matchingRDFLiterals}.
*/
class LiteralPatternImpl implements Node
{
protected $value;
protected $datatype;
protected $language;
/**
* @param string $value The Literal value
* @param Node $datatype The datatype of the Literal (respectively defaults to xsd:string or rdf:langString)
* @param string $lang The language tag of the Literal (optional)
*/
public function __construct($value, $datatype, $language = null)
{
$this->value = $value;
$this->datatype = $datatype;
if (null !== $language) {
$this->language = $language;
}
}
/**
* @return bool
*/
public function isBlank()
{
return false;
}
/**
* @return bool
*/
public function isConcrete()
{
return false;
}
/**
* @return bool
*/
public function isLiteral()
{
return false;
}
/**
* @return bool
*/
public function isNamed()
{
return false;
}
/**
* @return bool
*/
public function isPattern()
{
return true;
}
/**
* @param Node $toCompare
*
* @return bool true if this instance and $toCompare match with value, datatype and language,
* false otherwise
*/
public function equals(Node $toCompare)
{
// TODO to be done
// Only compare, if given instance is a literalpattern too
if ($toCompare instanceof LiteralPatternImpl) {
return $this->getValue() === $toCompare->getValue()
&& $this->getDatatype() === $toCompare->getDatatype()
&& $this->getLanguage() === $toCompare->getLanguage();
}
return false;
}
public function getValue()
{
return $this->value;
}
public function getDatatype()
{
return $this->datatype;
}
public function getLanguage()
{
return $this->language;
}
/**
* A literal matches only another literal if its value, datatype and language are equal.
*
* @param Node $toMatch Node instance to apply the pattern on
*
* @return bool true, if this pattern matches the node, false otherwise
*
* @todo check if that could be deleted
*/
public function matches(Node $toMatch)
{
if ($toMatch->isConcrete()) {
if ($toMatch instanceof Literal) {
return $this->getValue() === $toMatch->getValue()
&& $this->getDatatype() === $toMatch->getDatatype()
&& $this->getLanguage() === $toMatch->getLanguage();
}
return false;
} else {
throw new \Exception('The node to match has to be a concrete node');
}
}
/**
* Returns a string description of the literal pattern representation.
*/
public function __toString()
{
// TODO to be done
return 'LITERALPATTERN';
}
public function toNQuads()
{
throw new \Exception('The LiteralPattern is not valid in NQuads');
}
}