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+ |
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
|
23 |
|
public function deserialize( $serialization ) { |
30
|
23 |
|
if ( !is_array( $serialization ) ) { |
31
|
1 |
|
throw new DeserializationException( 'The aliasGroup list serialization should be an array' ); |
32
|
|
|
} |
33
|
|
|
|
34
|
22 |
|
return $this->getDeserialized( $serialization ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array[] $serialization |
39
|
|
|
* |
40
|
|
|
* @return AliasGroupList |
41
|
|
|
*/ |
42
|
22 |
|
private function getDeserialized( array $serialization ) { |
43
|
22 |
|
$aliasGroupList = new AliasGroupList(); |
44
|
|
|
|
45
|
22 |
|
foreach ( $serialization as $languageCode => $aliasGroupSerialization ) { |
46
|
16 |
|
$this->assertAttributeIsArray( $serialization, $languageCode ); |
47
|
|
|
|
48
|
15 |
|
$aliasGroupList->setGroup( $this->deserializeAliasGroup( $aliasGroupSerialization, $languageCode ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
16 |
|
return $aliasGroupList; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $serialization |
56
|
|
|
* @param string $languageCode |
57
|
|
|
* |
58
|
|
|
* @return AliasGroup |
59
|
|
|
*/ |
60
|
15 |
|
private function deserializeAliasGroup( array $serialization, $languageCode ) { |
61
|
15 |
|
$aliases = array(); |
62
|
|
|
|
63
|
15 |
|
foreach ( $serialization as $aliasSerialization ) { |
64
|
14 |
|
$this->assertIsValidAliasSerialization( $aliasSerialization, $languageCode ); |
65
|
|
|
|
66
|
9 |
|
$aliases[] = $aliasSerialization['value']; |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
return new AliasGroup( |
70
|
|
|
$languageCode, |
71
|
10 |
|
$aliases |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
14 |
|
private function assertIsValidAliasSerialization( $serialization, $requestedLanguage ) { |
76
|
14 |
|
if ( !is_array( $serialization ) ) { |
77
|
1 |
|
throw new DeserializationException( 'Term serializations must be arrays' ); |
78
|
|
|
} |
79
|
|
|
|
80
|
13 |
|
$this->requireAttribute( $serialization, 'language' ); |
81
|
12 |
|
$this->requireAttribute( $serialization, 'value' ); |
82
|
|
|
// Do not deserialize alias group fallbacks |
83
|
11 |
|
$this->assertNotAttribute( $serialization, 'source' ); |
84
|
|
|
|
85
|
10 |
|
$this->assertAttributeInternalType( $serialization, 'language', 'string' ); |
86
|
10 |
|
$this->assertAttributeInternalType( $serialization, 'value', 'string' ); |
87
|
10 |
|
$this->assertRequestedAndActualLanguageMatch( $serialization, $requestedLanguage ); |
88
|
9 |
|
} |
89
|
|
|
|
90
|
13 |
|
private function requireAttribute( array $array, $attributeName ) { |
91
|
13 |
|
if ( !array_key_exists( $attributeName, $array ) ) { |
92
|
2 |
|
throw new MissingAttributeException( |
93
|
2 |
|
$attributeName |
94
|
|
|
); |
95
|
|
|
} |
96
|
12 |
|
} |
97
|
|
|
|
98
|
11 |
|
private function assertNotAttribute( array $array, $key ) { |
99
|
11 |
|
if ( array_key_exists( $key, $array ) ) { |
100
|
1 |
|
throw new InvalidAttributeException( |
101
|
|
|
$key, |
102
|
1 |
|
$array[$key], |
103
|
1 |
|
'Deserialization of attribute ' . $key . ' not supported.' |
104
|
|
|
); |
105
|
|
|
} |
106
|
10 |
|
} |
107
|
|
|
|
108
|
10 |
|
private function assertRequestedAndActualLanguageMatch( |
109
|
|
|
array $serialization, |
110
|
|
|
$requestedLanguage |
111
|
|
|
) { |
112
|
10 |
|
if ( $serialization['language'] !== $requestedLanguage ) { |
113
|
1 |
|
throw new DeserializationException( |
114
|
|
|
'Deserialization of a value of the attribute language (actual)' |
115
|
|
|
. ' that is not matching the language key (requested) is not supported: ' |
116
|
1 |
|
. $serialization['language'] . ' !== ' . $requestedLanguage |
117
|
|
|
); |
118
|
|
|
} |
119
|
9 |
|
} |
120
|
|
|
|
121
|
16 |
|
private function assertAttributeIsArray( array $array, $attributeName ) { |
122
|
16 |
|
$this->assertAttributeInternalType( $array, $attributeName, 'array' ); |
123
|
15 |
|
} |
124
|
|
|
|
125
|
16 |
|
private function assertAttributeInternalType( array $array, $attributeName, $internalType ) { |
126
|
16 |
|
if ( gettype( $array[$attributeName] ) !== $internalType ) { |
127
|
1 |
|
throw new InvalidAttributeException( |
128
|
|
|
$attributeName, |
129
|
1 |
|
$array[$attributeName], |
130
|
1 |
|
"The internal type of attribute '$attributeName' needs to be '$internalType'" |
131
|
|
|
); |
132
|
|
|
} |
133
|
15 |
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|