Skip to content

Commit 614ebfb

Browse files
committed
Rework native EntityId(Value) serializations
1 parent 05bfe15 commit 614ebfb

7 files changed

Lines changed: 24 additions & 17 deletions

File tree

src/Entity/EntityIdValue.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public function __construct( EntityId $entityId ) {
3030
* @return string
3131
*/
3232
public function serialize() {
33-
return json_encode( [
34-
$this->entityId->getEntityType(),
35-
$this->getNumericId()
36-
] );
33+
return serialize( $this->entityId );
3734
}
3835

3936
/**
@@ -58,7 +55,14 @@ private function getNumericId() {
5855
* @throws IllegalValueException
5956
*/
6057
public function unserialize( $serialized ) {
61-
list( $entityType, $numericId ) = json_decode( $serialized );
58+
$array = json_decode( $serialized );
59+
60+
if ( !is_array( $array ) ) {
61+
$this->entityId = unserialize( $serialized );
62+
return;
63+
}
64+
65+
list( $entityType, $numericId ) = $array;
6266

6367
try {
6468
$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );

src/Entity/ItemId.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getEntityType() {
7070
* @return string
7171
*/
7272
public function serialize() {
73-
return json_encode( [ 'item', $this->serialization ] );
73+
return $this->serialization;
7474
}
7575

7676
/**
@@ -79,7 +79,8 @@ public function serialize() {
7979
* @param string $serialized
8080
*/
8181
public function unserialize( $serialized ) {
82-
list( , $this->serialization ) = json_decode( $serialized );
82+
$array = json_decode( $serialized );
83+
$this->serialization = is_array( $array ) ? $array[1] : $serialized;
8384
}
8485

8586
/**

src/Entity/PropertyId.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getEntityType() {
7070
* @return string
7171
*/
7272
public function serialize() {
73-
return json_encode( [ 'property', $this->serialization ] );
73+
return $this->serialization;
7474
}
7575

7676
/**
@@ -79,7 +79,8 @@ public function serialize() {
7979
* @param string $serialized
8080
*/
8181
public function unserialize( $serialized ) {
82-
list( , $this->serialization ) = json_decode( $serialized );
82+
$array = json_decode( $serialized );
83+
$this->serialization = is_array( $array ) ? $array[1] : $serialized;
8384
}
8485

8586
/**

tests/unit/Entity/EntityIdTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public function testDeserializationCompatibility() {
6969
* It is just here to catch unintentional changes.
7070
*/
7171
public function testSerializationStability() {
72-
$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}';
72+
$serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":4:{Q123}';
7373
$id = new ItemId( 'q123' );
7474

7575
$this->assertEquals(
7676
serialize( $id ),
77-
$v05serialization
77+
$serialization
7878
);
7979
}
8080

tests/unit/Entity/EntityIdValueTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public function testGetArrayValueRoundtrip( EntityIdValue $id ) {
8787
public function testSerializationCompatibility() {
8888
$id = new EntityIdValue( new ItemId( 'Q31337' ) );
8989

90-
// This is the serialization format from when the EntityIdValue was still together with EntityId.
91-
$this->assertEquals( '["item",31337]', $id->serialize() );
90+
$this->assertEquals( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
9291
}
9392

9493
public function testDeserializationCompatibility() {

tests/unit/Entity/ItemIdTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testGetEntityType() {
9292

9393
public function testSerialize() {
9494
$id = new ItemId( 'Q1' );
95-
$this->assertSame( '["item","Q1"]', $id->serialize() );
95+
$this->assertSame( 'Q1', $id->serialize() );
9696
}
9797

9898
/**
@@ -106,6 +106,7 @@ public function testUnserialize( $json, $expected ) {
106106

107107
public function serializationProvider() {
108108
return [
109+
[ 'Q2', 'Q2' ],
109110
[ '["item","Q2"]', 'Q2' ],
110111

111112
// All these cases are kind of an injection vector and allow constructing invalid ids.
@@ -114,7 +115,7 @@ public function serializationProvider() {
114115
[ '["",""]', '' ],
115116
[ '["",2]', 2 ],
116117
[ '["",null]', null ],
117-
[ '', null ],
118+
[ '', '' ],
118119
];
119120
}
120121

tests/unit/Entity/PropertyIdTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testGetEntityType() {
9292

9393
public function testSerialize() {
9494
$id = new PropertyId( 'P1' );
95-
$this->assertSame( '["property","P1"]', $id->serialize() );
95+
$this->assertSame( 'P1', $id->serialize() );
9696
}
9797

9898
/**
@@ -106,6 +106,7 @@ public function testUnserialize( $json, $expected ) {
106106

107107
public function serializationProvider() {
108108
return [
109+
[ 'P2', 'P2' ],
109110
[ '["property","P2"]', 'P2' ],
110111

111112
// All these cases are kind of an injection vector and allow constructing invalid ids.
@@ -114,7 +115,7 @@ public function serializationProvider() {
114115
[ '["",""]', '' ],
115116
[ '["",2]', 2 ],
116117
[ '["",null]', null ],
117-
[ '', null ],
118+
[ '', '' ],
118119
];
119120
}
120121

0 commit comments

Comments
 (0)