Henning Snater has uploaded a new change for review.
https://gerrit.wikimedia.org/r/90318
Change subject: Implemented numeric index in Claims
......................................................................
Implemented numeric index in Claims
Added optional "index" parameter to addClaim() to enable inserting a claim at a
specific index
within the list of claims. Added indexOf() to retrieve the numeric index of a
claim.
Change-Id: Iefeebdcc4c6bc8c477d3f234399bcd403466bd52
---
M DataModel/Claim/ClaimListAccess.php
M DataModel/Claim/Claims.php
M DataModel/ReferenceList.php
M DataModel/References.php
M tests/phpunit/Claim/ClaimsTest.php
5 files changed, 114 insertions(+), 9 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel
refs/changes/18/90318/1
diff --git a/DataModel/Claim/ClaimListAccess.php
b/DataModel/Claim/ClaimListAccess.php
index c4d7350..c7f0326 100644
--- a/DataModel/Claim/ClaimListAccess.php
+++ b/DataModel/Claim/ClaimListAccess.php
@@ -37,6 +37,17 @@
public function hasClaim( Claim $claim );
/**
+ * Returns the index of a claim or false if the reference 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.
*
diff --git a/DataModel/Claim/Claims.php b/DataModel/Claim/Claims.php
index a48e86a..afb4edf 100644
--- a/DataModel/Claim/Claims.php
+++ b/DataModel/Claim/Claims.php
@@ -110,10 +110,47 @@
* @since 0.1
*
* @param Claim $claim
+ * @param int|null $index
+ *
+ * @throws InvalidArgumentException
*/
- public function addClaim( Claim $claim ) {
- $key = $this->getClaimKey( $claim );
- $this->offsetSet( $key, $claim );
+ public function addClaim( Claim $claim, $index = null ) {
+ if( !is_null( $index ) && !is_integer( $index ) ) {
+ throw new InvalidArgumentException( 'Index needs to be
null or an integer value' );
+ } else if ( is_null( $index ) || $index >= count( $this ) ) {
+ $this->append( $claim );
+ } else {
+ $this->insertClaimAtIndex( $claim, $index );
+ }
+ }
+
+ /**
+ * @since 0.5
+ *
+ * @param Claim $claim
+ * @param int $index
+ */
+ protected function insertClaimAtIndex( Claim $claim, $index ) {
+ $claimsToShift = array();
+ $i = 0;
+
+ // Determine the claims to shift and remove them from the array:
+ foreach( $this as $object ) {
+ if( $i++ >= $index ) {
+ $claimsToShift[] = $object;
+ }
+ }
+
+ foreach( $claimsToShift as $object ) {
+ $this->offsetUnset( $this->getClaimKey( $object ) );
+ }
+
+ // Append the new claim and re-append the previously removed
claims:
+ $this->append( $claim );
+
+ foreach( $claimsToShift as $object ) {
+ $this->append( $object );
+ }
}
/**
@@ -123,14 +160,14 @@
*
* @param Claim $claim
*
- * @throws \InvalidArgumentException
+ * @throws InvalidArgumentException
*/
public function append( $claim ) {
if ( !( $claim instanceof Claim ) ) {
throw new InvalidArgumentException( '$claim must be a
Claim instances' );
}
- $this->addClaim( $claim );
+ parent::append( $claim );
}
/**
@@ -154,6 +191,32 @@
}
/**
+ * @see ClaimListAccess::indexOf
+ *
+ * @since 0.5
+ *
+ * @param Claim $claim
+ *
+ * @return int|boolean
+ */
+ public function indexOf( Claim $claim ) {
+ $guid = $claim->getGuid();
+ $index = 0;
+
+ /**
+ * @var Claim $claimObject
+ */
+ foreach( $this as $claimObject ) {
+ if( $claimObject->getGuid() === $guid ) {
+ return $index;
+ }
+ $index++;
+ }
+
+ return false;
+ }
+
+ /**
* @see ClaimListAccess::removeClaim
*
* @since 0.1
diff --git a/DataModel/ReferenceList.php b/DataModel/ReferenceList.php
index 329dc8d..45fe88c 100644
--- a/DataModel/ReferenceList.php
+++ b/DataModel/ReferenceList.php
@@ -47,7 +47,7 @@
}
/**
- * @since 0.1
+ * @since 0.5
*
* @param Reference $reference
* @param int $index
@@ -92,7 +92,7 @@
/**
* @see References::indexOf
*
- * @since 0.1
+ * @since 0.5
*
* @param Reference $reference
*
diff --git a/DataModel/References.php b/DataModel/References.php
index c7c0ac8..7b51990 100644
--- a/DataModel/References.php
+++ b/DataModel/References.php
@@ -39,7 +39,7 @@
/**
* Returns the index of a reference or false if the reference could not
be found.
*
- * @since 0.1
+ * @since 0.5
*
* @param Reference $reference
*
diff --git a/tests/phpunit/Claim/ClaimsTest.php
b/tests/phpunit/Claim/ClaimsTest.php
index bf2070d..93ad234 100644
--- a/tests/phpunit/Claim/ClaimsTest.php
+++ b/tests/phpunit/Claim/ClaimsTest.php
@@ -243,6 +243,37 @@
$this->assertNotNull( $claims->getClaimWithGuid(
$claim1->getGuid() ) );
$this->assertNotNull( $claims->getClaimWithGuid(
$claim2->getGuid() ) );
+
+ // Insert claim at the beginning:
+ $claim3 = $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P17' ) ) );
+ $claims->addClaim( $claim3, 0 );
+ $this->assertEquals( 0, $claims->indexOf( $claim3 ), 'Inserting
claim at the beginning failed' );
+
+ // Insert claim at another index:
+ $claim4 = $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P18' ) ) );
+ $claims->addClaim( $claim4, 1 );
+ $this->assertEquals( 1, $claims->indexOf( $claim4 ), 'Inserting
claim at index 1 failed' );
+ }
+
+ public function testIndexOf() {
+ $claims = new Claims();
+ $claimArray = array(
+ $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P1' ) ) ),
+ $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P2' ) ) ),
+ $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P3' ) ) ),
+ );
+ $excludedClaim = $this->makeClaim( new PropertyNoValueSnak( new
PropertyId( 'P99' ) ) );
+
+ foreach( $claimArray as $claim ) {
+ $claims->addClaim( $claim );
+ }
+
+ $this->assertFalse( $claims->indexOf( $excludedClaim ) );
+
+ $i = 0;
+ foreach( $claimArray as $claim ) {
+ $this->assertEquals( $i++, $claims->indexOf( $claim ) );
+ }
}
public function testAppend() {
@@ -602,7 +633,7 @@
$this->assertEquals( $claimsA->getHash(), $claimsB->getHash(),
'same content' );
}
- public function testItrerator() {
+ public function testIterator() {
$claims = new Claims( array(
$this->makeClaim( new PropertyNoValueSnak( new
PropertyId( "P42" ) ) ),
$this->makeClaim( new PropertySomeValueSnak( new
PropertyId( "P42" ) ) ),
--
To view, visit https://gerrit.wikimedia.org/r/90318
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iefeebdcc4c6bc8c477d3f234399bcd403466bd52
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits