TermListDeserializer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 71
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deserialize() 0 7 2
A getDeserialized() 0 14 2
A assertRequestedAndActualLanguageMatch() 0 12 2
A assertAttributeIsArray() 0 9 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 Wikibase\DataModel\Term\Term;
9
use Wikibase\DataModel\Term\TermList;
10
11
/**
12
 * Package private
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Addshore
16
 * @author Bene* < [email protected] >
17
 */
18
class TermListDeserializer implements Deserializer {
19
20
	/**
21
	 * @var Deserializer
22
	 */
23
	private $termDeserializer;
24
25 28
	public function __construct( Deserializer $termDeserializer ) {
26 28
		$this->termDeserializer = $termDeserializer;
27 28
	}
28
29
	/**
30
	 * @see Deserializer::deserialize
31
	 *
32
	 * @param array[] $serialization
33
	 *
34
	 * @throws DeserializationException
35
	 * @return TermList
36
	 */
37 23
	public function deserialize( $serialization ) {
38 23
		if ( !is_array( $serialization ) ) {
39 1
			throw new DeserializationException( 'The term list serialization should be an array' );
40
		}
41
42 22
		return $this->getDeserialized( $serialization );
43
	}
44
45
	/**
46
	 * @param array[] $serialization
47
	 *
48
	 * @return TermList
49
	 */
50 22
	private function getDeserialized( array $serialization ) {
51 22
		$termList = new TermList();
52
53 22
		foreach ( $serialization as $requestedLanguage => $termSerialization ) {
54 16
			$this->assertAttributeIsArray( $serialization, $requestedLanguage );
55 15
			$this->assertRequestedAndActualLanguageMatch( $termSerialization, $requestedLanguage );
56
57
			/** @var Term $term */
58 13
			$term = $this->termDeserializer->deserialize( $termSerialization );
59 11
			$termList->setTerm( $term );
60
		}
61
62 17
		return $termList;
63
	}
64
65 15
	private function assertRequestedAndActualLanguageMatch(
66
		array $serialization,
67
		$requestedLanguage
68
	) {
69 15
		if ( strcmp( $serialization['language'], $requestedLanguage ) !== 0 ) {
70 2
			throw new DeserializationException(
71
				'Deserialization of a value of the attribute language (actual)'
72
					. ' that is not matching the language key (requested) is not supported: '
73 2
					. $serialization['language'] . ' !== ' . $requestedLanguage
74
			);
75
		}
76 13
	}
77
78 16
	private function assertAttributeIsArray( array $array, $attributeName ) {
79 16
		if ( !is_array( $array[$attributeName] ) ) {
80 1
			throw new InvalidAttributeException(
81 1
				$attributeName,
82 1
				$array[$attributeName],
83 1
				"The internal type of attribute '$attributeName' needs to be 'array'"
84
			);
85
		}
86 15
	}
87
88
}
89