Skip to content
Closed
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
12 changes: 6 additions & 6 deletions src/Claim/Claim.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Comparable;
use Hashable;
use InvalidArgumentException;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Snak\SnakList;
use Wikibase\DataModel\Snak\Snaks;
Expand Down Expand Up @@ -36,7 +36,7 @@ class Claim implements Hashable, Comparable {
*
* @var Snak
*/
protected $mainSnak;
private $mainSnak;

/**
* The property snaks that are qualifiers for this claim.
Expand All @@ -45,14 +45,14 @@ class Claim implements Hashable, Comparable {
*
* @var Snaks
*/
protected $qualifiers;
private $qualifiers;

/**
* @since 0.2
*
* @var string|null
*/
protected $guid = null;
private $guid = null;

/**
* Constructor.
Expand Down Expand Up @@ -131,7 +131,7 @@ public function getHash() {
*
* @since 0.2
*
* @return EntityId
* @return PropertyId
*/
public function getPropertyId() {
return $this->getMainSnak()->getPropertyId();
Expand Down Expand Up @@ -212,7 +212,7 @@ public function equals( $target ) {
return $this->claimFieldsEqual( $target );
}

protected function claimFieldsEqual( Claim $target ) {
private function claimFieldsEqual( Claim $target ) {
return $this->guid === $target->guid
&& $this->mainSnak->equals( $target->mainSnak )
&& $this->qualifiers->equals( $target->qualifiers );
Expand Down
64 changes: 47 additions & 17 deletions src/Claim/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wikibase\DataModel\Claim;

use InvalidArgumentException;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\References;
Expand All @@ -18,32 +19,36 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class Statement extends Claim {
class Statement {

/**
* @var Claim
*/
private $claim;

/**
* @since 0.1
*
* @var References
*/
protected $references;
private $references;

/**
* @since 0.1
*
* @var integer, element of the Claim::RANK_ enum
*/
protected $rank = self::RANK_NORMAL;
private $rank;

/**
* @since 0.1
* @since 1.0
*
* @param Claim $claim
* @param References $references
* @param integer $rank Element of the Claim::RANK_ enum
*
* @param Snak $mainSnak
* @param Snaks|null $qualifiers
* @param References|null $references
* @throws InvalidArgumentException
*/
public function __construct( Snak $mainSnak, Snaks $qualifiers = null, References $references = null ) {
parent::__construct( $mainSnak, $qualifiers );
public function __construct( Claim $claim, References $references = null, $rank = Claim::RANK_NORMAL ) {
$this->claim = $claim;
$this->references = $references === null ? new ReferenceList() : $references;
$this->setRank( $rank );
}

/**
Expand All @@ -68,6 +73,24 @@ public function setReferences( References $references ) {
$this->references = $references;
}

/**
* @since 1.0
*
* @return Claim
*/
public function getClaim() {
return $this->claim;
}

/**
* @since 1.0
*
* @param Claim $claim
*/
public function setClaim( Claim $claim ) {
$this->claim = $claim;
}

/**
* Sets the rank of the statement.
* The rank is an element of the Claim::RANK_ enum, excluding RANK_TRUTH.
Expand All @@ -78,7 +101,7 @@ public function setReferences( References $references ) {
* @throws InvalidArgumentException
*/
public function setRank( $rank ) {
$ranks = array( self::RANK_DEPRECATED, self::RANK_NORMAL, self::RANK_PREFERRED );
$ranks = array( Claim::RANK_DEPRECATED, Claim::RANK_NORMAL, Claim::RANK_PREFERRED );

if ( !in_array( $rank, $ranks, true ) ) {
throw new InvalidArgumentException( 'Invalid rank specified for statement: ' . var_export( $rank, true ) );
Expand Down Expand Up @@ -109,7 +132,7 @@ public function getHash() {
return sha1( implode(
'|',
array(
parent::getHash(),
$this->claim->getHash(),
$this->rank,
$this->references->getValueHash(),
)
Expand All @@ -125,7 +148,7 @@ public function getHash() {
* @return Snak[]
*/
public function getAllSnaks() {
$snaks = parent::getAllSnaks();
$snaks = $this->claim->getAllSnaks();

/* @var Reference $reference */
foreach( $this->getReferences() as $reference ) {
Expand All @@ -149,8 +172,15 @@ public function equals( $target ) {
return false;
}

return $this->claimFieldsEqual( $target )
return $this->claim->equals( $target->claim )
&& $this->references->equals( $target->getReferences() );
}

/**
* @return PropertyId
*/
public function getPropertyId() {
return $this->claim->getPropertyId();
}

}
4 changes: 2 additions & 2 deletions tests/unit/ByPropertyIdArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use DataValues\StringValue;
use ReflectionClass;
use ReflectionMethod;
use Wikibase\Claims;
use Wikibase\DataModel\ByPropertyIdArray;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
Expand Down Expand Up @@ -80,7 +80,7 @@ function( Snak $snak ) {

$lists[] = array_map(
function( Snak $snak ) {
return new Statement( $snak );
return new Statement( new Claim( $snak ) );
},
$snaks
);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Claim/ClaimStandaloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testGivenClaimWithDifferentGuids_equalsReturnsFalse() {

public function testGivenSimilarStatement_equalsReturnsFalse() {
$claim = new Claim( new PropertyNoValueSnak( 42 ) );
$this->assertFalse( $claim->equals( new Statement( new PropertyNoValueSnak( 42 ) ) ) );
$this->assertFalse( $claim->equals( new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) ) ) );
}

}
8 changes: 4 additions & 4 deletions tests/unit/Claim/ClaimsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ protected function makeStatement( Snak $mainSnak, $guid = null ) {
$guid = 'TEST$statement-' . $this->guidCounter;
}

$claim = new Statement( $mainSnak );
$claim->setGuid( $guid );
$statement = new Statement( new Claim( $mainSnak ) );
$statement->getClaim()->setGuid( $guid );

return $claim;
return $statement;
}

public function testArrayObjectNotConstructedFromObject() {
Expand Down Expand Up @@ -516,7 +516,7 @@ public function getDiffProvider() {
$claim4 = $this->makeClaim( new PropertyNoValueSnak( 2 ) );

$statement0 = $this->makeStatement( new PropertyNoValueSnak( 5 ) );
$statement0->setRank( Statement::RANK_PREFERRED );
$statement0->setRank( Claim::RANK_PREFERRED );

$statement1 = $this->makeStatement( new PropertyNoValueSnak( 5 ) );
$statement1->setReferences( new ReferenceList( array( new Reference(
Expand Down
76 changes: 39 additions & 37 deletions tests/unit/Claim/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class StatementTest extends ClaimTest {
public function instanceProvider() {
$instances = array();

$id42 = new PropertyId( 'P42' );

$baseInstance = new Statement( new PropertyNoValueSnak( $id42 ) );
$baseInstance = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );

$instances[] = $baseInstance;

Expand Down Expand Up @@ -123,12 +121,6 @@ public function testSetRankToTruth( Statement $statement ) {
$statement->setRank( Claim::RANK_TRUTH );
}

public function testStatementRankCompatibility() {
$this->assertEquals( Claim::RANK_DEPRECATED, Statement::RANK_DEPRECATED );
$this->assertEquals( Claim::RANK_PREFERRED, Statement::RANK_PREFERRED );
$this->assertEquals( Claim::RANK_NORMAL, Statement::RANK_NORMAL );
}

/**
* @dataProvider instanceProvider
*/
Expand All @@ -147,12 +139,12 @@ public function testGetPropertyId( Claim $statement ) {
}

public function testGetHash() {
$claim0 = new Statement( new PropertyNoValueSnak( 42 ) );
$claim0->setGuid( 'claim0' );
$claim0 = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );
$claim0->getClaim()->setGuid( 'claim0' );
$claim0->setRank( Claim::RANK_DEPRECATED );

$claim1 = new Statement( new PropertyNoValueSnak( 42 ) );
$claim1->setGuid( 'claim1' );
$claim1 = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );
$claim1->getClaim()->setGuid( 'claim1' );
$claim1->setRank( Claim::RANK_DEPRECATED );

$this->assertEquals( $claim0->getHash(), $claim1->getHash() );
Expand All @@ -166,7 +158,7 @@ public function testGetAllSnaks( Claim $claim ) {
$statement = $claim;
$snaks = $statement->getAllSnaks();

$c = count( $statement->getQualifiers() ) + 1;
$c = count( $statement->getClaim()->getQualifiers() ) + 1;

/* @var Reference $reference */
foreach ( $statement->getReferences() as $reference ) {
Expand All @@ -177,7 +169,7 @@ public function testGetAllSnaks( Claim $claim ) {
}

public function testGivenNonStatement_equalsReturnsFalse() {
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
$statement = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );

$this->assertFalse( $statement->equals( null ) );
$this->assertFalse( $statement->equals( 42 ) );
Expand All @@ -186,70 +178,80 @@ public function testGivenNonStatement_equalsReturnsFalse() {

public function testGivenSameStatement_equalsReturnsTrue() {
$statement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 1337 ),
) ),
new Claim(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 1337 ),
) )
),
new ReferenceList( array(
new PropertyNoValueSnak( 1337 ),
) )
);

$statement->setGuid( 'kittens' );
$statement->getClaim()->setGuid( 'kittens' );

$this->assertTrue( $statement->equals( $statement ) );
$this->assertTrue( $statement->equals( clone $statement ) );
}

public function testGivenStatementWithDifferentProperty_equalsReturnsFalse() {
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
$statement = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );
$this->assertFalse( $statement->equals( new Statement( new PropertyNoValueSnak( 43 ) ) ) );
}

public function testGivenStatementWithDifferentSnakType_equalsReturnsFalse() {
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
$statement = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );
$this->assertFalse( $statement->equals( new Statement( new PropertySomeValueSnak( 42 ) ) ) );
}

public function testStatementClaimWithDifferentQualifiers_equalsReturnsFalse() {
$statement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 1337 ),
) )
new Claim(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 1337 ),
) )
)
);

$differentStatement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 32202 ),
) )
new Claim(
new PropertyNoValueSnak( 42 ),
new SnakList( array(
new PropertyNoValueSnak( 32202 ),
) )
)
);

$this->assertFalse( $statement->equals( $differentStatement ) );
}

public function testGivenStatementWithDifferentGuids_equalsReturnsFalse() {
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
$statement = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );

$differentStatement = new Statement( new PropertyNoValueSnak( 42 ) );
$differentStatement->setGuid( 'kittens' );
$differentStatement = new Statement( new Claim( new PropertyNoValueSnak( 42 ) ) );
$differentStatement->getClaim()->setGuid( 'kittens' );

$this->assertFalse( $statement->equals( $differentStatement ) );
}

public function testStatementClaimWithDifferentReferences_equalsReturnsFalse() {
$statement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList( array() ),
new Claim(
new PropertyNoValueSnak( 42 ),
new SnakList( array() )
),
new ReferenceList( array(
new PropertyNoValueSnak( 1337 ),
) )
);

$differentStatement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList( array() ),
new Claim(
new PropertyNoValueSnak( 42 ),
new SnakList( array() )
),
new ReferenceList( array(
new PropertyNoValueSnak( 32202 ),
) )
Expand Down