summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/Element/ExpLiteral.php
blob: f800c4820382a0eb51df1359dda0a5a34e927924 (plain)
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
<?php

namespace SMW\Exporter\Element;

use InvalidArgumentException;
use RuntimeException;
use SMWDataItem as DataItem;

/**
 * A single datatype literal for export. Defined by a literal value and a
 * datatype URI.
 *
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class ExpLiteral extends ExpElement {

	/**
	 * Lexical form of the literal.
	 * @var string
	 */
	private $lexicalForm;

	/**
	 * Datatype URI for the literal.
	 * @var string
	 */
	private $datatype;

	/**
	 * @var string
	 */
	private $lang = '';

	/**
	 * @note The given lexical form should be the plain string for
	 * representing the literal without datatype or language information.
	 * It must not use any escaping or abbreviation mechanisms.
	 *
	 * @param string $lexicalForm lexical form
	 * @param string $datatype Data type URI or empty for untyped literals
	 * @param string $lang
	 * @param DataItem|null $dataItem
	 *
	 * @throws InvalidArgumentException
	 */
	public function __construct( $lexicalForm, $datatype = '', $lang = '', DataItem $dataItem = null ) {

		if ( !is_string( $lexicalForm ) ) {
			throw new InvalidArgumentException( '$lexicalForm needs to be a string' );
		}

		if ( !is_string( $datatype ) ) {
			throw new InvalidArgumentException( '$datatype needs to be a string' );
		}

		if ( !is_string( $lang )  ) {
			throw new InvalidArgumentException( '$lang needs to be a string and $datatype has to be of langString type' );
		}

		parent::__construct( $dataItem );

		$this->lexicalForm = $lexicalForm;
		$this->datatype = $datatype;

		// 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'
		// can also be used instead of the simple Foo@lang-tag convention

		// https://www.w3.org/TR/2004/REC-rdf-concepts-20040210/#dfn-language-identifier
		// "...Plain literals have a lexical form and optionally a language tag as
		// defined by [RFC-3066], normalized to lowercase..."
		// https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
		// "...Lexical representations of language tags may be converted to
		// lower case. The value space of language tags is always in lower case..."
		$this->lang = strtolower( $lang );
	}

	/**
	 * Returns a language tag with the language tag must be well-formed according
	 * to BCP47
	 *
	 * @return string
	 */
	public function getLang() {
		return $this->lang;
	}

	/**
	 * Return the URI of the datatype used, or the empty string if untyped.
	 *
	 * @return string
	 */
	public function getDatatype() {
		return $this->datatype;
	}

	/**
	 * Return the lexical form of the literal. The result does not use
	 * any escapings and might still need to be escaped in some contexts.
	 * The lexical form is not validated or canonicalized.
	 *
	 * @return string
	 */
	public function getLexicalForm() {
		return $this->lexicalForm;
	}

	/**
	 * @since  2.2
	 *
	 * @return array
	 */
	public function getSerialization() {

		$serialization = [
			'type'     => self::TYPE_LITERAL,
			'lexical'  => $this->lexicalForm,
			'datatype' => $this->datatype,
			'lang'     => $this->lang
		];

		return $serialization + parent::getSerialization();
	}

	/**
	 * @see ExpElement::newFromSerialization
	 */
	protected static function deserialize( $serialization ) {

		if ( !isset( $serialization['lexical'] ) || !isset( $serialization['datatype'] ) || !isset( $serialization['lang'] ) ) {
			throw new RuntimeException( "Invalid format caused by a missing lexical/datatype element" );
		}

		return new self(
			$serialization['lexical'],
			$serialization['datatype'],
			$serialization['lang'],
			$serialization['dataitem']
		);
	}

}