Skip to content

Commit 9530078

Browse files
committed
Merge pull request #503 from wmde/itemIdParser
Add ItemIdParser
2 parents 21147c0 + b51b380 commit 9530078

5 files changed

Lines changed: 97 additions & 2 deletions

File tree

RELEASE-NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Wikibase DataModel release notes
22

3+
## Version 4.4.0 (2015-09-03)
4+
5+
* Added `ItemIdParser`
6+
37
## Version 4.3.0 (2015-09-02)
48

59
* Added `isEmpty` to `EntityDocument`

WikibaseDataModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
return 1;
1313
}
1414

15-
define( 'WIKIBASE_DATAMODEL_VERSION', '4.3.0' );
15+
define( 'WIKIBASE_DATAMODEL_VERSION', '4.4.0' );
1616

1717
if ( defined( 'MEDIAWIKI' ) ) {
1818
call_user_func( function() {

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"extra": {
4242
"branch-alias": {
43-
"dev-master": "4.3.x-dev"
43+
"dev-master": "4.4.x-dev"
4444
}
4545
},
4646
"scripts": {

src/Entity/ItemIdParser.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Entity;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* A trivial EntityIdParser that only parses the serializations of ItemIds. This is particularly
9+
* useful in cases where URIs are used to refer to concepts in an external Wikibase repository,
10+
* e.g. when referencing globes in coordinate values, or units in quantity values.
11+
*
12+
* @since 4.4
13+
*
14+
* @licence GNU GPL v2+
15+
* @author Thiemo Mättig
16+
*/
17+
class ItemIdParser implements EntityIdParser {
18+
19+
/**
20+
* @param string $idSerialization
21+
*
22+
* @throws EntityIdParsingException
23+
* @return ItemId
24+
*/
25+
public function parse( $idSerialization ) {
26+
try {
27+
return new ItemId( $idSerialization );
28+
} catch ( InvalidArgumentException $ex ) {
29+
throw new EntityIdParsingException( $ex->getMessage() );
30+
}
31+
}
32+
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Tests\Entity;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use Wikibase\DataModel\Entity\ItemId;
7+
use Wikibase\DataModel\Entity\ItemIdParser;
8+
9+
/**
10+
* @covers Wikibase\DataModel\Entity\ItemIdParser
11+
*
12+
* @licence GNU GPL v2+
13+
* @author Thiemo Mättig
14+
*/
15+
class ItemIdParserTest extends PHPUnit_Framework_TestCase {
16+
17+
/**
18+
* @dataProvider entityIdProvider
19+
*/
20+
public function testCanParseEntityId( $idString, ItemId $expected ) {
21+
$parser = new ItemIdParser();
22+
$actual = $parser->parse( $idString );
23+
24+
$this->assertEquals( $actual, $expected );
25+
}
26+
27+
public function entityIdProvider() {
28+
return array(
29+
array( 'q42', new ItemId( 'Q42' ) ),
30+
array( 'Q1337', new ItemId( 'Q1337' ) ),
31+
);
32+
}
33+
34+
/**
35+
* @dataProvider invalidIdSerializationProvider
36+
*/
37+
public function testCannotParseInvalidId( $invalidIdSerialization ) {
38+
$parser = new ItemIdParser();
39+
40+
$this->setExpectedException( 'Wikibase\DataModel\Entity\EntityIdParsingException' );
41+
$parser->parse( $invalidIdSerialization );
42+
}
43+
44+
public function invalidIdSerializationProvider() {
45+
return array(
46+
array( 'FOO' ),
47+
array( null ),
48+
array( 42 ),
49+
array( array() ),
50+
array( '' ),
51+
array( 'q0' ),
52+
array( '1p' ),
53+
array( 'p1' ),
54+
array( 'P100000' ),
55+
);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)