Skip to content

Commit 1fd3c33

Browse files
committed
[DRAFT] Introduce StatementGroup
Per #479 (comment)
1 parent e9a7171 commit 1fd3c33

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/Statement/StatementGroup.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Statement;
4+
5+
use InvalidArgumentException;
6+
use Traversable;
7+
use Wikibase\DataModel\Entity\PropertyId;
8+
use Wikibase\DataModel\PropertyIdProvider;
9+
10+
/**
11+
* List of statements with the same property id, grouped by rank.
12+
*
13+
* @since 3.1
14+
*
15+
* @license GNU GPL v2+
16+
* @author Bene* < benestar.wikimedia@gmail.com >
17+
*/
18+
class StatementGroup implements PropertyIdProvider {
19+
20+
/**
21+
* @var Statement[][]
22+
*/
23+
private $statementsByRank = array();
24+
25+
/**
26+
* @var PropertyId
27+
*/
28+
private $propertyId;
29+
30+
public function __construct( PropertyId $propertyId ) {
31+
$this->propertyId = $propertyId;
32+
}
33+
34+
/**
35+
* @param Statement[]|Traversable $statements
36+
* @throws InvalidArgumentException
37+
*/
38+
public function addStatements( $statements ) {
39+
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
40+
throw new InvalidArgumentException( '$statements must be an array or an instance of Traversable' );
41+
}
42+
43+
foreach ( $statements as $statement ) {
44+
if ( !( $statement instanceof Statement ) ) {
45+
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
46+
}
47+
48+
$this->addStatement( $statement );
49+
}
50+
}
51+
52+
/**
53+
* @param Statement $statement
54+
* @throws InvalidArgumentException
55+
*/
56+
public function addStatement( Statement $statement ) {
57+
if ( !$statement->getPropertyId()->equals( $this->propertyId ) ) {
58+
throw new InvalidArgumentException( '$statement must have the property id ' . $this->propertyId->getSerialization() );
59+
}
60+
61+
$this->statementsByRank[$statement->getRank()][] = $statement;
62+
}
63+
64+
/**
65+
* @see PropertyIdProvider::getPropertyId
66+
* @return PropertyId
67+
*/
68+
public function getPropertyId() {
69+
return $this->propertyId;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)