|
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+ |
|
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
|
|
|
/** |
|
26
|
|
|
* @param Deserializer $termDeserializer |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct( Deserializer $termDeserializer ) { |
|
29
|
|
|
$this->termDeserializer = $termDeserializer; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @see Deserializer::deserialize |
|
34
|
|
|
* |
|
35
|
|
|
* @param array[] $serialization |
|
36
|
|
|
* |
|
37
|
|
|
* @throws DeserializationException |
|
38
|
|
|
* @return TermList |
|
39
|
|
|
*/ |
|
40
|
|
|
public function deserialize( $serialization ) { |
|
41
|
|
|
if ( !is_array( $serialization ) ) { |
|
42
|
|
|
throw new DeserializationException( 'The term list serialization should be an array' ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->getDeserialized( $serialization ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array[] $serialization |
|
50
|
|
|
* |
|
51
|
|
|
* @return TermList |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getDeserialized( array $serialization ) { |
|
54
|
|
|
$termList = new TermList(); |
|
55
|
|
|
|
|
56
|
|
|
foreach ( $serialization as $requestedLanguage => $termSerialization ) { |
|
57
|
|
|
$this->assertAttributeIsArray( $serialization, $requestedLanguage ); |
|
58
|
|
|
$this->assertRequestedAndActualLanguageMatch( $termSerialization, $requestedLanguage ); |
|
59
|
|
|
|
|
60
|
|
|
/** @var Term $term */ |
|
61
|
|
|
$term = $this->termDeserializer->deserialize( $termSerialization ); |
|
62
|
|
|
$termList->setTerm( $term ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $termList; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function assertRequestedAndActualLanguageMatch( |
|
69
|
|
|
array $serialization, |
|
70
|
|
|
$requestedLanguage |
|
71
|
|
|
) { |
|
72
|
|
|
if ( $serialization['language'] !== $requestedLanguage ) { |
|
73
|
|
|
throw new DeserializationException( |
|
74
|
|
|
'Deserialization of a value of the attribute language (actual)' |
|
75
|
|
|
. ' that is not matching the language key (requested) is not supported: ' |
|
76
|
|
|
. $serialization['language'] . ' !== ' . $requestedLanguage |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function assertAttributeIsArray( array $array, $attributeName ) { |
|
82
|
|
|
if ( !is_array( $array[$attributeName] ) ) { |
|
83
|
|
|
throw new InvalidAttributeException( |
|
84
|
|
|
$attributeName, |
|
85
|
|
|
$array[$attributeName], |
|
86
|
|
|
"The internal type of attribute '$attributeName' needs to be 'array'" |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|