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
91 changes: 91 additions & 0 deletions src/Claim/ClaimListAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Wikibase\DataModel\Claim;

/**
* Interface for objects that can be accessed as a list of Claim objects.
*
* @since 0.2
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
interface ClaimListAccess {

/**
* Adds the provided claims to the list. If a claim with the same GUID is already in the list,
* it is replaced. Specifying an index within the list of claims will shift existing claims. If
* the index where to insert the claim in the list of claims is not specified, the claim will be
* appended to the list.
*
* @since 0.2
*
* @param Claim $claim
* @param int|null $index
*/
public function addClaim( Claim $claim, $index = null );

/**
* Returns if the list contains a claim with the same GUID as the provided claim.
*
* @since 0.2
*
* @param Claim $claim
*
* @return boolean
*/
public function hasClaim( Claim $claim );

/**
* Returns the index of a claim or false if the claim could not be found.
*
* @since 0.5
*
* @param Claim $claim
*
* @return int|boolean
*/
public function indexOf( Claim $claim );

/**
* Removes the claim with the same GUID as the provided claim if such a claim exists in the list.
* If the claim is not in the list, the call has no effect.
*
* @since 0.2
*
* @param Claim $claim
*/
public function removeClaim( Claim $claim );

/**
* Returns if the list contains a claim with the the provided GUID.
*
* @since 0.3
*
* @param string $claimGuid
*
* @return boolean
*/
public function hasClaimWithGuid( $claimGuid );

/**
* Removes the claim with the provided GUID if such a claim exists in the list.
*
* @since 0.3
*
* @param string $claimGuid
*/
public function removeClaimWithGuid( $claimGuid );

/**
* Returns the claim with the provided GUID or null if there is no such claim.
*
* @since 0.3
*
* @param string $claimGuid
*
* @return Claim|null
*/
public function getClaimWithGuid( $claimGuid );

}
16 changes: 15 additions & 1 deletion src/Claim/Claims.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Daniel Kinzler
* @author H. Snater < mediawiki@snater.com >
*/
class Claims extends ArrayObject implements Hashable, Comparable {
class Claims extends ArrayObject implements ClaimListAccess, Hashable, Comparable {

/**
* @see GenericArrayObject::__construct
Expand Down Expand Up @@ -82,6 +82,8 @@ private function getClaimKey( Claim $claim ) {
}

/**
* @see ClaimListAccess::addClaim
*
* @since 0.1
*
* @param Claim $claim
Expand Down Expand Up @@ -120,6 +122,8 @@ private function insertClaimAtIndex( Claim $claim, $index ) {
}

/**
* @see ClaimListAccess::hasClaim
*
* @since 0.1
*
* @param Claim $claim
Expand All @@ -138,6 +142,8 @@ public function hasClaim( Claim $claim ) {
}

/**
* @see ClaimListAccess::indexOf
*
* @since 0.5
*
* @param Claim $claim
Expand All @@ -162,6 +168,8 @@ public function indexOf( Claim $claim ) {
}

/**
* @see ClaimListAccess::removeClaim
*
* @since 0.1
*
* @param Claim $claim
Expand All @@ -181,6 +189,8 @@ public function removeClaim( Claim $claim ) {
}

/**
* @see ClaimListAccess::hasClaimWithGuid
*
* @since 0.3
*
* @param string $claimGuid
Expand All @@ -192,6 +202,8 @@ public function hasClaimWithGuid( $claimGuid ) {
}

/**
* @see ClaimListAccess::removeClaimWithGuid
*
* @since 0.3
*
* @param string $claimGuid
Expand All @@ -203,6 +215,8 @@ public function removeClaimWithGuid( $claimGuid ) {
}

/**
* @see ClaimListAccess::getClaimWithGuid
*
* @since 0.3
*
* @param string $claimGuid
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ public function copy() {
}

/**
* @see ClaimListAccess::addClaim
*
* @since 0.3
* @deprecated since 1.0
*
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/Claim/ClaimListAccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Wikibase\DataModel\Tests\Claim;

use DataValues\StringValue;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\ClaimListAccess;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertyValueSnak;

/**
* Tests for the ClaimListAccess implementing classes.
*
* @group Wikibase
* @group WikibaseDataModel
* @group WikibaseClaim
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class ClaimListAccessTest extends \PHPUnit_Framework_TestCase {

public function claimTestProvider() {
$claims = array();

$claims[] = new Claim( new PropertyNoValueSnak(
new PropertyId( 'P42' )
) );
$claims[] = new Claim( new PropertyValueSnak(
new PropertyId( 'P23' ),
new StringValue( 'ohi' )
) );

$lists = array();

$lists[] = new Claims();

$argLists = array();

/**
* @var Claim $claim
*/
foreach ( $claims as $i => $claim ) {
$claim->setGuid( "ClaimListAccessTest\$claim-$i" );
}

/**
* @var ClaimListAccess $list
*/
foreach ( $lists as $list ) {
foreach ( $claims as $claim ) {
$argLists[] = array( clone $list, array( $claim ) );
}

$argLists[] = array( clone $list, $claims );
}

return $argLists;
}

/**
* @dataProvider claimTestProvider
*
* @param ClaimListAccess $list
* @param array $claims
*/
public function testAllOfTheStuff( ClaimListAccess $list, array $claims ) {
foreach ( $claims as $claim ) {
$list->addClaim( $claim );
$this->assertTrue( $list->hasClaim( $claim ) );

$list->removeClaim( $claim );
$this->assertFalse( $list->hasClaim( $claim ) );
}
}

}