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