Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Wikibase DataModel release notes

## Version 4.4.0 (2015-09-03)

* Added `ItemIdParser`

## Version 4.3.0 (2015-09-02)

* Added `isEmpty` to `EntityDocument`
Expand Down
2 changes: 1 addition & 1 deletion WikibaseDataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
return 1;
}

define( 'WIKIBASE_DATAMODEL_VERSION', '4.3.0' );
define( 'WIKIBASE_DATAMODEL_VERSION', '4.4.0' );

if ( defined( 'MEDIAWIKI' ) ) {
call_user_func( function() {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.3.x-dev"
"dev-master": "4.4.x-dev"
}
},
"scripts": {
Expand Down
33 changes: 33 additions & 0 deletions src/Entity/ItemIdParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Wikibase\DataModel\Entity;

use InvalidArgumentException;

/**
* A trivial EntityIdParser that only parses the serializations of ItemIds. This is particularly
* useful in cases where URIs are used to refer to concepts in an external Wikibase repository,
* e.g. when referencing globes in coordinate values, or units in quantity values.
*
* @since 4.4
*
* @licence GNU GPL v2+
* @author Thiemo Mättig
*/
class ItemIdParser implements EntityIdParser {

/**
* @param string $idSerialization
*
* @throws EntityIdParsingException
* @return ItemId
*/
public function parse( $idSerialization ) {
try {
return new ItemId( $idSerialization );
} catch ( InvalidArgumentException $ex ) {
throw new EntityIdParsingException( $ex->getMessage() );
}
}

}
58 changes: 58 additions & 0 deletions tests/unit/Entity/ItemIdParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Wikibase\DataModel\Tests\Entity;

use PHPUnit_Framework_TestCase;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\ItemIdParser;

/**
* @covers Wikibase\DataModel\Entity\ItemIdParser
*
* @licence GNU GPL v2+
* @author Thiemo Mättig
*/
class ItemIdParserTest extends PHPUnit_Framework_TestCase {

/**
* @dataProvider entityIdProvider
*/
public function testCanParseEntityId( $idString, ItemId $expected ) {
$parser = new ItemIdParser();
$actual = $parser->parse( $idString );

$this->assertEquals( $actual, $expected );
}

public function entityIdProvider() {
return array(
array( 'q42', new ItemId( 'Q42' ) ),
array( 'Q1337', new ItemId( 'Q1337' ) ),
);
}

/**
* @dataProvider invalidIdSerializationProvider
*/
public function testCannotParseInvalidId( $invalidIdSerialization ) {
$parser = new ItemIdParser();

$this->setExpectedException( 'Wikibase\DataModel\Entity\EntityIdParsingException' );
$parser->parse( $invalidIdSerialization );
}

public function invalidIdSerializationProvider() {
return array(
array( 'FOO' ),
array( null ),
array( 42 ),
array( array() ),
array( '' ),
array( 'q0' ),
array( '1p' ),
array( 'p1' ),
array( 'P100000' ),
);
}

}