Skip to content

Commit 1559058

Browse files
committed
Introduce StatementGroup
Per #479 (comment)
1 parent e9a7171 commit 1559058

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

src/Statement/StatementGroup.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 4.2
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+
/**
31+
* @param PropertyId|int $propertyId
32+
*/
33+
public function __construct( $propertyId ) {
34+
if ( is_int( $propertyId ) ) {
35+
$propertyId = PropertyId::newFromNumber( $propertyId );
36+
}
37+
38+
if ( !( $propertyId instanceof PropertyId ) ) {
39+
throw new InvalidArgumentException( '$propertyId must be an integer or an instance of PropertyId' );
40+
}
41+
42+
$this->propertyId = $propertyId;
43+
}
44+
45+
/**
46+
* @param Statement[]|Traversable $statements
47+
* @throws InvalidArgumentException
48+
*/
49+
public function addStatements( $statements ) {
50+
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
51+
throw new InvalidArgumentException( '$statements must be an array or an instance of Traversable' );
52+
}
53+
54+
foreach ( $statements as $statement ) {
55+
if ( !( $statement instanceof Statement ) ) {
56+
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
57+
}
58+
59+
$this->addStatement( $statement );
60+
}
61+
}
62+
63+
/**
64+
* @param Statement $statement
65+
* @throws InvalidArgumentException
66+
*/
67+
public function addStatement( Statement $statement ) {
68+
if ( !$statement->getPropertyId()->equals( $this->propertyId ) ) {
69+
throw new InvalidArgumentException( '$statement must have the property id ' . $this->propertyId->getSerialization() );
70+
}
71+
72+
$this->statementsByRank[$statement->getRank()][] = $statement;
73+
}
74+
75+
/**
76+
* @see PropertyIdProvider::getPropertyId
77+
* @return PropertyId
78+
*/
79+
public function getPropertyId() {
80+
return $this->propertyId;
81+
}
82+
83+
/**
84+
* @param int $rank
85+
* @return Statement[]
86+
*/
87+
public function getByRank( $rank ) {
88+
if ( isset( $this->statementsByRank[$rank] ) ) {
89+
return $this->statementsByRank[$rank];
90+
}
91+
92+
return array();
93+
}
94+
95+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
4+
namespace Wikibase\DataModel\Tests\Statement;
5+
6+
use DataValues\StringValue;
7+
use InvalidArgumentException;
8+
use Wikibase\DataModel\Entity\PropertyId;
9+
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10+
use Wikibase\DataModel\Snak\PropertyValueSnak;
11+
use Wikibase\DataModel\Statement\Statement;
12+
use Wikibase\DataModel\Statement\StatementGroup;
13+
14+
/**
15+
* @covers Wikibase\DataModel\Statement\StatementGroup
16+
*
17+
* @licence GNU GPL v2+
18+
* @author Bene* < benestar.wikimedia@gmail.com >
19+
*/
20+
class StatementGroupTest extends \PHPUnit_Framework_TestCase {
21+
22+
public function testConstructor_numericId() {
23+
$statementGroup = new StatementGroup( 42 );
24+
$this->assertEquals( new PropertyId( 'P42' ), $statementGroup->getPropertyId() );
25+
}
26+
27+
public function testConstructor_propertyId() {
28+
$statementGroup = new StatementGroup( new PropertyId( 'P42' ) );
29+
$this->assertEquals( new PropertyId( 'P42' ), $statementGroup->getPropertyId() );
30+
}
31+
32+
/**
33+
* @expectedException InvalidArgumentException
34+
*/
35+
public function testConstructor_invalidArgument() {
36+
new StatementGroup( 'foo' );
37+
}
38+
39+
public function testAddStatement_validPropertyId() {
40+
$statementGroup = new StatementGroup( 42 );
41+
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
42+
$statementGroup->addStatement( $statement );
43+
44+
$this->assertEquals( array( $statement ), $statementGroup->getByRank( Statement::RANK_NORMAL ) );
45+
}
46+
47+
/**
48+
* @expectedException InvalidArgumentException
49+
*/
50+
public function testAddStatement_invalidPropertyId() {
51+
$statementGroup = new StatementGroup( 42 );
52+
$statement = new Statement( new PropertyNoValueSnak( 12 ) );
53+
$statementGroup->addStatement( $statement );
54+
}
55+
56+
public function testAddStatements() {
57+
$statementGroup = new StatementGroup( 42 );
58+
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
59+
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
60+
$baz = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
61+
$baz->setRank( Statement::RANK_PREFERRED );
62+
$statementGroup->addStatements( array( $foo, $bar, $baz ) );
63+
64+
$this->assertEquals( array(), $statementGroup->getByRank( Statement::RANK_DEPRECATED ) );
65+
$this->assertEquals( array( $foo, $bar ), $statementGroup->getByRank( Statement::RANK_NORMAL ) );
66+
$this->assertEquals( array( $baz ), $statementGroup->getByRank( Statement::RANK_PREFERRED ) );
67+
}
68+
69+
/**
70+
* @expectedException InvalidArgumentException
71+
*/
72+
public function testAddStatements_invalidPropertyIds() {
73+
$statementGroup = new StatementGroup( 42 );
74+
$foo = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
75+
$bar = new Statement( new PropertyValueSnak( 42, new StringValue( 'foo' ) ) );
76+
$baz = new Statement( new PropertyValueSnak( 12, new StringValue( 'foo' ) ) );
77+
$statementGroup->addStatements( array( $foo, $bar, $baz ) );
78+
}
79+
80+
/**
81+
* @expectedException InvalidArgumentException
82+
*/
83+
public function testAddStatements_noStatement() {
84+
$statementGroup = new StatementGroup( 42 );
85+
$statement = new Statement( new PropertyNoValueSnak( 12 ) );
86+
$statementGroup->addStatements( array( $statement, 'foo' ) );
87+
}
88+
89+
/**
90+
* @expectedException InvalidArgumentException
91+
*/
92+
public function testAddStatements_noArray() {
93+
$statementGroup = new StatementGroup( 42 );
94+
$statementGroup->addStatements( 'foo' );
95+
}
96+
97+
}

0 commit comments

Comments
 (0)