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
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Version 2.3.0 (dev)

* Added `AliasGroupList::toArray`
* Added `StatementList::getMainSnaks`
* Added `StatementList::getWithPropertyId`
* `BestStatementsFinder::getBestStatementsForProperty` no longer throws an `OutOfBounds` exception

## Version 2.2.0 (2014-11-10)
Expand Down
47 changes: 41 additions & 6 deletions src/Statement/StatementList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ class StatementList implements IteratorAggregate, Comparable, Countable {
* @throws InvalidArgumentException
*/
public function __construct( $statements = array() ) {
$this->addStatements( $statements );
}

private function addStatements( $statements ) {
$this->assertAreStatements( $statements );

foreach ( $statements as $statement ) {
$this->statements[] = $statement;
if ( is_array( $statements ) ) {
$this->statements = $statements;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overwriting the current statements, which is probably okay given the current usage of the function (in this class)... but it looks a bit odd given the function name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. There was little reason for the method anyway, so inlined now.

}
else {
foreach ( $statements as $statement ) {
$this->statements[] = $statement;
}
}
}

Expand Down Expand Up @@ -125,6 +126,25 @@ public function getWithUniqueMainSnaks() {
return new self( $statements );
}

/**
* @since 2.3
*
* @param PropertyId $id
*
* @return StatementList
*/
public function getWithPropertyId( PropertyId $id ) {
$statements = array();

foreach ( $this->statements as $statement ) {
if ( $statement->getPropertyId()->equals( $id ) ) {
$statements[] = $statement;
}
}

return new self( $statements );
}

/**
* Returns a list of all Snaks on this StatementList. This includes at least the main snaks of
* Claims, the snaks from Claim qualifiers, and the snaks from Statement References.
Expand All @@ -148,6 +168,21 @@ public function getAllSnaks() {
return $snaks;
}

/**
* @since 2.3
*
* @return Snak[]
*/
public function getMainSnaks() {
$snaks = array();

foreach ( $this->statements as $statement ) {
$snaks[] = $statement->getMainSnak();
}

return $snaks;
}

/**
* @return Traversable
*/
Expand Down
48 changes: 47 additions & 1 deletion tests/unit/Statement/StatementListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Snak\SnakList;
use Wikibase\DataModel\Statement\Statement;
Expand Down Expand Up @@ -394,9 +396,53 @@ public function testEmptyListIsEmpty() {
public function testNonEmptyListIsNotEmpty() {
$list = new StatementList( array(
$this->getStatementWithSnak( 1, 'foo' ),
));
) );

$this->assertFalse( $list->isEmpty() );
}

public function testGetMainSnaks() {
$list = new StatementList();

$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
$list->addNewStatement( new PropertyNoValueSnak( 1337 ), array( new PropertyNoValueSnak( 32202 ) ) );
$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );

$this->assertEquals(
array(
new PropertyNoValueSnak( 42 ),
new PropertyNoValueSnak( 1337 ),
new PropertyNoValueSnak( 9001 ),
),
$list->getMainSnaks()
);
}

public function testGivenNotKnownPropertyId_getWithPropertyIdReturnsEmptyList() {
$list = new StatementList();
$list->addNewStatement( new PropertyNoValueSnak( 42 ) );

$this->assertEquals(
new StatementList(),
$list->getWithPropertyId( new PropertyId( 'P2' ) )
);
}

public function testGivenKnownPropertyId_getWithPropertyIdReturnsListWithOnlyMatchingStatements() {
$list = new StatementList();
$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
$list->addNewStatement( new PropertySomeValueSnak( 42 ) );
$list->addNewStatement( new PropertySomeValueSnak( 9001 ) );

$expected = new StatementList();
$expected->addNewStatement( new PropertyNoValueSnak( 42 ) );
$expected->addNewStatement( new PropertySomeValueSnak( 42 ) );

$this->assertEquals(
$expected,
$list->getWithPropertyId( new PropertyId( 'P42' ) )
);
}

}