deserializeAliasGroup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 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\AliasGroup;
10
use Wikibase\DataModel\Term\AliasGroupList;
11
12
/**
13
 * Package private
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Addshore
17
 * @author Bene* < [email protected] >
18
 */
19
class AliasGroupListDeserializer implements Deserializer {
20
21
	/**
22
	 * @see Deserializer::deserialize
23
	 *
24
	 * @param array[] $serialization
25
	 *
26
	 * @throws DeserializationException
27
	 * @return AliasGroupList
28
	 */
29 24
	public function deserialize( $serialization ) {
30 24
		if ( !is_array( $serialization ) ) {
31 1
			throw new DeserializationException( 'The aliasGroup list serialization should be an array' );
32
		}
33
34 23
		return $this->getDeserialized( $serialization );
35
	}
36
37
	/**
38
	 * @param array[] $serialization
39
	 *
40
	 * @return AliasGroupList
41
	 */
42 23
	private function getDeserialized( array $serialization ) {
43 23
		$aliasGroupList = new AliasGroupList();
44
45 23
		foreach ( $serialization as $languageCode => $aliasGroupSerialization ) {
46 17
			$this->assertAttributeIsArray( $serialization, $languageCode );
47
48 16
			$aliasGroupList->setGroup(
49 16
				$this->deserializeAliasGroup( $aliasGroupSerialization, $languageCode )
50
			);
51
		}
52
53 17
		return $aliasGroupList;
54
	}
55
56
	/**
57
	 * @param array $serialization
58
	 * @param string $languageCode
59
	 *
60
	 * @return AliasGroup
61
	 */
62 16
	private function deserializeAliasGroup( array $serialization, $languageCode ) {
63 16
		$aliases = [];
64
65 16
		foreach ( $serialization as $aliasSerialization ) {
66 15
			$this->assertIsValidAliasSerialization( $aliasSerialization, $languageCode );
67
68 10
			$aliases[] = $aliasSerialization['value'];
69
		}
70
71 11
		return new AliasGroup( (string)$languageCode, $aliases );
72
	}
73
74 15
	private function assertIsValidAliasSerialization( $serialization, $requestedLanguage ) {
75 15
		if ( !is_array( $serialization ) ) {
76 1
			throw new DeserializationException( 'Term serializations must be arrays' );
77
		}
78
79 14
		$this->requireAttribute( $serialization, 'language' );
80 13
		$this->requireAttribute( $serialization, 'value' );
81
		// Do not deserialize alias group fallbacks
82 12
		$this->assertNotAttribute( $serialization, 'source' );
83
84 11
		$this->assertAttributeInternalType( $serialization, 'language', 'string' );
85 11
		$this->assertAttributeInternalType( $serialization, 'value', 'string' );
86 11
		$this->assertRequestedAndActualLanguageMatch( $serialization, $requestedLanguage );
87 10
	}
88
89 14
	private function requireAttribute( array $array, $attributeName ) {
90 14
		if ( !array_key_exists( $attributeName, $array ) ) {
91 2
			throw new MissingAttributeException(
92 2
				$attributeName
93
			);
94
		}
95 13
	}
96
97 12
	private function assertNotAttribute( array $array, $key ) {
98 12
		if ( array_key_exists( $key, $array ) ) {
99 1
			throw new InvalidAttributeException(
100 1
				$key,
101 1
				$array[$key],
102 1
				'Deserialization of attribute ' . $key . ' not supported.'
103
			);
104
		}
105 11
	}
106
107 11
	private function assertRequestedAndActualLanguageMatch(
108
		array $serialization,
109
		$requestedLanguage
110
	) {
111 11
		if ( strcmp( $serialization['language'], $requestedLanguage ) !== 0 ) {
112 1
			throw new DeserializationException(
113
				'Deserialization of a value of the attribute language (actual)'
114
					. ' that is not matching the language key (requested) is not supported: '
115 1
					. $serialization['language'] . ' !== ' . $requestedLanguage
116
			);
117
		}
118 10
	}
119
120 17
	private function assertAttributeIsArray( array $array, $attributeName ) {
121 17
		$this->assertAttributeInternalType( $array, $attributeName, 'array' );
122 16
	}
123
124 17
	private function assertAttributeInternalType( array $array, $attributeName, $internalType ) {
125 17
		if ( gettype( $array[$attributeName] ) !== $internalType ) {
126 1
			throw new InvalidAttributeException(
127 1
				$attributeName,
128 1
				$array[$attributeName],
129 1
				"The internal type of attribute '$attributeName' needs to be '$internalType'"
130
			);
131
		}
132 16
	}
133
134
}
135