1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Entity; |
4
|
|
|
|
5
|
|
|
use DataValues\DataValueObject; |
6
|
|
|
use DataValues\IllegalValueException; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Wikibase\DataModel\LegacyIdInterpreter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @since 0.5 |
12
|
|
|
* |
13
|
|
|
* @license GPL-2.0+ |
14
|
|
|
* @author Jeroen De Dauw < [email protected] > |
15
|
|
|
* @author Thiemo Mättig |
16
|
|
|
*/ |
17
|
|
|
class EntityIdValue extends DataValueObject { |
18
|
|
|
|
19
|
|
|
private $entityId; |
20
|
|
|
|
21
|
|
|
public function __construct( EntityId $entityId ) { |
22
|
|
|
$this->entityId = $entityId; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see Serializable::serialize |
27
|
|
|
* |
28
|
|
|
* @since 0.5 |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function serialize() { |
33
|
|
|
return json_encode( [ |
34
|
|
|
$this->entityId->getEntityType(), |
35
|
|
|
$this->getNumericId() |
36
|
|
|
] ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* This method gets the numeric id from the serialization. |
41
|
|
|
* It makes assumptions we do not want to make about the id format, |
42
|
|
|
* though cannot be removed until we ditch the "numeric id" part |
43
|
|
|
* from the serialization. |
44
|
|
|
* |
45
|
|
|
* @return float Numeric id as a whole number. Can not be int because of 32-bit PHP. |
46
|
|
|
*/ |
47
|
|
|
private function getNumericId() { |
48
|
|
|
return floatval( substr( $this->entityId->getSerialization(), 1 ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see Serializable::unserialize |
53
|
|
|
* |
54
|
|
|
* @since 0.5 |
55
|
|
|
* |
56
|
|
|
* @param string $serialized |
57
|
|
|
* |
58
|
|
|
* @throws IllegalValueException |
59
|
|
|
*/ |
60
|
|
|
public function unserialize( $serialized ) { |
61
|
|
|
list( $entityType, $numericId ) = json_decode( $serialized ); |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ); |
65
|
|
|
} catch ( InvalidArgumentException $ex ) { |
66
|
|
|
throw new IllegalValueException( 'Invalid EntityIdValue serialization.' ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->__construct( $entityId ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @see DataValue::getType |
74
|
|
|
* |
75
|
|
|
* @since 0.5 |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function getType() { |
80
|
|
|
return 'wikibase-entityid'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @see DataValue::getSortKey |
85
|
|
|
* |
86
|
|
|
* @since 0.5 |
87
|
|
|
* |
88
|
|
|
* @return string|float|int |
89
|
|
|
*/ |
90
|
|
|
public function getSortKey() { |
91
|
|
|
return $this->entityId->getSerialization(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @see DataValue::getValue |
96
|
|
|
* |
97
|
|
|
* @since 0.5 |
98
|
|
|
* |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
|
|
public function getValue() { |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @since 0.5 |
107
|
|
|
* |
108
|
|
|
* @return EntityId |
109
|
|
|
*/ |
110
|
|
|
public function getEntityId() { |
111
|
|
|
return $this->entityId; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @see DataValue::getArrayValue |
116
|
|
|
* |
117
|
|
|
* @since 0.5 |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getArrayValue() { |
122
|
|
|
return [ |
123
|
|
|
'entity-type' => $this->entityId->getEntityType(), |
124
|
|
|
'numeric-id' => $this->getNumericId(), |
125
|
|
|
'id' => $this->entityId->getSerialization(), |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Constructs a new instance of the DataValue from the provided data. |
131
|
|
|
* This can round-trip with |
132
|
|
|
* @see getArrayValue |
133
|
|
|
* |
134
|
|
|
* @since 0.5 |
135
|
|
|
* |
136
|
|
|
* @param mixed $data |
137
|
|
|
* |
138
|
|
|
* @throws IllegalValueException |
139
|
|
|
* @return self |
140
|
|
|
*/ |
141
|
|
|
public static function newFromArray( $data ) { |
142
|
|
|
if ( !is_array( $data ) ) { |
143
|
|
|
throw new IllegalValueException( '$data must be an array' ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ( array_key_exists( 'entity-type', $data ) && array_key_exists( 'numeric-id', $data ) ) { |
147
|
|
|
return self::newIdFromTypeAndNumber( $data['entity-type'], $data['numeric-id'] ); |
148
|
|
|
} elseif ( array_key_exists( 'id', $data ) ) { |
149
|
|
|
throw new IllegalValueException( |
150
|
|
|
'Not able to parse "id" strings, use callbacks in DataValueDeserializer instead' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
throw new IllegalValueException( 'Either "id" or "entity-type" and "numeric-id" fields required' ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $entityType |
159
|
|
|
* @param int|float|string $numericId |
160
|
|
|
* |
161
|
|
|
* @throws IllegalValueException |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
|
|
private static function newIdFromTypeAndNumber( $entityType, $numericId ) { |
165
|
|
|
try { |
166
|
|
|
return new self( LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ) ); |
167
|
|
|
} catch ( InvalidArgumentException $ex ) { |
168
|
|
|
throw new IllegalValueException( $ex->getMessage(), 0, $ex ); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|