Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/88969


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, 59 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel 
refs/changes/69/88969/1

diff --git a/DataModel/ReferenceList.php b/DataModel/ReferenceList.php
index e01569e..f1ef372 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,41 @@
         * @since 0.1
         *
         * @param Reference $reference
+        * @param int|null $index
+        *
+        * @throws InvalidArgumentException
         */
-       public function addReference( Reference $reference ) {
-               $this->attach( $reference );
+       public function addReference( Reference $reference, $index = null ) {
+               if( is_null( $index ) || $index >= $this->count() ) {
+                       // Append object to the end of the reference list.
+                       $this->attach( $reference );
+
+               } elseif( !is_integer( $index ) ) {
+                       throw new InvalidArgumentException( 'Index needs to be 
an integer value' );
+
+               } else {
+                       // Insert reference at a specific 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: newchange
Gerrit-Change-Id: I38031e6eae6bed107241e4a9c34a67c7c7abee13
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

Reply via email to