From 28e612ab7cc0c7b8c325a8a7c27d88925696f9cd Mon Sep 17 00:00:00 2001 From: Marius Hoch Date: Sun, 16 Nov 2014 17:29:22 +0100 Subject: [PATCH 1/2] Add the deprecated Claim handler functions to Property Right now these deprecated functions don't fullfil their contract, as the abstract Entity is not properly implementing them (which is ok as these function are being phased out). This is needed in order to actually make the Wikibase code that still relies on these old functions work with properties (which is something that used to work in the past when this code was still in Entity). Mid-term we will want to remove all usages of these functions and then phase them out, but for now they should at least be properly implemented (-> fullfiling their contract). The code and the tests were copied from item, with only very minor adjustments. --- src/Entity/Property.php | 59 ++++++++++++++++++++++++++++++ tests/unit/Entity/PropertyTest.php | 32 ++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/src/Entity/Property.php b/src/Entity/Property.php index 61a87184..588ecb28 100644 --- a/src/Entity/Property.php +++ b/src/Entity/Property.php @@ -3,6 +3,10 @@ namespace Wikibase\DataModel\Entity; use InvalidArgumentException; +use Wikibase\DataModel\Claim\Claim; +use Wikibase\DataModel\Claim\Claims; +use Wikibase\DataModel\Snak\Snak; +use Wikibase\DataModel\Statement\Statement; use Wikibase\DataModel\Statement\StatementList; use Wikibase\DataModel\Term\Fingerprint; use Wikibase\DataModel\StatementListProvider; @@ -203,4 +207,59 @@ public function setStatements( StatementList $statements ) { $this->statements = $statements; } + /** + * @deprecated since 1.0, use getStatements instead + * + * @return Statement[] + */ + public function getClaims() { + return $this->statements->toArray(); + } + + /** + * @deprecated since 1.0, use setStatements instead + * + * @param Claims $claims + */ + public function setClaims( Claims $claims ) { + $this->statements = new StatementList( iterator_to_array( $claims ) ); + } + + /** + * @deprecated since 1.0, use getStatements instead + * + * @return bool + */ + public function hasClaims() { + return $this->statements->count() !== 0; + } + + /** + * @deprecated since 1.0 + * + * @param Snak $mainSnak + * + * @return Statement + */ + public function newClaim( Snak $mainSnak ) { + return new Statement( new Claim( $mainSnak ) ); + } + + /** + * @deprecated since 1.0, use getStatements instead + * + * @param Claim $statement This needs to be a Statement as of 1.0 + * + * @throws InvalidArgumentException + */ + public function addClaim( Claim $statement ) { + if ( !( $statement instanceof Statement ) ) { + throw new InvalidArgumentException( '$statement must be an instance of Statement' ); + } elseif ( $statement->getGuid() === null ) { + throw new InvalidArgumentException( 'Can\'t add a Claim without a GUID.' ); + } + + $this->statements->addStatement( $statement ); + } + } diff --git a/tests/unit/Entity/PropertyTest.php b/tests/unit/Entity/PropertyTest.php index db91e8d4..189ac19a 100644 --- a/tests/unit/Entity/PropertyTest.php +++ b/tests/unit/Entity/PropertyTest.php @@ -7,6 +7,9 @@ use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\Snak\PropertyNoValueSnak; use Wikibase\DataModel\Statement\StatementList; +use Wikibase\DataModel\Statement\Statement; +use Wikibase\DataModel\Snak\PropertySomeValueSnak; +use Wikibase\DataModel\Claim\Claims; /** * @covers Wikibase\DataModel\Entity\Property @@ -207,4 +210,33 @@ public function testPropertyWithStatementsIsNotEmpty() { $this->assertFalse( $property->isEmpty() ); } + public function testNewClaimReturnsStatementWithProvidedMainSnak() { + /** @var Snak $snak */ + $snak = $this->getMock( 'Wikibase\DataModel\Snak\Snak' ); + + $property = Property::newFromType( 'string' ); + $statement = $property->newClaim( $snak ); + + $this->assertInstanceOf( 'Wikibase\DataModel\Statement\Statement', $statement ); + $this->assertEquals( $snak, $statement->getMainSnak() ); + } + + public function testSetClaims() { + $property = Property::newFromType( 'string' ); + + $statement0 = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) ); + $statement0->setGuid( 'TEST$NVS42' ); + + $statement1 = new Statement( new Claim( new PropertySomeValueSnak( 42 ) ) ); + $statement1->setGuid( 'TEST$SVS42' ); + + $statements = array( $statement0, $statement1 ); + + $property->setClaims( new Claims( $statements ) ); + $this->assertEquals( count( $statements ), $property->getStatements()->count(), "added some statements" ); + + $property->setClaims( new Claims() ); + $this->assertTrue( $property->getStatements()->isEmpty(), "should be empty again" ); + } + } From 6e55da408ea11b5cd3121bd64462aa1baf1553bd Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Thu, 20 Nov 2014 20:29:59 +0100 Subject: [PATCH 2/2] Use isEmpty --- src/Entity/Property.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entity/Property.php b/src/Entity/Property.php index 588ecb28..037f4809 100644 --- a/src/Entity/Property.php +++ b/src/Entity/Property.php @@ -231,7 +231,7 @@ public function setClaims( Claims $claims ) { * @return bool */ public function hasClaims() { - return $this->statements->count() !== 0; + return !$this->statements->isEmpty(); } /**