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