jenkins-bot has submitted this change and it was merged.

Change subject: Allow passing index when adding a Reference to a ReferenceList
......................................................................


Allow passing index when adding a Reference to a ReferenceList

In order to allow more flexibility when adding a Reference to a ReferenceList,
an index may be specified. This allows inserting a Reference at an other place
than at the end.

Change-Id: I38031e6eae6bed107241e4a9c34a67c7c7abee13
---
M DataModel/ReferenceList.php
M DataModel/References.php
M tests/phpunit/ReferenceListTest.php
3 files changed, 65 insertions(+), 3 deletions(-)

Approvals:
  Tobias Gritschacher: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DataModel/ReferenceList.php b/DataModel/ReferenceList.php
index e01569e..e8756ae 100644
--- a/DataModel/ReferenceList.php
+++ b/DataModel/ReferenceList.php
@@ -3,6 +3,7 @@
 namespace Wikibase;
 
 use Hashable;
+use InvalidArgumentException;
 
 /**
  * Implementation of the References interface.
@@ -29,9 +30,48 @@
         * @since 0.1
         *
         * @param Reference $reference
+        * @param int|null $index
+        *
+        * @throws InvalidArgumentException
         */
-       public function addReference( Reference $reference ) {
+       public function addReference( Reference $reference, $index = null ) {
+               if( !is_null( $index ) && !is_integer( $index ) ) {
+                       throw new InvalidArgumentException( 'Index needs to be 
an integer value' );
+               } else if ( is_null( $index ) || $index >= count( $this ) ) {
+                       // Append object to the end of the reference list.
+                       $this->attach( $reference );
+               } else {
+                       $this->insertReferenceAtIndex( $reference, $index );
+               }
+       }
+
+       /**
+        * @since 0.1
+        *
+        * @param Reference $reference
+        * @param int $index
+        */
+       protected function insertReferenceAtIndex( Reference $reference, $index 
) {
+               $referencesToShift = array();
+               $i = 0;
+
+               // Determine the references that need to be shifted and detach 
them:
+               foreach( $this as $object ) {
+                       if( $i++ >= $index ) {
+                               $referencesToShift[] = $object;
+                       }
+               }
+
+               foreach( $referencesToShift as $object ) {
+                       $this->detach( $object );
+               }
+
+               // Attach the new reference and reattach the previously 
detached references:
                $this->attach( $reference );
+
+               foreach( $referencesToShift as $object ) {
+                       $this->attach( $object );
+               }
        }
 
        /**
diff --git a/DataModel/References.php b/DataModel/References.php
index 3b4a135..3ca2e67 100644
--- a/DataModel/References.php
+++ b/DataModel/References.php
@@ -21,8 +21,9 @@
         * @since 0.1
         *
         * @param Reference $reference
+        * @param int|null $index
         */
-       public function addReference( Reference $reference );
+       public function addReference( Reference $reference, $index = null );
 
        /**
         * Returns if the list contains a reference with the same hash as the 
provided reference.
diff --git a/tests/phpunit/ReferenceListTest.php 
b/tests/phpunit/ReferenceListTest.php
index 6ba3cc0..a265463 100644
--- a/tests/phpunit/ReferenceListTest.php
+++ b/tests/phpunit/ReferenceListTest.php
@@ -123,14 +123,35 @@
         * @param \Wikibase\ReferenceList $array
         */
        public function testAddReference( ReferenceList $array ) {
+               // Append object to the end:
                $elementCount = count( $array );
 
                $elements = $this->getElementInstances();
                $element = array_shift( $elements );
-
                $array->addReference( $element );
 
                $this->assertEquals( ++$elementCount, count( $array ) );
+
+               // Insert object at the beginning:
+               $elements = $this->getElementInstances();
+               $element = array_shift( $elements );
+               $array->addReference( $element, 0 );
+
+               $array->rewind();
+
+               $this->assertEquals( ++$elementCount, count( $array ) );
+               $this->assertEquals( $array->current(), $element, 'Inserted 
object at the beginning' );
+
+               // Insert object at another index:
+               $elements = $this->getElementInstances();
+               $element = array_shift( $elements );
+               $array->addReference( $element, 1 );
+
+               $array->rewind();
+               $array->next();
+
+               $this->assertEquals( ++$elementCount, count( $array ) );
+               $this->assertEquals( $array->current(), $element, 'Inserted 
object at index 1' );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/88969
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I38031e6eae6bed107241e4a9c34a67c7c7abee13
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to