From c365af2a2773ae07657b95955711e465fc542569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiemo=20M=C3=A4ttig?= Date: Thu, 28 Apr 2016 15:10:11 +0200 Subject: [PATCH] Add index parameter to Statement::addStatement --- src/Statement/StatementList.php | 28 +++++++++++++---- tests/unit/Statement/StatementListTest.php | 36 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/Statement/StatementList.php b/src/Statement/StatementList.php index 847f1238..2846dd0d 100644 --- a/src/Statement/StatementList.php +++ b/src/Statement/StatementList.php @@ -26,6 +26,7 @@ * @license GPL-2.0+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > * @author Bene* < benestar.wikimedia@gmail.com > + * @author Thiemo Mättig */ class StatementList implements IteratorAggregate, Comparable, Countable { @@ -74,8 +75,23 @@ public function getPropertyIds() { return $propertyIds; } - public function addStatement( Statement $statement ) { - $this->statements[] = $statement; + /** + * @since 1.0, setting an index is supported since 6.1 + * @see ReferenceList::addReference + * + * @param Statement $statement + * @param int|null $index New position of the added statement, or null to append. + * + * @throws InvalidArgumentException + */ + public function addStatement( Statement $statement, $index = null ) { + if ( $index === null ) { + $this->statements[] = $statement; + } elseif ( is_int( $index ) && $index >= 0 ) { + array_splice( $this->statements, $index, 0, array( $statement ) ); + } else { + throw new InvalidArgumentException( '$index must be a non-negative integer or null' ); + } } /** @@ -91,7 +107,7 @@ public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references $statement = new Statement( $mainSnak, $qualifiers, $references ); $statement->setGuid( $guid ); - $this->addStatement( $statement ); + $this->statements[] = $statement; } /** @@ -139,7 +155,7 @@ public function getByPropertyId( PropertyId $id ) { foreach ( $this->statements as $statement ) { if ( $statement->getPropertyId()->equals( $id ) ) { - $statementList->addStatement( $statement ); + $statementList->statements[] = $statement; } } @@ -159,7 +175,7 @@ public function getByRank( $acceptableRanks ) { foreach ( $this->statements as $statement ) { if ( array_key_exists( $statement->getRank(), $acceptableRanks ) ) { - $statementList->addStatement( $statement ); + $statementList->statements[] = $statement; } } @@ -318,7 +334,7 @@ public function filter( StatementFilter $filter ) { foreach ( $this->statements as $statement ) { if ( $filter->statementMatches( $statement ) ) { - $statementList->addStatement( $statement ); + $statementList->statements[] = $statement; } } diff --git a/tests/unit/Statement/StatementListTest.php b/tests/unit/Statement/StatementListTest.php index 4ed9c1e0..dfb523e1 100644 --- a/tests/unit/Statement/StatementListTest.php +++ b/tests/unit/Statement/StatementListTest.php @@ -236,6 +236,42 @@ public function testAddStatementWithGuid() { $this->assertEquals( new StatementList( $statement ), $list ); } + public function testGivenNegativeIndex_addStatementFails() { + $statement = new Statement( new PropertyNoValueSnak( 1 ) ); + $list = new StatementList(); + + $this->setExpectedException( 'InvalidArgumentException' ); + $list->addStatement( $statement, -1 ); + } + + public function testGivenLargeIndex_addStatementAppends() { + $statement = new Statement( new PropertyNoValueSnak( 1 ) ); + $list = new StatementList(); + + $list->addStatement( $statement, 1000 ); + $this->assertEquals( new StatementList( $statement ), $list ); + } + + public function testGivenZeroIndex_addStatementPrepends() { + $statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); + $statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); + $list = new StatementList( $statement2 ); + + $list->addStatement( $statement1, 0 ); + $this->assertEquals( new StatementList( $statement1, $statement2 ), $list ); + } + + public function testGivenValidIndex_addStatementInserts() { + $statement1 = new Statement( new PropertyNoValueSnak( 1 ) ); + $statement2 = new Statement( new PropertyNoValueSnak( 2 ) ); + $statement3 = new Statement( new PropertyNoValueSnak( 3 ) ); + $list = new StatementList( $statement1, $statement3 ); + + $list->addStatement( $statement2, 1 ); + $this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list ); + $this->assertSame( array( 0, 1, 2 ), array_keys( $list->toArray() ), 'array keys' ); + } + public function testGivenGuidOfPresentStatement_statementIsRemoved() { $statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' ); $statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );