1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\DispatchableDeserializer; |
7
|
|
|
use Deserializers\Exceptions\DeserializationException; |
8
|
|
|
use Deserializers\Exceptions\InvalidAttributeException; |
9
|
|
|
use Deserializers\Exceptions\MissingAttributeException; |
10
|
|
|
use Wikibase\DataModel\Entity\Property; |
11
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @license GPL-2.0-or-later |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
* @author Bene* < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class LegacyPropertyDeserializer implements DispatchableDeserializer { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Deserializer |
22
|
|
|
*/ |
23
|
|
|
private $idDeserializer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Deserializer |
27
|
|
|
*/ |
28
|
|
|
private $fingerprintDeserializer; |
29
|
|
|
|
30
|
16 |
|
public function __construct( Deserializer $idDeserializer, Deserializer $fingerprintDeserializer ) { |
31
|
16 |
|
$this->idDeserializer = $idDeserializer; |
32
|
16 |
|
$this->fingerprintDeserializer = $fingerprintDeserializer; |
33
|
16 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $serialization |
37
|
|
|
* |
38
|
|
|
* @return Property |
39
|
|
|
* @throws DeserializationException |
40
|
|
|
*/ |
41
|
16 |
|
public function deserialize( $serialization ) { |
42
|
16 |
|
if ( !is_array( $serialization ) ) { |
43
|
1 |
|
throw new DeserializationException( 'Property serialization should be an array' ); |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
return new Property( |
47
|
15 |
|
$this->getPropertyId( $serialization ), |
48
|
13 |
|
$this->fingerprintDeserializer->deserialize( $serialization ), |
49
|
11 |
|
$this->getDataTypeId( $serialization ) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $serialization |
55
|
|
|
* |
56
|
|
|
* @return PropertyId|null |
57
|
|
|
* @throws InvalidAttributeException |
58
|
|
|
*/ |
59
|
15 |
|
private function getPropertyId( array $serialization ) { |
60
|
15 |
|
if ( array_key_exists( 'entity', $serialization ) ) { |
61
|
3 |
|
$id = $this->idDeserializer->deserialize( $serialization['entity'] ); |
62
|
|
|
|
63
|
2 |
|
if ( !( $id instanceof PropertyId ) ) { |
64
|
1 |
|
throw new InvalidAttributeException( |
65
|
1 |
|
'entity', |
66
|
1 |
|
$serialization['entity'], |
67
|
1 |
|
'Properties should have a property id' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $id; |
72
|
|
|
} |
73
|
|
|
|
74
|
12 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $serialization |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
* @throws MissingAttributeException |
82
|
|
|
* @throws InvalidAttributeException |
83
|
|
|
*/ |
84
|
11 |
|
private function getDataTypeId( array $serialization ) { |
85
|
11 |
|
if ( !array_key_exists( 'datatype', $serialization ) ) { |
86
|
1 |
|
throw new MissingAttributeException( 'datatype' ); |
87
|
|
|
} |
88
|
|
|
|
89
|
10 |
|
if ( !is_string( $serialization['datatype'] ) ) { |
90
|
1 |
|
throw new InvalidAttributeException( |
91
|
1 |
|
'datatype', |
92
|
1 |
|
$serialization['datatype'], |
93
|
1 |
|
'The datatype key should point to a string' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
9 |
|
return $serialization['datatype']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @see DispatchableDeserializer::isDeserializerFor |
102
|
|
|
* |
103
|
|
|
* @since 2.2 |
104
|
|
|
* |
105
|
|
|
* @param mixed $serialization |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isDeserializerFor( $serialization ) { |
110
|
|
|
return is_array( $serialization ) |
111
|
|
|
// This element is called 'id' in the current serialization. |
112
|
|
|
&& array_key_exists( 'entity', $serialization ) |
113
|
|
|
&& array_key_exists( 'datatype', $serialization ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|