From 671470b7e4d45c2d7cfcc5b87e99e3eeb3edf333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiemo=20M=C3=A4ttig?= Date: Wed, 15 Feb 2017 16:14:31 +0100 Subject: [PATCH] Add ItemId and PropertyId::newFromRepositoryAndNumber --- src/Entity/ItemId.php | 17 +++++++++++++++++ src/Entity/PropertyId.php | 17 +++++++++++++++++ tests/unit/Entity/ItemIdTest.php | 10 ++++++++++ tests/unit/Entity/PropertyIdTest.php | 10 ++++++++++ 4 files changed, 54 insertions(+) diff --git a/src/Entity/ItemId.php b/src/Entity/ItemId.php index 934b8ae2..e715804b 100644 --- a/src/Entity/ItemId.php +++ b/src/Entity/ItemId.php @@ -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 ] ) ); + } + } diff --git a/src/Entity/PropertyId.php b/src/Entity/PropertyId.php index 550a05fd..1ea69f46 100644 --- a/src/Entity/PropertyId.php +++ b/src/Entity/PropertyId.php @@ -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 ] ) ); + } + } diff --git a/tests/unit/Entity/ItemIdTest.php b/tests/unit/Entity/ItemIdTest.php index 573fc255..fd9e6a2c 100644 --- a/tests/unit/Entity/ItemIdTest.php +++ b/tests/unit/Entity/ItemIdTest.php @@ -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' ); + } + } diff --git a/tests/unit/Entity/PropertyIdTest.php b/tests/unit/Entity/PropertyIdTest.php index b8d059ec..6df7ea4b 100644 --- a/tests/unit/Entity/PropertyIdTest.php +++ b/tests/unit/Entity/PropertyIdTest.php @@ -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' ); + } + }