Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce StatementGroup #507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
130 changes: 130 additions & 0 deletions src/Statement/StatementGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Wikibase\DataModel\Statement;

use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\PropertyIdProvider;

/**
* List of statements with the same property id.
*
* @since 4.3
*
* @license GNU GPL v2+
* @author Bene* < [email protected] >
*/
class StatementGroup implements IteratorAggregate, Countable {

/**
* @var PropertyId
*/
private $propertyId;

/**
* @var Statement[]
*/
private $statements = array();

/**
* @param PropertyId|int $propertyId
*/
public function __construct( $propertyId ) {
if ( is_int( $propertyId ) ) {
$propertyId = PropertyId::newFromNumber( $propertyId );
}

if ( !( $propertyId instanceof PropertyId ) ) {
throw new InvalidArgumentException( '$propertyId must be an integer or an instance of PropertyId' );
}

$this->propertyId = $propertyId;
}

/**
* @param Statement[]|Traversable $statements
* @throws InvalidArgumentException
*/
public function addStatements( $statements ) {
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
throw new InvalidArgumentException( '$statements must be an array or an instance of Traversable' );
}

foreach ( $statements as $statement ) {
if ( !( $statement instanceof Statement ) ) {
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
}

$this->addStatement( $statement );
}
}

/**
* @param Statement $statement
* @throws InvalidArgumentException
*/
public function addStatement( Statement $statement ) {
if ( !$statement->getPropertyId()->equals( $this->propertyId ) ) {
throw new InvalidArgumentException( '$statement must have the property id ' . $this->propertyId->getSerialization() );
}

$this->statements[] = $statement;
}

/**
* @return PropertyId
*/
public function getPropertyId() {
return $this->propertyId;
}

/**
* @param int $rank
* @return Statement[]
*/
public function getByRank( $rank ) {
$statements = array();

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

return $statements;
}

/**
* @return Traversable
*/
public function getIterator() {
return new ArrayIterator( $this->statements );
}

/**
* @return Statement[] Numerically indexed (non-sparse) array.
*/
public function toArray() {
return $this->statements;
}

/**
* @see Countable::count
*
* @return int
*/
public function count() {
return count( $this->statements );
}

/**
* @return bool
*/
public function isEmpty() {
return empty( $this->statements );
}

}
139 changes: 139 additions & 0 deletions tests/unit/Statement/StatementGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php


namespace Wikibase\DataModel\Tests\Statement;

use DataValues\StringValue;
use InvalidArgumentException;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Statement\StatementGroup;

/**
* @covers Wikibase\DataModel\Statement\StatementGroup
*
* @licence GNU GPL v2+
* @author Bene* < [email protected] >
*/
class StatementGroupTest extends \PHPUnit_Framework_TestCase {

public function testConstructor_numericId() {
$statementGroup = new StatementGroup( 42 );
$this->assertEquals( new PropertyId( 'P42' ), $statementGroup->getPropertyId() );
}

public function testConstructor_propertyId() {
$statementGroup = new StatementGroup( new PropertyId( 'P42' ) );
$this->assertEquals( new PropertyId( 'P42' ), $statementGroup->getPropertyId() );
}

/**
* @expectedException InvalidArgumentException
*/
public function testConstructor_invalidArgument() {
new StatementGroup( 'foo' );
}

public function testAddStatement_validPropertyId() {
$statementGroup = new StatementGroup( 42 );
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
$statementGroup->addStatement( $statement );

$this->assertEquals( array( $statement ), $statementGroup->getByRank( Statement::RANK_NORMAL ) );
}

/**
* @expectedException InvalidArgumentException
*/
public function testAddStatement_invalidPropertyId() {
$statementGroup = new StatementGroup( 42 );
$statement = new Statement( new PropertyNoValueSnak( 12 ) );
$statementGroup->addStatement( $statement );
}

public function testAddStatements_validPropertyIds() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'bar' ) ) );
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'baz' ) ) );
$baz->setRank( Statement::RANK_PREFERRED );
$statementGroup->addStatements( array( $foo, $bar, $baz ) );

$this->assertEquals( array(), $statementGroup->getByRank( Statement::RANK_DEPRECATED ) );
$this->assertEquals( array( $foo, $bar ), $statementGroup->getByRank( Statement::RANK_NORMAL ) );
$this->assertEquals( array( $baz ), $statementGroup->getByRank( Statement::RANK_PREFERRED ) );
}

/**
* @expectedException InvalidArgumentException
*/
public function testAddStatements_invalidPropertyIds() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'bar' ) ) );
$baz = new Statement( new PropertyValueSnak( 12, new StringValue( 'baz' ) ) );
$statementGroup->addStatements( array( $foo, $bar, $baz ) );
}

/**
* @expectedException InvalidArgumentException
*/
public function testAddStatements_noStatement() {
$statementGroup = new StatementGroup( 42 );
$statement = new Statement( new PropertyNoValueSnak( 12 ) );
$statementGroup->addStatements( array( $statement, 'foo' ) );
}

/**
* @expectedException InvalidArgumentException
*/
public function testAddStatements_noArray() {
$statementGroup = new StatementGroup( 42 );
$statementGroup->addStatements( 'foo' );
}

public function testToArray() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'bar' ) ) );
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'baz' ) ) );
$statementGroup->addStatements( array( $foo, $bar, $baz ) );

$this->assertEquals( array( $foo, $bar, $baz ), $statementGroup->toArray() );
}

public function testCount_emptyGroup() {
$statementGroup = new StatementGroup( 42 );

$this->assertEquals( 0, $statementGroup->count() );
}

public function testCount_filledGroup() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'bar' ) ) );
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'baz' ) ) );
$statementGroup->addStatements( array( $foo, $bar, $baz ) );

$this->assertEquals( 3, $statementGroup->count() );
}

public function testEmpty_emptyGroup() {
$statementGroup = new StatementGroup( 42 );

$this->assertTrue( $statementGroup->isEmpty() );
}

public function testEmpty_filledGroup() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'bar' ) ) );
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'baz' ) ) );
$statementGroup->addStatements( array( $foo, $bar, $baz ) );

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

}