1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\DispatchableDeserializer; |
7
|
|
|
use Deserializers\Exceptions\DeserializationException; |
8
|
|
|
use Wikibase\DataModel\Statement\Statement; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @licence GNU GPL v2+ |
12
|
|
|
* @author Jeroen De Dauw < [email protected] > |
13
|
|
|
* @author Thiemo Mättig |
14
|
|
|
*/ |
15
|
|
|
class StatementDeserializer implements Deserializer { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DispatchableDeserializer |
19
|
|
|
*/ |
20
|
|
|
private $legacyDeserializer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DispatchableDeserializer |
24
|
|
|
*/ |
25
|
|
|
private $currentDeserializer; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
DispatchableDeserializer $legacyDeserializer, |
29
|
|
|
DispatchableDeserializer $currentDeserializer |
30
|
|
|
) { |
31
|
|
|
$this->legacyDeserializer = $legacyDeserializer; |
32
|
|
|
$this->currentDeserializer = $currentDeserializer; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $serialization |
37
|
|
|
* |
38
|
|
|
* @return Statement |
39
|
|
|
* @throws DeserializationException |
40
|
|
|
*/ |
41
|
|
|
public function deserialize( $serialization ) { |
42
|
|
|
if ( !is_array( $serialization ) ) { |
43
|
|
|
throw new DeserializationException( 'Claim serialization must be an array' ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( $this->currentDeserializer->isDeserializerFor( $serialization ) ) { |
47
|
|
|
return $this->currentDeserializer->deserialize( $serialization ); |
48
|
|
|
} elseif ( $this->legacyDeserializer->isDeserializerFor( $serialization ) ) { |
49
|
|
|
return $this->legacyDeserializer->deserialize( $serialization ); |
50
|
|
|
} else { |
51
|
|
|
return $this->fromUnknownSerialization( $serialization ); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function fromUnknownSerialization( array $serialization ) { |
56
|
|
|
try { |
57
|
|
|
return $this->currentDeserializer->deserialize( $serialization ); |
58
|
|
|
} catch ( DeserializationException $currentEx ) { |
59
|
|
|
try { |
60
|
|
|
return $this->legacyDeserializer->deserialize( $serialization ); |
61
|
|
|
} catch ( DeserializationException $legacyEx ) { |
62
|
|
|
throw new DeserializationException( |
63
|
|
|
'The provided claim serialization is neither legacy (' |
64
|
|
|
. $legacyEx->getMessage() . ') nor current (' |
65
|
|
|
. $currentEx->getMessage() . ')' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|