summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/Element/ExpElement.php
blob: 1e37f10d05e8bcbd46ce0e57ebcdf3bda7f93392 (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
<?php

namespace SMW\Exporter\Element;

use RuntimeException;
use SMW\Exporter\Element;
use SMWDataItem as DataItem;

/**
 * ExpElement is a class for representing single elements that appear in
 * exported data, such as individual resources, data literals, or blank nodes.
 *
 * A single element for export, e.g. a data literal, instance name, or blank
 * node. This abstract base class declares the basic common functionality of
 * export elements (which is not much, really).
 * @note This class should not be instantiated directly.
 *
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
abstract class ExpElement implements Element {

	/**
	 * The DataItem that this export element is associated with, if
	 * any. Might be unset if not given yet.
	 *
	 * @var DataItem|null
	 */
	protected $dataItem;

	/**
	 * @since 1.6
	 *
	 * @param DataItem|null $dataItem
	 */
	public function __construct( DataItem $dataItem = null ) {
		$this->dataItem = $dataItem;
	}

	/**
	 * Get a DataItem object that represents the contents of this export
	 * element in SMW, or null if no such data item could be found.
	 *
	 * @return DataItem|null
	 */
	public function getDataItem() {
		return $this->dataItem;
	}

	/**
	 * @since 2.2
	 *
	 * @return string
	 */
	public function getHash() {
		return md5( json_encode( $this->getSerialization() ) );
	}

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

		$dataItem = null;

		if ( $this->getDataItem() !== null ) {
			$dataItem = [
				'type' => $this->getDataItem()->getDIType(),
				'item' => $this->getDataItem()->getSerialization()
			];
		}

		return [
			'dataitem' => $dataItem
		];
	}

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

		$dataItem = null;

		if ( !array_key_exists( 'dataitem', $serialization ) ) {
			throw new RuntimeException( "The serialization format is missing a dataitem element" );
		}

		// If it is null, isset will ignore it
		if ( isset( $serialization['dataitem'] ) ) {
			$dataItem = DataItem::newFromSerialization(
				$serialization['dataitem']['type'],
				$serialization['dataitem']['item']
			);
		}

		return $dataItem;
	}

	/**
	 * @since  2.2
	 *
	 * @param array $serialization
	 *
	 * @return ExpElement
	 */
	public static function newFromSerialization( array $serialization ) {

		if ( !isset( $serialization['type'] ) ) {
			throw new RuntimeException( "The serialization format is missing a type element" );
		}

		switch ( $serialization['type'] ) {
			case Element::TYPE_RESOURCE:
				$elementClass = ExpResource::class;
				break;
			case Element::TYPE_NSRESOURCE:
				$elementClass = ExpNsResource::class;
				break;
			case Element::TYPE_LITERAL:
				$elementClass = ExpLiteral::class;
				break;
			default:
				throw new RuntimeException( "Unknown type" );
		}

		$serialization['dataitem'] = self::deserialize( $serialization );

		return $elementClass::deserialize( $serialization );
	}

}