diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index c0b53c65..27b58273 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -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) diff --git a/src/Statement/StatementList.php b/src/Statement/StatementList.php index bfadc275..97a16d5e 100644 --- a/src/Statement/StatementList.php +++ b/src/Statement/StatementList.php @@ -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; + } + else { + foreach ( $statements as $statement ) { + $this->statements[] = $statement; + } } } @@ -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. @@ -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 */ diff --git a/tests/unit/Statement/StatementListTest.php b/tests/unit/Statement/StatementListTest.php index fbfb3bef..af27c0eb 100644 --- a/tests/unit/Statement/StatementListTest.php +++ b/tests/unit/Statement/StatementListTest.php @@ -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; @@ -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' ) ) + ); + } + }