Skip to content

Commit

Permalink
Merge pull request #720 from wmde/newForeignFromNumber
Browse files Browse the repository at this point in the history
Add ItemId and PropertyId::newFromRepositoryAndNumber
  • Loading branch information
Daniel Kinzler authored Mar 3, 2017
2 parents d6a55f2 + 671470b commit c7b7b37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Entity/ItemId.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@ public static function newFromNumber( $numericId ) {
return new self( 'Q' . $numericId );
}

/**
* CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs.
*
* @param string $repositoryName
* @param int|float|string $numericId
*
* @return self
* @throws InvalidArgumentException
*/
public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) {
if ( !is_numeric( $numericId ) ) {
throw new InvalidArgumentException( '$numericId must be numeric' );
}

return new self( self::joinSerialization( [ $repositoryName, '', 'Q' . $numericId ] ) );
}

}
17 changes: 17 additions & 0 deletions src/Entity/PropertyId.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@ public static function newFromNumber( $numericId ) {
return new self( 'P' . $numericId );
}

/**
* CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs.
*
* @param string $repositoryName
* @param int|float|string $numericId
*
* @return self
* @throws InvalidArgumentException
*/
public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) {
if ( !is_numeric( $numericId ) ) {
throw new InvalidArgumentException( '$numericId must be numeric' );
}

return new self( self::joinSerialization( [ $repositoryName, '', 'P' . $numericId ] ) );
}

}
10 changes: 10 additions & 0 deletions tests/unit/Entity/ItemIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,14 @@ public function invalidNumericIdProvider() {
];
}

public function testNewFromRepositoryAndNumber() {
$id = ItemId::newFromRepositoryAndNumber( 'foo', 1 );
$this->assertSame( 'foo:Q1', $id->getSerialization() );
}

public function testNewFromRepositoryAndNumberWithInvalidNumericId() {
$this->setExpectedException( InvalidArgumentException::class );
ItemId::newFromRepositoryAndNumber( '', 'Q1' );
}

}
10 changes: 10 additions & 0 deletions tests/unit/Entity/PropertyIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,14 @@ public function invalidNumericIdProvider() {
];
}

public function testNewFromRepositoryAndNumber() {
$id = PropertyId::newFromRepositoryAndNumber( 'foo', 1 );
$this->assertSame( 'foo:P1', $id->getSerialization() );
}

public function testNewFromRepositoryAndNumberWithInvalidNumericId() {
$this->setExpectedException( InvalidArgumentException::class );
PropertyId::newFromRepositoryAndNumber( '', 'P1' );
}

}

0 comments on commit c7b7b37

Please sign in to comment.