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
/
LiteralImpl.php
117 lines (101 loc) · 3.08 KB
/
LiteralImpl.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
<?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;
class LiteralImpl extends AbstractLiteral
{
/**
* @var string
*/
protected static $xsdString = 'http://www.w3.org/2001/XMLSchema#string';
/**
* @var string
*/
protected static $rdfLangString = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
/**
* @var string
*/
protected $value;
/**
* @var Node
*/
protected $datatype = null;
/**
* @var string
*/
protected $lang = null;
/**
* @param string $value The Literal value
* @param NamedNode $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, NamedNode $datatype = null, $lang = null)
{
if ($value === null) {
throw new \Exception('Literal value can\'t be null. Please use AnyPattern if you need a variable.');
} elseif (!is_string($value)) {
throw new \Exception('The literal value has to be of type string');
}
$this->value = $value;
if ($lang !== null) {
$this->lang = (string) $lang;
}
if (
$lang !== null &&
$lang !== '' &&
$datatype !== null &&
$datatype->isNamed() &&
$datatype->getUri() !== self::$rdfLangString
) {
throw new \Exception('Language tagged Literals must have <'.self::$rdfLangString.'> datatype.');
}
if (
($lang === null || $lang == '') &&
$datatype !== null &&
$datatype->isNamed() &&
$datatype->getUri() === self::$rdfLangString
) {
throw new \Exception('No or empty Language Tag for Literals with <'.self::$rdfLangString.'> datatype.');
}
if ($datatype !== null) {
$this->datatype = $datatype;
} elseif ($lang !== null) {
$this->datatype = new NamedNodeImpl(self::$rdfLangString);
} else {
$this->datatype = new NamedNodeImpl(self::$xsdString);
}
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Get the datatype of the Literal. It can be one of the XML Schema datatypes (XSD) or anything else. If the URI is
* needed it can be retrieved by calling ->getDatatype()->getUri().
*
* An overview about all XML Schema datatypes: {@url http://www.w3.org/TR/xmlschema-2/#built-in-datatypes}
*
* @return Node the datatype of the Literal as named node
*/
public function getDatatype()
{
return $this->datatype;
}
/**
* @return string|null
*/
public function getLanguage()
{
return $this->lang;
}
}