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