Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Statement/StatementList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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' );
}
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -318,7 +334,7 @@ public function filter( StatementFilter $filter ) {

foreach ( $this->statements as $statement ) {
if ( $filter->statementMatches( $statement ) ) {
$statementList->addStatement( $statement );
$statementList->statements[] = $statement;
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/unit/Statement/StatementListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down