summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Deserializers/ExpDataDeserializer.php
blob: bb5906db7c97ad43b7821bb39c2fa7e3f19e9ffc (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
<?php

namespace SMW\Deserializers;

use Deserializers\Deserializer;
use OutOfBoundsException;
use SMW\Exporter\Element\ExpElement;
use SMWExpData as ExpData;

/**
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author mwjames
 */
class ExpDataDeserializer implements Deserializer {

	/**
	 * @see Deserializers::deserialize
	 *
	 * @since 2.2
	 *
	 * @return ExpData
	 * @throws OutOfBoundsException
	 */
	public function deserialize( $serialization ) {

		$expData = null;

		if ( isset( $serialization['version'] ) && $serialization['version'] !== 0.1 ) {
			throw new OutOfBoundsException( 'Serializer/Deserializer version does not match, please update your data' );
		}

		if ( isset( $serialization['subject'] ) ) {
			$expData = $this->newExpData( $serialization['subject'] );
		}

		if ( !$expData instanceof ExpData ) {
			throw new OutOfBoundsException( 'ExpData could not be created probably due to an invalid subject' );
		}

		$this->doDeserialize( $serialization, $expData );

		return $expData;
	}

	private function newExpData( $subject ) {
		return new ExpData( ExpElement::newFromSerialization( $subject ) );
	}

	private function doDeserialize( $serialization, $expData ) {

		foreach ( $serialization['data'] as $data ) {

			$property = ExpElement::newFromSerialization( $data['property'] );

			foreach ( $data['children'] as $child ) {
				$expData->addPropertyObjectValue(
					$property,
					$this->doDeserializeChild( $child )
				);
			}
		}
	}

	private function doDeserializeChild( $serialization ) {

		if ( !isset( $serialization['subject'] ) ) {
			return ExpElement::newFromSerialization( $serialization );
		}

		$element = $this->newExpData( $serialization['subject'] );
		$this->doDeserialize( $serialization, $element );

		return $element;
	}

}