Skip to content

Commit

Permalink
Introduce StatementGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
Benestar committed Jun 23, 2015
1 parent e9a7171 commit 7333df7
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Statement/StatementGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 3.1
*
* @license GNU GPL v2+
* @author Bene* < [email protected] >
*/
class StatementGroup implements PropertyIdProvider {

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

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

public function __construct( PropertyId $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;
}

}

0 comments on commit 7333df7

Please sign in to comment.