TermDeserializer::getDeserialized()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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