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