diff --git a/src/Claim/ClaimListAccess.php b/src/Claim/ClaimListAccess.php new file mode 100644 index 00000000..4b4099f2 --- /dev/null +++ b/src/Claim/ClaimListAccess.php @@ -0,0 +1,91 @@ + + */ +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 ); + +} diff --git a/src/Claim/Claims.php b/src/Claim/Claims.php index 0563cda0..5fb88075 100644 --- a/src/Claim/Claims.php +++ b/src/Claim/Claims.php @@ -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 @@ -82,6 +82,8 @@ private function getClaimKey( Claim $claim ) { } /** + * @see ClaimListAccess::addClaim + * * @since 0.1 * * @param Claim $claim @@ -120,6 +122,8 @@ private function insertClaimAtIndex( Claim $claim, $index ) { } /** + * @see ClaimListAccess::hasClaim + * * @since 0.1 * * @param Claim $claim @@ -138,6 +142,8 @@ public function hasClaim( Claim $claim ) { } /** + * @see ClaimListAccess::indexOf + * * @since 0.5 * * @param Claim $claim @@ -162,6 +168,8 @@ public function indexOf( Claim $claim ) { } /** + * @see ClaimListAccess::removeClaim + * * @since 0.1 * * @param Claim $claim @@ -181,6 +189,8 @@ public function removeClaim( Claim $claim ) { } /** + * @see ClaimListAccess::hasClaimWithGuid + * * @since 0.3 * * @param string $claimGuid @@ -192,6 +202,8 @@ public function hasClaimWithGuid( $claimGuid ) { } /** + * @see ClaimListAccess::removeClaimWithGuid + * * @since 0.3 * * @param string $claimGuid @@ -203,6 +215,8 @@ public function removeClaimWithGuid( $claimGuid ) { } /** + * @see ClaimListAccess::getClaimWithGuid + * * @since 0.3 * * @param string $claimGuid diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index 5726ed8c..7384c24f 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -354,6 +354,8 @@ public function copy() { } /** + * @see ClaimListAccess::addClaim + * * @since 0.3 * @deprecated since 1.0 * diff --git a/tests/unit/Claim/ClaimListAccessTest.php b/tests/unit/Claim/ClaimListAccessTest.php new file mode 100644 index 00000000..b5173338 --- /dev/null +++ b/tests/unit/Claim/ClaimListAccessTest.php @@ -0,0 +1,79 @@ + + */ +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 ) ); + } + } + +}