Passed
Push — fixTravisHHVM ( 29be00...81ae42 )
by Marius
07:29 queued 04:42
created

LegacyPropertyDeserializer::isDeserializerFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\InvalidAttributeException;
8
use Deserializers\Exceptions\MissingAttributeException;
9
use Wikibase\DataModel\Entity\Property;
10
use Wikibase\DataModel\Entity\PropertyId;
11
12
/**
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Bene* < [email protected] >
16
 */
17
class LegacyPropertyDeserializer implements Deserializer {
18
19
	/**
20
	 * @var Deserializer
21
	 */
22
	private $idDeserializer;
23
24
	/**
25
	 * @var Deserializer
26
	 */
27
	private $fingerprintDeserializer;
28
29 16
	public function __construct( Deserializer $idDeserializer, Deserializer $fingerprintDeserializer ) {
30 16
		$this->idDeserializer = $idDeserializer;
31 16
		$this->fingerprintDeserializer = $fingerprintDeserializer;
32 16
	}
33
34
	/**
35
	 * @param array $serialization
36
	 *
37
	 * @return Property
38
	 * @throws DeserializationException
39
	 */
40 16
	public function deserialize( $serialization ) {
41 16
		if ( !is_array( $serialization ) ) {
42 1
			throw new DeserializationException( 'Property serialization should be an array' );
43
		}
44
45 15
		return new Property(
46 15
			$this->getPropertyId( $serialization ),
47 13
			$this->fingerprintDeserializer->deserialize( $serialization ),
48 11
			$this->getDataTypeId( $serialization )
49
		);
50
	}
51
52
	/**
53
	 * @param array $serialization
54
	 *
55
	 * @return PropertyId|null
56
	 * @throws InvalidAttributeException
57
	 */
58 15
	private function getPropertyId( array $serialization ) {
59 15
		if ( array_key_exists( 'entity', $serialization ) ) {
60 3
			$id = $this->idDeserializer->deserialize( $serialization['entity'] );
61
62 2
			if ( !( $id instanceof PropertyId ) ) {
63 1
				throw new InvalidAttributeException(
64 1
					'entity',
65 1
					$serialization['entity'],
66 1
					'Properties should have a property id'
67
				);
68
			}
69
70 1
			return $id;
71
		}
72
73 12
		return null;
74
	}
75
76
	/**
77
	 * @param array $serialization
78
	 *
79
	 * @return string
80
	 * @throws MissingAttributeException
81
	 * @throws InvalidAttributeException
82
	 */
83 11
	private function getDataTypeId( array $serialization ) {
84 11
		if ( !array_key_exists( 'datatype', $serialization ) ) {
85 1
			throw new MissingAttributeException( 'datatype' );
86
		}
87
88 10
		if ( !is_string( $serialization['datatype'] ) ) {
89 1
			throw new InvalidAttributeException(
90 1
				'datatype',
91 1
				$serialization['datatype'],
92 1
				'The datatype key should point to a string'
93
			);
94
		}
95
96 9
		return $serialization['datatype'];
97
	}
98
99
}
100