1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
9
|
|
|
use Wikibase\DataModel\LegacyIdInterpreter; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser; |
11
|
|
|
use Wikibase\DataModel\Entity\EntityIdParsingException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class LegacyEntityIdDeserializer implements Deserializer { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EntityIdParser |
21
|
|
|
*/ |
22
|
|
|
private $idParser; |
23
|
|
|
|
24
|
|
|
public function __construct( EntityIdParser $idParser ) { |
25
|
|
|
$this->idParser = $idParser; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string|array $serialization |
30
|
|
|
* |
31
|
|
|
* @return EntityId |
32
|
|
|
* @throws DeserializationException |
33
|
|
|
*/ |
34
|
|
|
public function deserialize( $serialization ) { |
35
|
|
|
if ( is_string( $serialization ) ) { |
36
|
|
|
return $this->getParsedId( $serialization ); |
37
|
|
|
} |
38
|
|
|
elseif ( $this->isLegacyFormat( $serialization ) ) { |
39
|
|
|
return $this->getIdFromLegacyFormat( $serialization ); |
40
|
|
|
} |
41
|
|
|
else { |
42
|
|
|
throw new DeserializationException( 'Entity id format not recognized' ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function isLegacyFormat( $serialization ) { |
47
|
|
|
return is_array( $serialization ) && count( $serialization ) == 2 |
48
|
|
|
&& array_key_exists( 0, $serialization ) && array_key_exists( 1, $serialization ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getParsedId( $serialization ) { |
52
|
|
|
try { |
53
|
|
|
return $this->idParser->parse( $serialization ); |
54
|
|
|
} |
55
|
|
|
catch ( EntityIdParsingException $ex ) { |
56
|
|
|
throw new DeserializationException( $ex->getMessage(), $ex ); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getIdFromLegacyFormat( array $serialization ) { |
61
|
|
|
try { |
62
|
|
|
return LegacyIdInterpreter::newIdFromTypeAndNumber( $serialization[0], $serialization[1] ); |
63
|
|
|
} |
64
|
|
|
catch ( InvalidArgumentException $ex ) { |
65
|
|
|
throw new DeserializationException( $ex->getMessage(), $ex ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|