Daniel Werner has submitted this change and it was merged.
Change subject: Match EntityId and Serializer changes so the tests apss once
again
......................................................................
Match EntityId and Serializer changes so the tests apss once again
Change-Id: I0906681c164619da3f179e386168ffe3cab9ecd4
---
M Tests/Integration/Wikibase/Query/QueryEntityRoundtripTest.php
M Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php
M Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
M Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php
M Tests/Phpunit/Wikibase/Query/QueryEntityDeserializerTest.php
M Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php
M Tests/Phpunit/Wikibase/Query/QueryEntityTest.php
A Tests/Phpunit/Wikibase/Query/QueryIdTest.php
M Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
M Tests/evilMediaWikiBootstrap.php
M src/Wikibase/Query/ByPropertyValueEntityFinder.php
M src/Wikibase/Query/PropertyDataValueTypeFinder.php
M src/Wikibase/Query/QueryContent.php
M src/Wikibase/Query/QueryEntity.php
M src/Wikibase/Query/QueryEntityDeserializer.php
M src/Wikibase/Query/QueryEntitySerializer.php
A src/Wikibase/Query/QueryId.php
17 files changed, 245 insertions(+), 90 deletions(-)
Approvals:
Daniel Werner: Looks good to me, approved
diff --git a/Tests/Integration/Wikibase/Query/QueryEntityRoundtripTest.php
b/Tests/Integration/Wikibase/Query/QueryEntityRoundtripTest.php
index 23b73fb..e8846a7 100644
--- a/Tests/Integration/Wikibase/Query/QueryEntityRoundtripTest.php
+++ b/Tests/Integration/Wikibase/Query/QueryEntityRoundtripTest.php
@@ -10,16 +10,17 @@
use Ask\Language\Query;
use Ask\Language\Selection\PropertySelection;
use Ask\SerializerFactory;
-use DataValues\DataValueFactory;
use DataValues\StringValue;
use Wikibase\Claim;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\PropertySomeValueSnak;
use Wikibase\PropertyValueSnak;
use Wikibase\Query\DIC\ExtensionAccess;
use Wikibase\Query\QueryEntity;
use Wikibase\Query\QueryEntityDeserializer;
use Wikibase\Query\QueryEntitySerializer;
+use Wikibase\Query\QueryId;
use Wikibase\Repo\WikibaseRepo;
use Wikibase\SnakList;
@@ -51,7 +52,7 @@
}
protected function newQueryEntity() {
- $awesomePropertyId = new EntityId( 'property', 42 );
+ $awesomePropertyId = new EntityIdValue( new PropertyId( 'P42' )
);
$query = new Query(
new SomeProperty(
@@ -69,7 +70,7 @@
$queryEntity = new QueryEntity( $query );
- $queryEntity->setId( 1337 );
+ $queryEntity->setId( new QueryId( 'Y1337' ) );
$queryEntity->setLabel( 'en', 'Awesome' );
$queryEntity->setLabel( 'de', 'Awesome' );
diff --git a/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php
b/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php
index ba2e4d9..02c8195 100644
--- a/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php
+++ b/Tests/Integration/Wikibase/Query/QueryEntitySerializationTest.php
@@ -11,12 +11,14 @@
use Ask\SerializerFactory;
use DataValues\StringValue;
use Wikibase\Claim;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\PropertySomeValueSnak;
use Wikibase\PropertyValueSnak;
use Wikibase\Query\DIC\ExtensionAccess;
use Wikibase\Query\QueryEntity;
use Wikibase\Query\QueryEntitySerializer;
+use Wikibase\Query\QueryId;
use Wikibase\SnakList;
/**
@@ -43,7 +45,7 @@
}
protected function newQueryEntity() {
- $awesomePropertyId = new EntityId( 'property', 42 );
+ $awesomePropertyId = new EntityIdValue( new PropertyId( 'P42' )
);
$query = new Query(
new SomeProperty(
@@ -61,7 +63,7 @@
$queryEntity = new QueryEntity( $query );
- $queryEntity->setId( 1337 );
+ $queryEntity->setId( new QueryId( 'Y1337' ) );
$queryEntity->setLabel( 'en', 'Awesome' );
$queryEntity->setLabel( 'de', 'Awesome' );
@@ -90,10 +92,10 @@
}
private function newQueryEntitySerialization() {
- $awesomePropertyId = new EntityId( 'property', 42 );
+ $awesomePropertyId = new EntityIdValue( new PropertyId( 'P42' )
);
return array(
- 'entity' => array( 'query', 1337 ),
+ 'entity' => 'Y1337',
'label' => array(
'en' => 'Awesome',
diff --git a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
index 2bd36c1..90ca33f 100644
--- a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
+++ b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
@@ -2,15 +2,15 @@
namespace Tests\Phpunit\Wikibase\Query;
-use Ask\Language\Description\AnyValue;
use Ask\Language\Description\Description;
use Ask\Language\Description\SomeProperty;
use Ask\Language\Description\ValueDescription;
use Ask\Language\Option\QueryOptions;
-use DataValues\DataValue;
use DataValues\DataValueFactory;
use DataValues\StringValue;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Query\ByPropertyValueEntityFinder;
/**
@@ -76,7 +76,7 @@
}
protected function mockProperty() {
- return new EntityId( 'property', 4242 );
+ return new PropertyId( 'P4242' );
}
protected function assertEntityIdsEqual( array $expected, $actual ) {
@@ -95,7 +95,7 @@
'p42',
$fooString->toArray(),
new SomeProperty(
- $this->mockProperty(),
+ $this->mockPropertyValue(),
new ValueDescription( $fooString )
),
new QueryOptions( 10, 0 )
@@ -105,7 +105,7 @@
'p9001',
$barString->toArray(),
new SomeProperty(
- $this->mockProperty(),
+ $this->mockPropertyValue(),
new ValueDescription( $barString )
),
new QueryOptions( 42, 100 )
@@ -124,7 +124,7 @@
$this->newSimpleEntityIdFiner()->findEntities(
array(
- 'property' => $this->mockProperty()->toArray(),
+ 'property' =>
$this->mockPropertyValue()->toArray(),
'value' => json_encode( $fooString->toArray() ),
'limit' => $limit,
'offset' => '0'
@@ -180,7 +180,7 @@
$this->newSimpleEntityIdFiner()->findEntities(
array(
- 'property' => $this->mockProperty()->toArray(),
+ 'property' =>
$this->mockPropertyValue()->toArray(),
'value' => json_encode( $fooString->toArray() ),
'limit' => '100',
'offset' => $offset
@@ -206,7 +206,7 @@
$this->newSimpleEntityIdFiner()->findEntities(
array(
- 'property' => $this->mockProperty()->toArray(),
+ 'property' =>
$this->mockPropertyValue()->toArray(),
'value' => $value,
'limit' => '100',
'offset' => '0'
@@ -214,6 +214,10 @@
);
}
+ protected function mockPropertyValue() {
+ return new EntityIdValue( $this->mockProperty() );
+ }
+
public function invalidValueProvider() {
return array(
array( array() ),
diff --git a/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php
b/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php
index bc32417..1b167b8 100644
--- a/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php
+++ b/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php
@@ -2,14 +2,12 @@
namespace Tests\Phpunit\Wikibase\Query;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Query\PropertyDataValueTypeFinder;
/**
* @covers Wikibase\Query\PropertyDataValueTypeFinder
*
- * @file
- * @ingroup WikibaseQuery
* @group WikibaseQuery
*
* @licence GNU GPL v2+
@@ -18,7 +16,7 @@
class PropertyDataValueTypeFinderTest extends \PHPUnit_Framework_TestCase {
public function testGetDataValueTypeForProperty() {
- $propertyId = new EntityId( 'property', 1337 );
+ $propertyId = new PropertyId( 'P1337' );
$dataTypeId = 'awesomeType';
$dataValueType = 'awesomeDvType';
diff --git a/Tests/Phpunit/Wikibase/Query/QueryEntityDeserializerTest.php
b/Tests/Phpunit/Wikibase/Query/QueryEntityDeserializerTest.php
index 70662e1..7e27cd4 100644
--- a/Tests/Phpunit/Wikibase/Query/QueryEntityDeserializerTest.php
+++ b/Tests/Phpunit/Wikibase/Query/QueryEntityDeserializerTest.php
@@ -9,12 +9,11 @@
use Wikibase\EntityId;
use Wikibase\Query\QueryEntity;
use Wikibase\Query\QueryEntityDeserializer;
+use Wikibase\Query\QueryId;
/**
* @covers Wikibase\Query\QueryEntityDeserializer
*
- * @file
- * @ingroup WikibaseQuery
* @group WikibaseQuery
*
* @licence GNU GPL v2+
@@ -73,7 +72,7 @@
public function testCanNotDeserializeInvalidSerialization(
$notAQueryEntitySerialization ){
$deserializer = $this->newSimpleQueryEntityDeserializer();
- $canDeserialize = $deserializer->canDeserialize(
$notAQueryEntitySerialization );
+ $canDeserialize = $deserializer->isDeserializerFor(
$notAQueryEntitySerialization );
$this->assertFalse( $canDeserialize );
@@ -91,7 +90,7 @@
->will( $this->returnValue( $mockQuery ) );
$queryDeserializer->expects( $this->once() )
- ->method( 'canDeserialize' )
+ ->method( 'isDeserializerFor' )
->with( $this->equalTo(
$queryEntitySerialzation['query'] ) )
->will( $this->returnValue( true ) );
@@ -105,9 +104,9 @@
);
}
- public function newQueryEntitySerialization(){
+ public function newQueryEntitySerialization() {
return array(
- 'entity' => array( 'query', 1337 ),
+ 'entity' => 'Y1337',
'query' => array(
'objectType' => 'query',
@@ -200,7 +199,7 @@
->will( $this->returnValue( $this->newQuery() ) );
$queryDeserializer->expects( $this->once() )
- ->method( 'canDeserialize' )
+ ->method( 'isDeserializerFor' )
->will( $this->returnValue( true ) );
return $deserializer;
@@ -242,8 +241,7 @@
$deserialization
);
- $idSerialization = $queryEntitySerialzation['entity'];
- $expectedId = new EntityId( $idSerialization[0],
$idSerialization[1] );
+ $expectedId = new QueryId( $queryEntitySerialzation['entity'] );
$this->assertEquals(
$expectedId,
@@ -270,6 +268,18 @@
);
$argLists[] = array(
+ 'Q42'
+ );
+
+ $argLists[] = array(
+ 'Y 1'
+ );
+
+ $argLists[] = array(
+ 'Y1.42'
+ );
+
+ $argLists[] = array(
array()
);
diff --git a/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php
b/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php
index 50af9ad..c5e4b19 100644
--- a/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php
+++ b/Tests/Phpunit/Wikibase/Query/QueryEntitySerializerTest.php
@@ -11,13 +11,11 @@
use Wikibase\PropertySomeValueSnak;
use Wikibase\Query\QueryEntity;
use Wikibase\Query\QueryEntitySerializer;
-use Wikibase\Test\ClaimListAccessTest;
+use Wikibase\Query\QueryId;
/**
* @covers Wikibase\Query\QueryEntitySerializer
*
- * @file
- * @ingroup WikibaseQuery
* @group WikibaseQuery
*
* @licence GNU GPL v2+
@@ -76,13 +74,13 @@
public function testCannotSerialize( $notAQueryEntity ) {
$serializer = $this->newSimpleQueryEntitySerializer();
- $this->assertFalse( $serializer->canSerialize( $notAQueryEntity
) );
+ $this->assertFalse( $serializer->isSerializerFor(
$notAQueryEntity ) );
}
public function testCanSerialize() {
$queryEntity = $this->newSimpleEntity();
$serializer = $this->newSimpleQueryEntitySerializer();
- $this->assertTrue( $serializer->canSerialize( $queryEntity ) );
+ $this->assertTrue( $serializer->isSerializerFor( $queryEntity )
);
}
public function testSerializationCallsQuerySerialization() {
@@ -110,34 +108,28 @@
$serialization =
$this->newSimpleQueryEntitySerializer()->serialize( $queryEntity );
- $this->assertHasSerializedId( $serialization, null );
+ $this->assertNull( $serialization['entity'] );
}
/**
- * @dataProvider idNumberProvider
+ * @dataProvider idProvider
*/
- public function testSerializationContainsId( $idNumber ) {
+ public function testSerializationContainsId( QueryId $id ) {
$queryEntity = $this->newSimpleEntity();
- $queryEntity->setId( $idNumber );
+ $queryEntity->setId( $id );
$serialization =
$this->newSimpleQueryEntitySerializer()->serialize( $queryEntity );
- $this->assertHasSerializedId( $serialization, array(
$queryEntity->getType(), $idNumber ) );
+ $this->assertEquals( $serialization['entity'], $id );
}
- public function idNumberProvider() {
+ public function idProvider() {
return array(
- array( 42 ),
- array( 9001 ),
- array( 31337 ),
+ array( new QueryId( 'Y42' ) ),
+ array( new QueryId( 'Y9001' ) ),
+ array( new QueryId( 'Y31337' ) ),
);
- }
-
- protected function assertHasSerializedId( $serialization, $expectedId )
{
- $this->assertInternalType( 'array', $serialization );
- $this->assertArrayHasKey( 'entity', $serialization );
- $this->assertEquals( $expectedId, $serialization['entity'] );
}
/**
diff --git a/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php
b/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php
index 8c070ad..1b01bbf 100644
--- a/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php
+++ b/Tests/Phpunit/Wikibase/Query/QueryEntityTest.php
@@ -10,10 +10,6 @@
/**
* @covers Wikibase\Query\QueryEntity
*
- * @file
- * @since 0.1
- *
- * @ingroup WikibaseQuery
* @group WikibaseQuery
*
* @licence GNU GPL v2+
diff --git a/Tests/Phpunit/Wikibase/Query/QueryIdTest.php
b/Tests/Phpunit/Wikibase/Query/QueryIdTest.php
new file mode 100644
index 0000000..a3a6e5c
--- /dev/null
+++ b/Tests/Phpunit/Wikibase/Query/QueryIdTest.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Tests\Phpunit\Wikibase\Query;
+
+use Wikibase\Query\QueryId;
+
+/**
+ * @covers Wikibase\Query\QueryId
+ *
+ * @group Wikibase
+ * @group WikibaseQuery
+ * @group EntityIdTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class ItemIdTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider idSerializationProvider
+ */
+ public function testCanConstructId( $idSerialization ) {
+ $id = new QueryId( $idSerialization );
+
+ $this->assertEquals(
+ strtoupper( $idSerialization ),
+ $id->getSerialization()
+ );
+ }
+
+ public function idSerializationProvider() {
+ return array(
+ array( 'y1' ),
+ array( 'y100' ),
+ array( 'y1337' ),
+ array( 'y31337' ),
+ array( 'Y31337' ),
+ array( 'Y42' ),
+ );
+ }
+
+ /**
+ * @dataProvider invalidIdSerializationProvider
+ */
+ public function testCannotConstructWithInvalidSerialization(
$invalidSerialization ) {
+ $this->setExpectedException( 'InvalidArgumentException' );
+ new QueryId( $invalidSerialization );
+ }
+
+ public function invalidIdSerializationProvider() {
+ return array(
+ array( 'y' ),
+ array( 'p1' ),
+ array( 'yy1' ),
+ array( '1y' ),
+ array( 'y01' ),
+ array( 'y 1' ),
+ array( ' y1' ),
+ array( 'y1 ' ),
+ array( '1' ),
+ array( ' ' ),
+ array( '' ),
+ array( '0' ),
+ array( 0 ),
+ array( 1 ),
+ );
+ }
+
+}
diff --git a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
index 2733de9..c8e8e54 100644
--- a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
+++ b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
@@ -3,7 +3,8 @@
namespace Tests\Integration\Wikibase\Query;
use DataValues\StringValue;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Item;
use Wikibase\Property;
use Wikibase\PropertyContent;
@@ -25,10 +26,11 @@
class EntitiesByPropertyValueApiTest extends \ApiTestCase {
const MODULE_NAME = 'entitiesByPropertyValue';
- const PROPERTY_ID = 31337;
- const ITEM_ID = 42;
- const PROPERTY_ID_STRING = 'p31337';
- const ITEM_ID_STRING = 'q42';
+ const PROPERTY_ID_STRING = 'P31337';
+ const ITEM_ID_STRING = 'Q42';
+
+ protected $itemId;
+ protected $propertyId;
protected function getQueryStore() {
return ExtensionAccess::getWikibaseQuery()->getQueryStore();
@@ -44,6 +46,10 @@
public function setUp() {
parent::setUp();
+
+ $this->itemId = new ItemId( self::ITEM_ID_STRING );
+ $this->propertyId = new PropertyId( self::PROPERTY_ID_STRING );
+
$this->reinitializeStore();
$this->createNewProperty();
@@ -57,7 +63,7 @@
protected function createNewProperty() {
$property = Property::newEmpty();
- $property->setId( new EntityId( 'property', self::PROPERTY_ID )
);
+ $property->setId( $this->propertyId );
$property->setDataTypeId( 'string' );
$propertyContent = PropertyContent::newFromProperty( $property
);
@@ -76,11 +82,11 @@
protected function newMockItem() {
$item = Item::newEmpty();
- $item->setId( new EntityId( 'item', self::ITEM_ID ) );
+ $item->setId( $this->itemId );
$item->addClaim( new Statement(
new PropertyValueSnak(
- new EntityId( 'property', self::PROPERTY_ID ),
+ $this->propertyId,
$this->newMockValue()
)
) );
diff --git a/Tests/evilMediaWikiBootstrap.php b/Tests/evilMediaWikiBootstrap.php
index 6ba1e6e..1ec368e 100644
--- a/Tests/evilMediaWikiBootstrap.php
+++ b/Tests/evilMediaWikiBootstrap.php
@@ -72,4 +72,4 @@
}
$wgCommandLineMode = true;
return $settingsFile;
-}
\ No newline at end of file
+}
diff --git a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
index beb33bb..caf0a5a 100644
--- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
+++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
@@ -9,7 +9,9 @@
use DataValues\DataValueFactory;
use InvalidArgumentException;
use RuntimeException;
-use Wikibase\EntityId;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\EntityIdParser;
use Wikibase\QueryEngine\QueryEngine;
@@ -89,23 +91,27 @@
$value = $this->dvFactory->newFromArray(
$valueSerialization );
}
catch ( RuntimeException $ex ) {
- throw new InvalidArgumentException( '', 0, $ex );
+ throw new InvalidArgumentException( $ex->getMessage(),
0, $ex );
+ }
+
+ if ( !( $propertyId instanceof PropertyId ) ) {
+ throw new InvalidArgumentException( 'The provided
EntityId needs to be a PropertyId' );
}
return $this->findByPropertyValue( $propertyId, $value, $limit,
$offset );
}
/**
- * @param EntityId $propertyId
+ * @param PropertyId $propertyId
* @param DataValue $value
* @param int $limit
* @param int $offset
*
* @return EntityId[]
*/
- protected function findByPropertyValue( EntityId $propertyId, DataValue
$value, $limit, $offset ) {
+ protected function findByPropertyValue( PropertyId $propertyId,
DataValue $value, $limit, $offset ) {
$description = new SomeProperty(
- $propertyId,
+ new EntityIdValue( $propertyId ),
new ValueDescription( $value )
);
diff --git a/src/Wikibase/Query/PropertyDataValueTypeFinder.php
b/src/Wikibase/Query/PropertyDataValueTypeFinder.php
index 4be1084..7dcd831 100644
--- a/src/Wikibase/Query/PropertyDataValueTypeFinder.php
+++ b/src/Wikibase/Query/PropertyDataValueTypeFinder.php
@@ -3,7 +3,7 @@
namespace Wikibase\Query;
use DataTypes\DataTypeFactory;
-use DataValues\DataValue;
+use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Lib\PropertyDataTypeLookup;
use Wikibase\QueryEngine\PropertyDataValueTypeLookup;
@@ -29,13 +29,11 @@
/**
* @see PropertyDataValueTypeLookup::getDataValueTypeForProperty
*
- * @param DataValue $propertyId
+ * @param PropertyId $propertyId
*
* @return string
*/
- public function getDataValueTypeForProperty( DataValue $propertyId ) {
- // TODO: verify is EntityId
-
+ public function getDataValueTypeForProperty( PropertyId $propertyId ) {
$dataTypeId =
$this->propertyDtLookup->getDataTypeIdForProperty( $propertyId );
// TODO: catch OutOfBoundsException
diff --git a/src/Wikibase/Query/QueryContent.php
b/src/Wikibase/Query/QueryContent.php
index 23e0cae..4363306 100644
--- a/src/Wikibase/Query/QueryContent.php
+++ b/src/Wikibase/Query/QueryContent.php
@@ -2,17 +2,17 @@
namespace Wikibase\Query;
-use Title;
use Content;
+use DataUpdate;
use ParserOptions;
use ParserOutput;
+use Status;
+use Title;
+use User;
use Wikibase\EntityContent;
use Wikibase\EntityDeletionUpdate;
use Wikibase\EntityModificationUpdate;
use WikiPage;
-use User;
-use Status;
-use DataUpdate;
/**
* Content object for articles representing Wikibase queries.
diff --git a/src/Wikibase/Query/QueryEntity.php
b/src/Wikibase/Query/QueryEntity.php
index abb3e1c..099d674 100644
--- a/src/Wikibase/Query/QueryEntity.php
+++ b/src/Wikibase/Query/QueryEntity.php
@@ -5,6 +5,7 @@
use Ask\DeserializerFactory;
use Ask\Language\Query;
use Ask\SerializerFactory;
+use InvalidArgumentException;
use MWException;
use RuntimeException;
use Wikibase\Entity;
@@ -76,6 +77,21 @@
}
/**
+ * @see Entity::setId
+ *
+ * @param QueryId $id
+ *
+ * @throws InvalidArgumentException
+ */
+ public function setId( $id ) {
+ if ( !is_object( $id ) || !( $id instanceof QueryId ) ) {
+ throw new InvalidArgumentException( 'The id of a
QueryEntity can only be of type QueryId' );
+ }
+
+ parent::setId( $id );
+ }
+
+ /**
* @see Entity::stub
*/
public function stub() {
@@ -100,4 +116,15 @@
}
}
+ /**
+ * @since 0.1
+ *
+ * @param string $idSerialization
+ *
+ * @return QueryId
+ */
+ protected function idFromSerialization( $idSerialization ) {
+ return new QueryId( $idSerialization );
+ }
+
}
diff --git a/src/Wikibase/Query/QueryEntityDeserializer.php
b/src/Wikibase/Query/QueryEntityDeserializer.php
index 56df911..30f9f63 100644
--- a/src/Wikibase/Query/QueryEntityDeserializer.php
+++ b/src/Wikibase/Query/QueryEntityDeserializer.php
@@ -5,10 +5,15 @@
use Deserializers\Deserializer;
use Deserializers\Exceptions\DeserializationException;
use Deserializers\Exceptions\MissingAttributeException;
+use InvalidArgumentException;
use Wikibase\Claim;
-use Wikibase\EntityId;
/**
+ * Deserialization for the internal representation of QueryEntity objects.
+ *
+ * TODO: The term deserialization code can become a strategy so this class
+ * can be used for external representation deserialization as well.
+ *
* @since 1.0
*
* @file
@@ -51,7 +56,7 @@
}
protected function assertCanDeserialize() {
- if( !$this->canDeserialize( $this->serialization ) ) {
+ if( !$this->isDeserializerFor( $this->serialization ) ) {
throw new DeserializationException( 'Cannot
deserialize.' );
}
}
@@ -63,14 +68,14 @@
}
protected function deserializeId() {
- $idSerialization = $this->serialization['entity'];
-
- if ( !is_array( $idSerialization ) || count( $idSerialization )
!== 2
- || !is_string( $idSerialization[0] ) || !is_int(
$idSerialization[1] ) ) {
- throw new DeserializationException( 'Invalid entity id
provided' );
+ try {
+ $id = new QueryId( $this->serialization['entity'] );
+ }
+ catch ( InvalidArgumentException $ex ) {
+ throw new DeserializationException( $ex->getMessage(),
$ex );
}
- $this->queryEntity->setId( new EntityId( $idSerialization[0],
$idSerialization[1] ) );
+ $this->queryEntity->setId( $id );
}
protected function deserializeLabels() {
@@ -141,7 +146,7 @@
}
}
- public function canDeserialize( $serialization ) {
+ public function isDeserializerFor( $serialization ) {
if( !is_array( $serialization ) ) {
return false;
}
@@ -152,7 +157,7 @@
}
}
- return $this->queryDeserializer->canDeserialize(
$serialization['query'] );
+ return $this->queryDeserializer->isDeserializerFor(
$serialization['query'] );
}
protected function requireAttribute( $attributeName ) {
diff --git a/src/Wikibase/Query/QueryEntitySerializer.php
b/src/Wikibase/Query/QueryEntitySerializer.php
index fd57838..2e42e6a 100644
--- a/src/Wikibase/Query/QueryEntitySerializer.php
+++ b/src/Wikibase/Query/QueryEntitySerializer.php
@@ -52,7 +52,7 @@
return $id;
}
else {
- return array( $id->getEntityType(), $id->getNumericId()
);
+ return $id->getSerialization();
}
}
@@ -67,7 +67,7 @@
}
- public function canSerialize( $mixed ) {
+ public function isSerializerFor( $mixed ) {
return $mixed instanceof QueryEntity;
}
diff --git a/src/Wikibase/Query/QueryId.php b/src/Wikibase/Query/QueryId.php
new file mode 100644
index 0000000..33c84a9
--- /dev/null
+++ b/src/Wikibase/Query/QueryId.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Wikibase\Query;
+
+use InvalidArgumentException;
+use Wikibase\DataModel\Entity\EntityId;
+
+/**
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class QueryId extends EntityId {
+
+ const PATTERN = '/^y[1-9][0-9]*$/i';
+
+ /**
+ * @param string $idSerialization
+ *
+ * @throws InvalidArgumentException
+ */
+ public function __construct( $idSerialization ) {
+ $this->assertValidIdFormat( $idSerialization );
+
+ parent::__construct(
+ QueryEntity::ENTITY_TYPE,
+ $idSerialization
+ );
+ }
+
+ protected function assertValidIdFormat( $idSerialization ) {
+ if ( !is_string( $idSerialization ) ) {
+ throw new InvalidArgumentException( 'The id
serialization needs to be a string.' );
+ }
+
+ if ( !preg_match( self::PATTERN, $idSerialization ) ) {
+ throw new InvalidArgumentException( 'Invalid QueryId
serialization provided.' );
+ }
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/82873
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0906681c164619da3f179e386168ffe3cab9ecd4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQuery
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits