|
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
|
|
|
* @licence GNU GPL v2+ |
|
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
|
|
|
public function __construct( Deserializer $idDeserializer, Deserializer $fingerprintDeserializer ) { |
|
31
|
|
|
$this->idDeserializer = $idDeserializer; |
|
32
|
|
|
$this->fingerprintDeserializer = $fingerprintDeserializer; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $serialization |
|
37
|
|
|
* |
|
38
|
|
|
* @return Property |
|
39
|
|
|
* @throws DeserializationException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function deserialize( $serialization ) { |
|
42
|
|
|
if ( !is_array( $serialization ) ) { |
|
43
|
|
|
throw new DeserializationException( 'Item serialization should be an array' ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return new Property( |
|
47
|
|
|
$this->getPropertyId( $serialization ), |
|
48
|
|
|
$this->fingerprintDeserializer->deserialize( $serialization ), |
|
49
|
|
|
$this->getDataTypeId( $serialization ) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $serialization |
|
55
|
|
|
* |
|
56
|
|
|
* @return PropertyId|null |
|
57
|
|
|
* |
|
58
|
|
|
* @throws InvalidAttributeException |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getPropertyId( array $serialization ) { |
|
61
|
|
|
if ( array_key_exists( 'entity', $serialization ) ) { |
|
62
|
|
|
$id = $this->idDeserializer->deserialize( $serialization['entity'] ); |
|
63
|
|
|
|
|
64
|
|
|
if ( !( $id instanceof PropertyId ) ) { |
|
65
|
|
|
throw new InvalidAttributeException( |
|
66
|
|
|
'entity', |
|
67
|
|
|
$serialization['entity'], |
|
68
|
|
|
'Properties should have a property id' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $id; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $serialization |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
* |
|
83
|
|
|
* @throws MissingAttributeException |
|
84
|
|
|
* @throws InvalidAttributeException |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getDataTypeId( array $serialization ) { |
|
87
|
|
|
if ( !array_key_exists( 'datatype', $serialization ) ) { |
|
88
|
|
|
throw new MissingAttributeException( 'datatype' ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( !is_string( $serialization['datatype'] ) ) { |
|
92
|
|
|
throw new InvalidAttributeException( |
|
93
|
|
|
'datatype', |
|
94
|
|
|
$serialization['datatype'], |
|
95
|
|
|
'The datatype key should point to a string' |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $serialization['datatype']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @see DispatchableDeserializer::isDeserializerFor |
|
104
|
|
|
* |
|
105
|
|
|
* @since 2.2 |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $serialization |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
public function isDeserializerFor( $serialization ) { |
|
112
|
|
|
return is_array( $serialization ) && array_key_exists( 'datatype', $serialization ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|