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
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
class ItemId extends EntityId implements Int32EntityId { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @since 0.5 |
17
|
|
|
*/ |
18
|
|
|
const PATTERN = '/^Q[1-9]\d{0,9}\z/i'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $idSerialization |
22
|
33 |
|
* |
23
|
33 |
|
* @throws InvalidArgumentException |
24
|
16 |
|
*/ |
25
|
16 |
|
public function __construct( $idSerialization ) { |
26
|
|
|
$this->assertValidIdFormat( $idSerialization ); |
27
|
33 |
|
$this->serialization = strtoupper( $idSerialization ); |
28
|
33 |
|
} |
29
|
2 |
|
|
30
|
|
|
private function assertValidIdFormat( $idSerialization ) { |
31
|
|
|
if ( !is_string( $idSerialization ) ) { |
32
|
31 |
|
throw new InvalidArgumentException( '$idSerialization must be a string' ); |
33
|
15 |
|
} |
34
|
|
|
|
35
|
16 |
|
if ( !preg_match( self::PATTERN, $idSerialization ) ) { |
36
|
|
|
throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ( strlen( $idSerialization ) > 10 |
40
|
1 |
|
&& substr( $idSerialization, 1 ) > Int32EntityId::MAX |
41
|
1 |
|
) { |
42
|
|
|
throw new InvalidArgumentException( '$idSerialization can not exceed ' |
43
|
|
|
. Int32EntityId::MAX ); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
/** |
48
|
1 |
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public function getNumericId() { |
51
|
|
|
return (int)substr( $this->serialization, 1 ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
1 |
|
*/ |
57
|
1 |
|
public function getEntityType() { |
58
|
|
|
return 'item'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see Serializable::serialize |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
1 |
|
*/ |
66
|
1 |
|
public function serialize() { |
67
|
1 |
|
return json_encode( array( 'item', $this->serialization ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see Serializable::unserialize |
72
|
|
|
* |
73
|
|
|
* @param string $serialized |
74
|
|
|
*/ |
75
|
|
|
public function unserialize( $serialized ) { |
76
|
|
|
list( , $this->serialization ) = json_decode( $serialized ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Construct an ItemId given the numeric part of its serialization. |
81
|
9 |
|
* |
82
|
9 |
|
* CAUTION: new usages of this method are discouraged. Typically you |
83
|
1 |
|
* should avoid dealing with just the numeric part, and use the whole |
84
|
|
|
* serialization. Not doing so in new code requires special justification. |
85
|
|
|
* |
86
|
8 |
|
* @param int|float|string $numericId |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
public static function newFromNumber( $numericId ) { |
92
|
|
|
if ( !is_numeric( $numericId ) ) { |
93
|
|
|
throw new InvalidArgumentException( '$numericId must be numeric' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new self( 'Q' . $numericId ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|