Completed
Push — master ( 275c6f...87855a )
by Bene
26s
created

TermDeserializer::assertAttributeIsString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Wikibase\DataModel\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\Term\Term;
10
11
/**
12
 * Package private
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Addshore
16
 */
17
class TermDeserializer implements Deserializer {
18
19
	/**
20
	 * @param string[] $serialization
21
	 *
22
	 * @return Term
23
	 * @throws DeserializationException
24
	 */
25
	public function deserialize( $serialization ) {
26
		$this->assertCanDeserialize( $serialization );
27
		return $this->getDeserialized( $serialization );
28
	}
29
30
	/**
31
	 * @param string[] $serialization
32
	 *
33
	 * @return Term
34
	 */
35
	private function getDeserialized( array $serialization ) {
36
		return new Term( $serialization['language'], $serialization['value'] );
37
	}
38
39
	/**
40
	 * @param string[] $serialization
41
	 *
42
	 * @throws DeserializationException
43
	 */
44
	private function assertCanDeserialize( $serialization ) {
45
		if ( !is_array( $serialization ) ) {
46
			throw new DeserializationException( 'The term serialization should be an array' );
47
		}
48
49
		$this->requireAttribute( $serialization, 'language' );
50
		$this->requireAttribute( $serialization, 'value' );
51
		// Do not deserialize term fallbacks
52
		$this->assertNotAttribute( $serialization, 'source' );
53
54
		$this->assertAttributeIsString( $serialization, 'language' );
55
		$this->assertAttributeIsString( $serialization, 'value' );
56
	}
57
58
	private function assertAttributeIsString( array $array, $attributeName ) {
59
		if ( !is_string( $array[$attributeName] ) ) {
60
			throw new InvalidAttributeException(
61
				$attributeName,
62
				$array[$attributeName],
63
				"The internal type of attribute '$attributeName' needs to be 'string'"
64
			);
65
		}
66
	}
67
68
	/**
69
	 * @param string[] $serialization
70
	 * @param string $attribute
71
	 *
72
	 * @throws MissingAttributeException
73
	 */
74
	private function requireAttribute( $serialization, $attribute ) {
75
		if ( !is_array( $serialization ) || !array_key_exists( $attribute, $serialization ) ) {
76
			throw new MissingAttributeException( $attribute );
77
		}
78
	}
79
80
	/**
81
	 * @param string[] $array
82
	 * @param string $key
83
	 *
84
	 * @throws InvalidAttributeException
85
	 */
86
	private function assertNotAttribute( array $array, $key ) {
87
		if ( array_key_exists( $key, $array ) ) {
88
			throw new InvalidAttributeException(
89
				$key,
90
				$array[$key],
91
				'Deserialization of attribute ' . $key . ' not supported.'
92
			);
93
		}
94
	}
95
96
}
97