Skip to content

Commit

Permalink
Introduce StatementGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
Benestar committed Aug 26, 2015
1 parent e9a7171 commit 1559058
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/Statement/StatementGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Wikibase\DataModel\Statement;

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

/**
* List of statements with the same property id, grouped by rank.
*
* @since 4.2
*
* @license GNU GPL v2+
* @author Bene* < [email protected] >
*/
class StatementGroup implements PropertyIdProvider {

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

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

/**
* @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->statementsByRank[$statement->getRank()][] = $statement;
}

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

/**
* @param int $rank
* @return Statement[]
*/
public function getByRank( $rank ) {
if ( isset( $this->statementsByRank[$rank] ) ) {
return $this->statementsByRank[$rank];
}

return array();
}

}
97 changes: 97 additions & 0 deletions tests/unit/Statement/StatementGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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() {
$statementGroup = new StatementGroup( 42 );
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
$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( 'foo' ) ) );
$baz = new Statement( new PropertyValueSnak( 12, new StringValue( 'foo' ) ) );
$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' );
}

}

0 comments on commit 1559058

Please sign in to comment.