Skip to content

Commit 00d2df0

Browse files
committed
Allow calling getNumericId on foreign ItemIds and PropertyIds
It seems that disallowing calling getNumericId on foreign ItemId and PropertyId instances (ie. containing a prefix in their serializations) was not right. Although it definitely it should not be assumed that "Q1" and "foo:Q1" refer to the same entity of type "item" and numeric part of id "1", the actual code accessing data from the database (ie. accessing numeric values from the DB) must make use of getNumericId to get the numeric part of both "Q1" and "foo:Q1". It should be the responsibility of the caller to make sure the right database is accessed and that the number part of id makes sense in the given context. ItemId/PropertyId should not really know anything about this.
1 parent 269f49a commit 00d2df0

4 files changed

Lines changed: 14 additions & 22 deletions

File tree

src/Entity/ItemId.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@ private function assertValidIdFormat( $idSerialization ) {
5151
/**
5252
* @see Int32EntityId::getNumericId
5353
*
54-
* @throws RuntimeException if called on a foreign ID.
5554
* @return int Guaranteed to be a distinct integer in the range [1..2147483647].
5655
*/
5756
public function getNumericId() {
58-
if ( $this->isForeign() ) {
59-
throw new RuntimeException( 'getNumericId must not be called on foreign ItemIds' );
60-
}
61-
62-
return (int)substr( $this->serialization, 1 );
57+
$serializationParts = self::splitSerialization( $this->serialization );
58+
return (int)substr( $serializationParts[2], 1 );
6359
}
6460

6561
/**

src/Entity/PropertyId.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@ private function assertValidIdFormat( $idSerialization ) {
5151
/**
5252
* @see Int32EntityId::getNumericId
5353
*
54-
* @throws RuntimeException if called on a foreign ID.
5554
* @return int Guaranteed to be a distinct integer in the range [1..2147483647].
5655
*/
5756
public function getNumericId() {
58-
if ( $this->isForeign() ) {
59-
throw new RuntimeException( 'getNumericId must not be called on foreign PropertyIds' );
60-
}
61-
62-
return (int)substr( $this->serialization, 1 );
57+
$serializationParts = self::splitSerialization( $this->serialization );
58+
return (int)substr( $serializationParts[2], 1 );
6359
}
6460

6561
/**

tests/unit/Entity/ItemIdTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function testGetNumericId() {
8181
$this->assertSame( 1, $id->getNumericId() );
8282
}
8383

84+
public function testGetNumericId_foreignId() {
85+
$id = new ItemId( 'foo:Q1' );
86+
$this->assertSame( 1, $id->getNumericId() );
87+
}
88+
8489
public function testGetEntityType() {
8590
$id = new ItemId( 'Q1' );
8691
$this->assertSame( 'item', $id->getEntityType() );
@@ -151,9 +156,4 @@ public function invalidNumericIdProvider() {
151156
];
152157
}
153158

154-
public function testGetNumericIdThrowsExceptionOnForeignIds() {
155-
$this->setExpectedException( RuntimeException::class );
156-
( new ItemId( 'foo:Q42' ) )->getNumericId();
157-
}
158-
159159
}

tests/unit/Entity/PropertyIdTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function testGetNumericId() {
8181
$this->assertSame( 1, $id->getNumericId() );
8282
}
8383

84+
public function testGetNumericId_foreignId() {
85+
$id = new PropertyId( 'foo:P1' );
86+
$this->assertSame( 1, $id->getNumericId() );
87+
}
88+
8489
public function testGetEntityType() {
8590
$id = new PropertyId( 'P1' );
8691
$this->assertSame( 'property', $id->getEntityType() );
@@ -151,9 +156,4 @@ public function invalidNumericIdProvider() {
151156
];
152157
}
153158

154-
public function testGetNumericIdThrowsExceptionOnForeignIds() {
155-
$this->setExpectedException( RuntimeException::class );
156-
( new PropertyId( 'foo:P42' ) )->getNumericId();
157-
}
158-
159159
}

0 commit comments

Comments
 (0)