|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @since 0.5 |
|
10
|
|
|
* |
|
11
|
|
|
* @license GPL-2.0+ |
|
12
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
13
|
|
|
*/ |
|
14
|
|
|
class ItemId extends EntityId implements Int32EntityId { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @since 0.5 |
|
18
|
|
|
*/ |
|
19
|
|
|
const PATTERN = '/^Q[1-9]\d{0,9}\z/i'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $idSerialization |
|
23
|
|
|
* @param string $repositoryName |
|
24
|
|
|
* |
|
25
|
|
|
* @throws InvalidArgumentException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct( $idSerialization, $repositoryName = '' ) { |
|
28
|
|
|
if ( !is_string( $idSerialization ) ) { |
|
29
|
|
|
throw new InvalidArgumentException( '$idSerialization must be a string' ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$parts = explode( ':', $idSerialization ); |
|
33
|
|
|
$localId = end( $parts ); |
|
34
|
|
|
$this->assertValidIdFormat( $localId ); |
|
35
|
|
|
$parts[count( $parts ) - 1] = strtoupper( $parts[count( $parts ) - 1] ); |
|
36
|
|
|
|
|
37
|
|
|
$this->serialization = implode( ':', $parts ); |
|
38
|
|
|
$this->repositoryName = $repositoryName; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function assertValidIdFormat( $localId ) { |
|
42
|
|
|
if ( !preg_match( self::PATTERN, $localId ) ) { |
|
43
|
|
|
throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ( strlen( $localId ) > 10 |
|
47
|
|
|
&& substr( $localId, 1 ) > Int32EntityId::MAX |
|
48
|
|
|
) { |
|
49
|
|
|
throw new InvalidArgumentException( '$idSerialization can not exceed ' |
|
50
|
|
|
. Int32EntityId::MAX ); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return int |
|
56
|
|
|
* |
|
57
|
|
|
* @throws RuntimeException if called on a foreign ID. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getNumericId() { |
|
60
|
|
|
if ( $this->isForeign() ) { |
|
61
|
|
|
throw new RuntimeException( 'getNumericId must not be called on foreign ItemIds' ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return (int)substr( $this->getSerialization(), 1 ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getEntityType() { |
|
71
|
|
|
return 'item'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @see Serializable::serialize |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function serialize() { |
|
80
|
|
|
$data = [ 'item', $this->serialization ]; |
|
81
|
|
|
|
|
82
|
|
|
if ( $this->isForeign() ) { |
|
83
|
|
|
$data[] = $this->repositoryName; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return json_encode( $data ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @see Serializable::unserialize |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $serialized |
|
93
|
|
|
*/ |
|
94
|
|
|
public function unserialize( $serialized ) { |
|
95
|
|
|
list( , $this->serialization, $this->repositoryName ) = array_merge( json_decode( $serialized ), [ '' ] ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Construct an ItemId given the numeric part of its serialization. |
|
100
|
|
|
* |
|
101
|
|
|
* CAUTION: new usages of this method are discouraged. Typically you |
|
102
|
|
|
* should avoid dealing with just the numeric part, and use the whole |
|
103
|
|
|
* serialization. Not doing so in new code requires special justification. |
|
104
|
|
|
* |
|
105
|
|
|
* @param int|float|string $numericId |
|
106
|
|
|
* |
|
107
|
|
|
* @return self |
|
108
|
|
|
* @throws InvalidArgumentException |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function newFromNumber( $numericId ) { |
|
111
|
|
|
if ( !is_numeric( $numericId ) ) { |
|
112
|
|
|
throw new InvalidArgumentException( '$numericId must be numeric' ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return new self( 'Q' . $numericId ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|