Skip to content

Commit 8b2bf2c

Browse files
authored
Merge pull request #695 from wmde/foreignids-allow-getnumericid
Allow calling getNumericId on foreign ItemIds and PropertyIds
2 parents 269f49a + 00d2df0 commit 8b2bf2c

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)