summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/Element/ExpNsResource.php
blob: 18e7f174054111a265cb3ee9e1a80bd7cf80adae (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
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

namespace SMW\Exporter\Element;

use InvalidArgumentException;
use RuntimeException;
use SMWDataItem as DataItem;

/**
 * A single resource (individual) for export, defined by a URI for which there
 * also is a namespace abbreviation.
 *
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class ExpNsResource extends ExpResource {

	/**
	 * Local part of the abbreviated URI
	 * @var string
	 */
	private $localName;

	/**
	 * Namespace URI prefix of the abbreviated URI
	 * @var string
	 */
	private $namespace;

	/**
	 * Namespace abbreviation of the abbreviated URI
	 * @var string
	 */
	private $namespaceId;

	/**
	 * @note The given URI must not contain serialization-specific
	 * abbreviations or escapings, such as XML entities.
	 *
	 * @param string $localName Local part of the abbreviated URI
	 * @param string $namespace Namespace URI prefix of the abbreviated URI
	 * @param string $namespaceId Namespace abbreviation of the abbreviated URI
	 * @param DataItem|null $dataItem
	 *
	 * @throws InvalidArgumentException
	 */
	public function __construct( $localName, $namespace, $namespaceId, DataItem $dataItem = null ) {

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

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

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

		parent::__construct( $namespace . $localName, $dataItem );

		$this->localName = $localName;
		$this->namespace = $namespace;
		$this->namespaceId = $namespaceId;
	}

	/**
	 * Return a qualified name for the element.
	 *
	 * @return string
	 */
	public function getQName() {
		return $this->namespaceId . ':' . $this->localName;
	}

	/**
	 * Get the namespace identifier used (the part before :).
	 *
	 * @return string
	 */
	public function getNamespaceId() {
		return $this->namespaceId;
	}

	/**
	 * Get the namespace URI that is used in the abbreviation.
	 *
	 * @return string
	 */
	public function getNamespace() {
		return $this->namespace;
	}

	/**
	 * Get the local name (the part after :).
	 *
	 * @return string
	 */
	public function getLocalName() {
		return $this->localName;
	}

	/**
	 * Check if the local name is qualifies as a local name in XML and
	 * Turtle. The function returns true if this is surely the case, and
	 * false if it may not be the case. However, we do not check the whole
	 * range of allowed Unicode entities for performance reasons.
	 *
	 * @return boolean
	 */
	public function hasAllowedLocalName() {
		return preg_match( '/^[A-Za-z_][-A-Za-z_0-9]*$/u', $this->localName );
	}

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

		// Use '|' as divider as it is unlikely that symbol appears within a uri
		$serialization = [
			'type' => self::TYPE_NSRESOURCE,
			'uri'  => $this->localName . '|' . $this->namespace . '|' . $this->namespaceId
		];

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

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

		if ( !isset( $serialization['uri'] ) ) {
			throw new RuntimeException( "Invalid serialization format, missing a uri element" );
		}

		if ( substr_count( $serialization['uri'], '|') < 2 ) {
			throw new RuntimeException( "Invalid uri format, expected two '|' dividers" );
		}

		list( $localName, $namespace, $namespaceId ) = explode( '|', $serialization['uri'], 3 );

		return new self(
			$localName,
			$namespace,
			$namespaceId,
			$serialization['dataitem']
		);
	}

}