Skip to content

Commit 671470b

Browse files
committed
Add ItemId and PropertyId::newFromRepositoryAndNumber
1 parent d6a55f2 commit 671470b

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/Entity/ItemId.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,21 @@ public static function newFromNumber( $numericId ) {
103103
return new self( 'Q' . $numericId );
104104
}
105105

106+
/**
107+
* CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs.
108+
*
109+
* @param string $repositoryName
110+
* @param int|float|string $numericId
111+
*
112+
* @return self
113+
* @throws InvalidArgumentException
114+
*/
115+
public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) {
116+
if ( !is_numeric( $numericId ) ) {
117+
throw new InvalidArgumentException( '$numericId must be numeric' );
118+
}
119+
120+
return new self( self::joinSerialization( [ $repositoryName, '', 'Q' . $numericId ] ) );
121+
}
122+
106123
}

src/Entity/PropertyId.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,21 @@ public static function newFromNumber( $numericId ) {
103103
return new self( 'P' . $numericId );
104104
}
105105

106+
/**
107+
* CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs.
108+
*
109+
* @param string $repositoryName
110+
* @param int|float|string $numericId
111+
*
112+
* @return self
113+
* @throws InvalidArgumentException
114+
*/
115+
public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) {
116+
if ( !is_numeric( $numericId ) ) {
117+
throw new InvalidArgumentException( '$numericId must be numeric' );
118+
}
119+
120+
return new self( self::joinSerialization( [ $repositoryName, '', 'P' . $numericId ] ) );
121+
}
122+
106123
}

tests/unit/Entity/ItemIdTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,14 @@ public function invalidNumericIdProvider() {
156156
];
157157
}
158158

159+
public function testNewFromRepositoryAndNumber() {
160+
$id = ItemId::newFromRepositoryAndNumber( 'foo', 1 );
161+
$this->assertSame( 'foo:Q1', $id->getSerialization() );
162+
}
163+
164+
public function testNewFromRepositoryAndNumberWithInvalidNumericId() {
165+
$this->setExpectedException( InvalidArgumentException::class );
166+
ItemId::newFromRepositoryAndNumber( '', 'Q1' );
167+
}
168+
159169
}

tests/unit/Entity/PropertyIdTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,14 @@ public function invalidNumericIdProvider() {
156156
];
157157
}
158158

159+
public function testNewFromRepositoryAndNumber() {
160+
$id = PropertyId::newFromRepositoryAndNumber( 'foo', 1 );
161+
$this->assertSame( 'foo:P1', $id->getSerialization() );
162+
}
163+
164+
public function testNewFromRepositoryAndNumberWithInvalidNumericId() {
165+
$this->setExpectedException( InvalidArgumentException::class );
166+
PropertyId::newFromRepositoryAndNumber( '', 'P1' );
167+
}
168+
159169
}

0 commit comments

Comments
 (0)