Completed
Push — master ( 2b1f4f...6221e9 )
by
unknown
28s
created

LegacyEntityDeserializer::isDeserializerFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\DispatchableDeserializer;
7
use Deserializers\Exceptions\DeserializationException;
8
use Wikibase\DataModel\Entity\EntityDocument;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class LegacyEntityDeserializer implements DispatchableDeserializer {
15
16
	/**
17
	 * @var Deserializer
18
	 */
19
	private $itemDeserializer;
20
21
	/**
22
	 * @var Deserializer
23
	 */
24
	private $propertyDeserializer;
25
26
	public function __construct( Deserializer $itemDeserializer, Deserializer $propertyDeserializer ) {
27
		$this->itemDeserializer = $itemDeserializer;
28
		$this->propertyDeserializer = $propertyDeserializer;
29
	}
30
31
	/**
32
	 * @param mixed $serialization
33
	 *
34
	 * @return EntityDocument
35
	 * @throws DeserializationException
36
	 */
37
	public function deserialize( $serialization ) {
38
		if ( !is_array( $serialization ) ) {
39
			throw new DeserializationException( 'Entity serialization must be an array' );
40
		}
41
42
		if ( $this->isPropertySerialization( $serialization ) ) {
43
			return $this->propertyDeserializer->deserialize( $serialization );
44
		}
45
46
		return $this->itemDeserializer->deserialize( $serialization );
47
	}
48
49
	private function isPropertySerialization( $serialization ) {
50
		return array_key_exists( 'datatype', $serialization );
51
	}
52
53
	/**
54
	 * @see DispatchableDeserializer::isDeserializerFor
55
	 *
56
	 * @since 2.2
57
	 *
58
	 * @param mixed $serialization
59
	 *
60
	 * @return bool
61
	 */
62
	public function isDeserializerFor( $serialization ) {
63
		return is_array( $serialization )
64
			// This element is called 'id' in the current serialization.
65
			&& array_key_exists( 'entity', $serialization );
66
	}
67
68
}
69