Skip to content

Commit effa40e

Browse files
author
Daniel Kinzler
committed
Merge pull request #671 from wmde/entityIdJson2
Serialize EntityIdValue with "id" without newFromArray support
2 parents 403c110 + 739a09b commit effa40e

2 files changed

Lines changed: 49 additions & 26 deletions

File tree

src/Entity/EntityIdValue.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
* @license GPL-2.0+
1414
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
15+
* @author Thiemo Mättig
1516
*/
1617
class EntityIdValue extends DataValueObject {
1718

@@ -43,7 +44,7 @@ public function serialize() {
4344
*
4445
* @return float Numeric id as a whole number. Can not be int because of 32-bit PHP.
4546
*/
46-
protected function getNumericId() {
47+
private function getNumericId() {
4748
return floatval( substr( $this->entityId->getSerialization(), 1 ) );
4849
}
4950

@@ -121,6 +122,7 @@ public function getArrayValue() {
121122
return array(
122123
'entity-type' => $this->entityId->getEntityType(),
123124
'numeric-id' => $this->getNumericId(),
125+
'id' => $this->entityId->getSerialization(),
124126
);
125127
}
126128

@@ -138,27 +140,33 @@ public function getArrayValue() {
138140
*/
139141
public static function newFromArray( $data ) {
140142
if ( !is_array( $data ) ) {
141-
throw new IllegalValueException( '$data must be an array; got ' . gettype( $data ) );
143+
throw new IllegalValueException( '$data must be an array' );
142144
}
143145

144-
if ( !array_key_exists( 'entity-type', $data ) ) {
145-
throw new IllegalValueException( "'entity-type' field required" );
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+
);
146152
}
147153

148-
if ( !array_key_exists( 'numeric-id', $data ) ) {
149-
throw new IllegalValueException( "'numeric-id' field required" );
150-
}
154+
throw new IllegalValueException( 'Either "id" or "entity-type" and "numeric-id" fields required' );
155+
}
151156

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 ) {
152165
try {
153-
$id = LegacyIdInterpreter::newIdFromTypeAndNumber(
154-
$data['entity-type'],
155-
$data['numeric-id']
156-
);
166+
return new self( LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ) );
157167
} catch ( InvalidArgumentException $ex ) {
158168
throw new IllegalValueException( $ex->getMessage(), 0, $ex );
159169
}
160-
161-
return new static( $id );
162170
}
163171

164172
}

tests/unit/Entity/EntityIdValueTest.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wikibase\DataModel\Tests\Entity;
44

5+
use PHPUnit_Framework_TestCase;
56
use Wikibase\DataModel\Entity\EntityIdValue;
67
use Wikibase\DataModel\Entity\ItemId;
78
use Wikibase\DataModel\Entity\PropertyId;
@@ -15,8 +16,9 @@
1516
*
1617
* @license GPL-2.0+
1718
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
19+
* @author Thiemo Mättig
1820
*/
19-
class EntityIdValueTest extends \PHPUnit_Framework_TestCase {
21+
class EntityIdValueTest extends PHPUnit_Framework_TestCase {
2022

2123
public function testCanConstruct() {
2224
$entityId = new ItemId( 'Q123' );
@@ -102,28 +104,37 @@ public function testDeserializationCompatibility() {
102104
public function testGetArrayValueCompatibility() {
103105
$id = new EntityIdValue( new ItemId( 'Q31337' ) );
104106

105-
$this->assertEquals(
107+
$this->assertSame(
106108
// This is the serialization format from when the EntityIdValue was still together with EntityId.
107109
array(
108110
'entity-type' => 'item',
109-
'numeric-id' => 31337,
111+
'numeric-id' => (float)31337,
112+
'id' => 'Q31337',
110113
),
111114
$id->getArrayValue()
112115
);
113116
}
114117

115-
public function testNewFromArrayCompatibility() {
118+
/**
119+
* @dataProvider validArrayProvider
120+
*/
121+
public function testNewFromArrayCompatibility( array $array ) {
116122
$id = new EntityIdValue( new ItemId( 'Q31337' ) );
117123

118-
$this->assertEquals(
119-
$id,
120-
EntityIdValue::newFromArray(
121-
// This is the serialization format from when the EntityIdValue was still together with EntityId.
122-
array(
123-
'entity-type' => 'item',
124-
'numeric-id' => 31337,
125-
)
126-
)
124+
$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) );
125+
}
126+
127+
public function validArrayProvider() {
128+
return array(
129+
'Legacy format' => array( array(
130+
'entity-type' => 'item',
131+
'numeric-id' => 31337,
132+
) ),
133+
'Maximum compatibility' => array( array(
134+
'entity-type' => 'item',
135+
'numeric-id' => 31337,
136+
'id' => 'Q31337',
137+
) ),
127138
);
128139
}
129140

@@ -144,6 +155,10 @@ public function invalidArrayProvider() {
144155

145156
array( array() ),
146157

158+
'newFromArray can not deserialize' => array( array(
159+
'id' => 'Q42',
160+
) ),
161+
147162
array( array(
148163
'entity-type' => 'item',
149164
) ),

0 commit comments

Comments
 (0)